Hello everyone,
I'm trying to figure out a way to change a variable in my arduino code after the code has already been uploaded to the board using a keyboard. Any advice would be greatly appreciated. I'm guessing I will need to use some serial communication, but how would I collect keyboard data?
Thanks!
By "keyboard" do you mean a wired keypad... or do you mean from an attached PC?
Would be a good idea to post your code, then we can see how to help.
Slightly modified:
const byte ledPin = 13; // the pin that the LED is attached to
byte incomingByte; // a variable to read incoming serial data into
booelan ledState = false;
void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
// see if there's incoming serial data:
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
// if it's a capital H (ASCII 72), turn on the LED:
if (incomingByte == 'H') {
ledState = HIGH;
}
// if it's an L (ASCII 76) turn off the LED:
if (incomingByte == 'L') {
ledState = LOW;
}
digitalWrite(ledPin, ledState);
}
}
So, instead of entering 'H' or 'L', you want to be able enter, say "456" and assign that value to a variable in your program?
You're a good way there - you need to parse the number a digit at a time. There are plenty of examples around.
[EDIT] Sorry, that came out wrong.
Lookup the function "atoi ()" - couple of things to watch out for; it works on strings, so you've got to build the string yourself a character at a time, and don't forget to terminate the string with a zero (which means your buffer has to be longer than the string you expect)
Okay I think i've figured out my own answer. I need to write a separate program, in python or C (or whatever) to send the serial data through the usb port. My arduino board would have a serial input check waiting for the incoming data...
That would be a good way of doing it if your Arduino is connected to yor Pc anyway.
I need to write a separate program,
You could just use the send box in the Arduino's monitor box to send stuff.