Go Down

Topic: [SOLVED]Serial input affecting a delay (Read 2167 times) previous topic - next topic

gravebond

Apr 02, 2011, 09:46 pm Last Edit: Apr 03, 2011, 12:22 pm by gravebond Reason: 1
Hey guys. Im a bit perplexed! Im trying to set the rate of blinking of an LED (in an optocoupler) by reading what comes through the serial port. Here is my code;
Code: [Select]
#define OPTO 13 // optocoupler pin
int val =0; // variable to store the serial
int pause =0; // variable for our blinks

void setup(){
 
Serial.begin(300);
pinMode (OPTO, OUTPUT);

}

void loop (){
while (Serial.available() ==0 ); // Arduino waits to recieve data
 
int val = Serial.read()-('0'); // only reads the first byte of data

int pause = map (val,0,9, 15, 100); // hence maximum of 9

 pause = constrain (pause , 15, 100);

 
digitalWrite(OPTO, HIGH);
delay(pause);
digitalWrite (OPTO, LOW);
delay(pause);
 
  Serial.println (pause);

 
}


The problem is that it only blinks the LED at the specified rate once, and then the serial goes back to 0.
Any help would be smashing thanks!

davekw7x

#1
Apr 02, 2011, 10:56 pm Last Edit: Apr 02, 2011, 11:05 pm by davekw7x Reason: 1

... perplexed!..

Step away from Arduino for a minute and think about what you need to do.  Sometimes I actually write it out (on the back of a cocktail napkin at the NAME OF WATERING HOLE DELETED).

Often I write it in pseudo-code rather than my native language (American-English, as it happens.)  The point is that I try to express it in logical terms before I actually write code.

