Go Down

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

AWOL

You could test to see if the character is in the range '0' to '9' with a pair of comparisons.
"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


You could test to see if the character is in the range '0' to '9' with a pair of comparisons.

if( val > =0 && val =< 9) ?

AWOL

Almost.
The characters coming in are ASCII, so:
if( val >= '0' && val =< '9')
"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

if( val >= '0' && val  =<'9'){
  digitalWrite(OPTO, HIGH);
  delay(pause);
  digitalWrite (OPTO, LOW);
  delay(pause);
  Serial.println (pause);
}

gets an error : expected - primary expression before '<' token

AWOL

"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

i think it was a copy and paste thing as well cos when i switched them around it threw up another error, but after retyping it, its compiled. Problem now is , it stops the function if you type in an erroneous char and the led stops its blinking.

gravebond

Tried a couple of else statements that arent working the way i wish.
I basically want a function to say

"Else , ignore and proceed as usual ...."

AWOL

Code: [Select]
void loop ()
{
  if (Serial.available() >0 ) {
    int val = Serial.read(); // only reads the first byte of data
    if (val >= '0' && val <= '9') {
      pause = map (val, '0', '9', 15, 100);
    } else {
    //invalid character, ignite smoke bombs, sound klaxons
    }
  }

  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

Code: [Select]
#define OPTO 13 // optocoupler pin
int pause; // variable for our blinks
//we havnt declared val here as we only want it referenced inside our
//"if (Serial.available() >0 ) {" function.
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
     if (val >= '0' && val <= '9') { // Checks for any eroneous chars
       pause = map (val, '0', '9', 15, 100);
       
       
    } else {     
   Serial.println ("Invalid!"); // prints invalid if you type something other
    }                          // than 0-9
  }

  digitalWrite(OPTO, HIGH);
  delay(pause);
  digitalWrite (OPTO, LOW);
  delay(pause);// check the sensors
  Serial.println (pause);
}


Working. Might i say that you sir are cryptic... hepful yes... cryptic ....yes.
Thanks very much :D

PaulS

Quote
Working. Might i say that you sir are cryptic... hepful yes... cryptic ....yes.

What more do you need than working code? Often, when we post code fragments the idea is the give the poster the information about what command to use, in the expectation that the reference pages will be used to check the exact syntax, as well as learn more about the command.

gravebond


Quote
Working. Might i say that you sir are cryptic... hepful yes... cryptic ....yes.

What more do you need than working code? Often, when we post code fragments the idea is the give the poster the information about what command to use, in the expectation that the reference pages will be used to check the exact syntax, as well as learn more about the command.

Consider me learned! cheers folks

Go Up