Pages: [1]   Go Down
Author Topic: Serail monitor control problem  (Read 479 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

first, code...
Code:
void setup(){
  Serial.begin(9600);
  pinMode(13,OUTPUT);
  digitalWrite(13,LOW);
}

int byteRead = 0;

void loop(){
  if(Serial.available()){
    byteRead = Serial.parseInt();
    if(byteRead>255)digitalWrite(13,HIGH);
    else analogWrite(13,byteRead);
    delay(500);
  }
}

problem:

when i input one value,
or if I input a series of value separated by space and when reading reach the last one
the LED have a second of delay ( someone tell me it is timeout ) before reacting to the last value.

when i put a space or any non digit at the end after the last value,
the LED have a second of delay after reacting to the last value and then turn off.

when i input a digit ( eg. 5 ) during that delay period of time,
the digit "added" to the last value ( eg. 25 ) to become a new value ( here: 25 + 5 = 255 )

So can any one tell me what the problem is and how to solve this
if I want the LED change the brightness at any moment immediately?
Logged

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

Maybe you need to have a byte variable called intRead. Or maybe a float variable called charRead.

Quote
So can any one tell me what the problem is and how to solve this
if I want the LED change the brightness at any moment immediately?
Quit using parseInt().
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe you need to have a byte variable called intRead. Or maybe a float variable called charRead.

Quote
So can any one tell me what the problem is and how to solve this
if I want the LED change the brightness at any moment immediately?
Quit using parseInt().

how?
Logged

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

Quote
how?
Simple, really. Highlight the code, and hit delete.

You need to write your own code that reads one character at a time from the serial port and stores it in an array, adding a NULL after each addition. When the character is the end of number/packet marker, call atoi() with the array, reset index, and NULL the array.
Logged

Pages: [1]   Go Up
Jump to: