Pages: 1 [2]   Go Down
Author Topic: [SOLVED]Serial input affecting a delay  (Read 1795 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25730
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"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.

UK
Offline Offline
Newbie
*
Karma: 0
Posts: 31
wewt
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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) ?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25730
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Almost.
The characters coming in are ASCII, so:
if( val >= '0' && val =< '9')
Logged

"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.

UK
Offline Offline
Newbie
*
Karma: 0
Posts: 31
wewt
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25730
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

<=

(How embarrassing)
Logged

"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.

UK
Offline Offline
Newbie
*
Karma: 0
Posts: 31
wewt
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Newbie
*
Karma: 0
Posts: 31
wewt
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ...."
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25730
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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);
}
 
Logged

"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.

UK
Offline Offline
Newbie
*
Karma: 0
Posts: 31
wewt
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#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 smiley-grin
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Newbie
*
Karma: 0
Posts: 31
wewt
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: 1 [2]   Go Up
Jump to: