1

当斜边进入一定范围时,我试图让我的 LED 闪烁。但似乎它通过斜边范围的值比它应该的次数更多。LED 在恢复正常之前闪烁大约 30 -40 次。不知道如何解决这个问题。

这是我的处理代码:

import processing.serial.*;

float r_height; // rise of the slope
float r_width; // run of the slope
float hypotnuse; // hypotenuse of the right angle
int d = 20; // diameter of the chocolate
float x ; // x of the chocolate destination
float y ; // y of the chocolate destination
int ledGlow; // how much the LED will glow

Serial myPort;  // serial port object

void setup () {

  size (510, 510); // size of the canvas
  String portName = Serial.list()[8]; // my arduino port
  myPort = new Serial(this, portName, 9600); 
  background (0); // color of the background
  fill(204); // fill of the ellipse
  ellipseMode (CORNER); //Ellipse mode
  x = 0; //The placement on initial X for chocolate
  y = 0; // the placement on initial Y for chocolate
  ellipse (x, y, d, d); // ellipse
  frameRate (30);

}

void draw () {

  r_height = mouseY - y; // rise
  r_width =  mouseX - x; //run
  hypotnuse = sqrt (( (sq(r_height)) + (sq (r_width)))); //A^2 +B^2 = C^2
  ledGlow = 255 - (round (hypotnuse/2.84)); // flipping the values
   myPort.write(ledGlow); // The value being sent to the Arduino
  println (ledGlow);


  } 

这是arduino代码:

float val; // Data received from the serial port
int ledPin = 9; 


void setup() {
  pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
  Serial.begin(9600); // Start serial communication at 9600 bps
}

void loop() {
  if (Serial.available()) 
  { // If data is available to read,
    val = Serial.read(); // read it and store it in val

    // long steps2move = val.toInt();
  }

  if (val > 230) {

    analogWrite (ledPin, 255) ; // I have already tried digitalWrite
    delay (100);
    analogWrite (ledPin, 1) ;
    delay (100);

  } 

  else if  (val < 230) {
    analogWrite(ledPin, val);
  }
}

更新的阿杜诺:

float val; // Data received from the serial port
int ledPin = 9; // Set the pin to digital I/O 13
unsigned long currentTime  = 0; 
unsigned long pastTime     = 0; 
int currentState = 0; 
int wait         = 0; 

void setup() {
  pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
  Serial.begin(9600); // Start serial communication at 9600 bps
}

void loop() {
  if (Serial.available()) 
  { // If data is available to read,
    val = Serial.read(); // read it and store it in val

    // long steps2move = val.toInt();
  }



  if (val > 230) {

  pastTime = currentTime; 
  currentTime = millis();   

  unsigned long timePassed = currentTime - pastTime;
  if(timePassed >= wait) 
  {
    switch(currentState ) 
    {
      case 0:                  
        digitalWrite(9, HIGH); 
        wait  = 500;   
        currentState = 1;     
        break;            

      case 1:
        digitalWrite(9, LOW); 
        wait  = 500;  
        currentState = 0;
       break;
    }
  }

  } 

  else if  (val < 230) {
    analogWrite(ledPin, val/2);
  }
}
4

1 回答 1

2

处理代码大概是不断写出串行。但是,当斜边进入您设置的范围时,Arduino 就会有这些delay()调用。我认为这将导致它落后,因此它会在清除延迟期间传入的串行数据积压的同时不断闪烁。

我认为更好的方法是完全避免使用delay(),这样 Arduino 可以尽可能快地处理串行数据。在每个循环上,它应该首先获取最新的串行数据(如果有的话)。基于此,它应该计算并存储 LED 当前应该做什么(即它是否应该闪烁,或者它应该是什么亮度)。

之后(无论是否实际接收到任何串行数据),可以从存储的状态更新 LED。记住不要delay()用于闪烁。相反,您可以跟踪它上次闪烁的时间,并确定从那时起是否经过了 100 毫秒(使用millis())。如果是这样,请将其关闭。如果又过了 100 毫秒,请将其重新打开。

这种方法将闪存时序与串行数据分离,因此希望它能更好地工作。

于 2013-10-22T15:14:55.627 回答