Go Down

Topic: [SOLVED]Serial input affecting a delay (Read 2 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.

Go Up