Go Down

Topic: blinking led with serial (Read 435 times) previous topic - next topic

Feb 10, 2013, 09:17 pm Last Edit: Feb 10, 2013, 09:25 pm by oliverrose99 Reason: 1
hi

I recently got an Arduino uno and are staring with the basics. I want to control the blinking rate of a led using serial monitor.
it works between 1 and 9 but not anything higher. what am i doing wrong

Code: [Select]
const int led = 13;
int ledSpeed = 0;

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

void loop() {

if (Serial.available() > 0) {
 ledSpeed = Serial.read();
}

 digitalWrite(led, HIGH);
 delay(ledSpeed);
 digitalWrite(led, LOW);
 delay(ledSpeed);

}

AWOL

Quote
what am i dooing wrong
Assuming that you're handling multi digit numbers correctly?
"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.

Nick Gammon

Better read this about getting multiple bytes in as serial data:

http://www.gammon.com.au/serial
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

AWOL can you explain what you mean

AWOL

Sure.
Image you enter "18".
What do you think your sketch does?
"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.

i think it only uses the first digit because if i put in 111111 the delay will be 1 and if i put in 99999 the delay will be 9 and i need this fixing

AWOL

Quote
i think it only uses the first digit
I know it only uses the last digit.
How could it know otherwise?
"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.

could i use a unsigned int instead

Arrch


could i use a unsigned int instead


If you want, but I don't see how that will make a difference. The flaw in that you are under the impression that sending a value of say "1000" through the serial monitor will be received as 1000 on the other end. In actuality, your Arduino will receive '1', '0', '0', and '0'. So with your code, it will receive '1', and blink the LED with a delay of 49 mS (the decimal value of ASCII '1'). It will then repeat the process 3 more times using 48 (the decimal value of ASCII '0'). If you have any line ending selected, it will do it for those values.

What you are looking for is a state machine that will accept the individual characters and put them into an array. If you receive the stop byte, such as a new line, then feed the null-terminated array into atoi() to get the decimal value and use that in your delay Here is a simple example:

Code: [Select]
const char stopByte = '\n';

// Maximum characters in an int + null terminated character
const byte maxBuffer = 6;

void setup() {
  Serial.begin(115200);
  Serial.println("[Serial2Int]");
}

void loop() {
  // Stores the characters between the start and stop bytes
  static char buffer[maxBuffer];
  // Keeps track of spot in buffer
  static byte index=0;
 
  if (Serial.available() > 0 ) {
    char inChar = Serial.read();
   
    if (inChar==stopByte)  // If stop byte is received
    {
      processData(buffer); // process the data
      index=0; // reset the array
      buffer[0] = '\0';
    }
    else
    {
      buffer[index] = inChar; // put the character into our array
      index++; // and move to the next key in the array
      buffer[index] = '\0'; // then null terminate
    }
  }
}

void processData(char buffer[]) {
  unsigned int value = atoi(buffer); // convert string to int
  Serial.print("Value: ");
  Serial.println(value);
}

ok thanks alot for that. ime very new to this

Go Up