2013-10-22 18 views
1

我正试图在斜边进入一定范围时让指示灯闪烁。但它似乎正在通过斜边范围的值超过它应该的次数。 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); 
    } 
} 

修订ARDUINO:

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); 
    } 
} 

回答

2

处理代码为想必写出来连续不断地连续。但是,当斜边进入你设定的范围时,Arduino会有这些电话。我认为这会导致它滞后,所以在清除延迟期间出现的串行数据积压时,它会一直闪烁。

我认为更好的方法是根本避免使用delay(),所以Arduino可以尽可能快地处理串行数据。在每个循环中,它应该首先获取最新的串行数据(如果有的话)。基于此,它应该计算出并存储LED当前应该做的事情(即它应该是闪烁的,否则应该是什么亮度)。之后(不管是否实际接收到任何串行数据),LED可以从存储状态更新。切记不要使用delay()进行闪烁。相反,您可以跟踪上次闪现的时间,并确定自那以后是否已过去100 ms(使用millis())。如果是这样,请将其关闭。如果再过100 ms,请重新打开。

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

+0

感谢您的回答!我看了一些关于milli如何工作的例子,我尝试了一些东西,但现在它一直没有工作。 Cna你请看看它,并告诉我我做错了什么? – tailedmouse

+1

不客气!它看起来像你的新代码中的问题是你每次循环更新'pastTime'。相反,尝试只在实际打开或关闭LED时进行更新。这样,它会记录闪光之间的时间,而不是循环之间的时间。 –

+1

哦,我的上帝!它工作....谢谢嘘!还有另一个问题呢!这真的很愚蠢,我正在划分价值,但我忘了改变230!它现在有效! – tailedmouse