(Instead of scanning the booze-stained napkin from this particular session at the lounge, I'll post my take on it as text):

The loop() function is executed repeatedly:

BEGIN loop function

   IF there is a character available THEN
       Read the character and calculate the "pause" value
   END IF

   Turn on LED
   pause
   Turn off LED
   pause

END loop function



Now if that makes sense, compare with your code.  If it doesn't make sense, ask again.


Regards,

Dave


gravebond

Sound sound advice indeed. Still though i am having trouble. Here's what i tried;
Code: [Select]
#define OPTO 13 // optocoupler pin
int val =0; // variable to store the serial
int pause =0; // variable for our blinks

void setup(){
 
Serial.begin(300);
pinMode (OPTO, OUTPUT);

}

void loop (){



  if (Serial.available() > 0) {
   
int val = Serial.read()-('0'); // only reads the first byte of data
 
int pause = map (val,0,9, 15, 100); // hence maximum of 9

  pause = constrain (pause , 15, 100);
  }

 
 
digitalWrite(OPTO, HIGH);
delay(pause);
digitalWrite (OPTO, LOW);
delay(pause);
 
   Serial.println (pause);

   
}


its still doing the same thing. Halp...

AWOL

It isn't doing the same thing.
You have two "pause" - get rid of the local one.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

gravebond

What do you mean by local one?
If i get rid of my "pause" int i cant map new values onto the single serial character i am getting.

gravebond

i see now its just printing 0 constantly regardless of my typing into the serial moniter

AWOL

#6
Apr 02, 2011, 11:27 pm Last Edit: Apr 02, 2011, 11:30 pm by AWOL Reason: 1
Code: [Select]
pause = map (val,0,9, 15, 100); // hence maximum of 9



You had (always had)two variables called "pause".
Check scope rules.
http://www.arduino.cc/en/Reference/Scope
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

gravebond

Ive now uploaded this and at least the serial moniter is making more sense now. However the Led is still not behaving.
Code: [Select]
#define OPTO 13 // optocoupler pin
int val =0; // variable to store the serial
int pause =0; // variable for our blinks

void setup(){
 
Serial.begin(9600);
pinMode (OPTO, OUTPUT);

}

void loop (){

while (Serial.available() ==0 );

  if (Serial.available() >0 ) {
   
int val = Serial.read()-('0'); // only reads the first byte of data
 
pause = map (val,0,9, 15, 100); // hence maximum of 9

  pause = constrain (pause , 15, 100);
 
  Serial.flush();
  }

 
 
digitalWrite(OPTO, HIGH);
delay(pause);
digitalWrite (OPTO, LOW);
delay(pause);
 
   Serial.println (pause);

   
}


The led just flashes once, do need to make a seperate void and call it with an if?

gravebond

#8
Apr 02, 2011, 11:51 pm Last Edit: Apr 03, 2011, 12:09 am by gravebond Reason: 1
Right ive got it working by adding a do.. while statement.
Code: [Select]
#define OPTO 13 // optocoupler pin
int val =0; // variable to store the serial
int pause =0; // variable for our blinks

void setup(){
 
Serial.begin(9600);
pinMode (OPTO, OUTPUT);

}

void loop (){

while (Serial.available() ==0 );

 if (Serial.available() >0 ) {
   
int val = Serial.read()-('0'); // only reads the first byte of data
 
pause = map (val,0,9, 15, 100); // hence maximum of 9

 pause = constrain (pause , 15, 100);
 
 Serial.flush();
 }

 
 
 
 
 
 do
{
 digitalWrite(OPTO, HIGH);
delay(pause);
digitalWrite (OPTO, LOW);
delay(pause);// check the sensors

} while (Serial.available() ==0 );

 
  Serial.println (pause);

 
}



However i dont think this is what you guys were getting at, so if theres a much more economic way of achieving the same results i would appreciate any insight. There is also some odd behaviour... When i input into the serial monitor, i have to input the number i want twice before the arduino changes the value of "pause". any ideas?

AWOL

#9
Apr 03, 2011, 12:13 am Last Edit: Apr 03, 2011, 12:15 am by AWOL Reason: 1
The economic way was described in reply #1 = I can't see why it wouldn't work.
Why did you reintroduce the "while" loop?
It isn't needed.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

gravebond

#10
Apr 03, 2011, 12:19 am Last Edit: Apr 03, 2011, 12:21 am by gravebond Reason: 1
i couldnt get it to work the way described in reply # 1. I was clutching at straws when i added the while loop, and it happened to work. Although it does look very clumsy. Could you explicitly tell me the code involved to make it work as described in reply #1 as i may have made a silly mistake somewhere when i tried my implementation of it?

AWOL

[uncompiled, untested]
Code: [Select]
#define OPTO 13 // optocoupler pin
int pause; // variable for our blinks

void setup()
{
Serial.begin(9600);
pinMode (OPTO, OUTPUT);
}

void loop ()
{
  if (Serial.available() >0 ) {
    int val = Serial.read(); // only reads the first byte of data
    pause = map (val, '0', '9', 15, 100); // hence maximum of 9
    pause = constrain (pause , 15, 100); // only needed if you don't get '0' to '9'
  }

  digitalWrite(OPTO, HIGH);
  delay(pause);
  digitalWrite (OPTO, LOW);
  delay(pause);// check the sensors
  Serial.println (pause);
}
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

gravebond

RIght that works, and was in fact the second thing i tried, the problem was declaring pause as an int in the map function in the loop. Thanks for the halp!.

PaulS

Code: [Select]
  if (Serial.available() >0 ) {
    int val = Serial.read(); // only reads the first byte of data
    pause = map (val, '0', '9', 15, 100); // hence maximum of 9
    pause = constrain (pause , 15, 100); // only needed if you don't get '0' to '9'
  }

You might want to modify this code to properly deal with invalid input. Suppose the serial port is sent a 'B'. That is not in the range '0' to '9'. As written, the code will cause pause to be assigned either 15 or 100, depending on which invalid character is sent.

I'd prefer to simply ignore any invalid characters. Your mileage may vary.

gravebond

Whats the most economical way of doing this? I dont want to have to make a separate if statement for number 1 -9. Another constrain on the val?

Go Up