Hi Guys. I’m a noob in coding so please bare with me.
The case:
I’m using VB to send three sets of numbers which separated by a comma. Arduino would then parse these numbers to be able to blink and led . First number is to turn the led “on” or “off”; second number is the “On” delay; and the third number be the “off” delay.
The problem:
Sending 1,500,500 makes the led blink, however, sending 0,0,0 does not make the program stop the blinking. The led continuously blinks until the reset button is pressed. My understanding is that the program is in an infinite loop inside the ‘while’ statement. What I don’t get is why can’t the program detect when the condition for the ‘while’ statement has changed?
The blink of the led is much slower than that of the value sent from the VB. Eg: when sending 1,500,500 , the actual blink of the led feels like 5000 “on” delay and 5000 “off” delay. Though probably this is due to poor coding, I’m very much interested in finding ways to make this simpler.
The Code:
int ledPin =13;
void setup(){
Serial.begin(9600);
pinMode(ledPin,OUTPUT);
digitalWrite(ledPin,LOW);
}
void loop(){
while (Serial.available() > 0) {
int onswitch = Serial.parseInt();
while (onswitch==1){pulse();}
}}
void pulse(){
while (Serial.available() > 0) {
int onswitch = Serial.parseInt();
int ontime = Serial.parseInt();
int offtime = Serial.parseInt();
digitalWrite (ledPin,HIGH);
delay (ontime);
digitalWrite (ledPin,LOW);
delay (offtime);}
}
thanks in advance for the help.