I have a arduino that displays LCD text based on what is sent serially from python code.
On the python code, I presently have a captured parsed string using python. right now the string is simply written to LCD.
My question if the parsed string contains either 1, 2, or 3 etc. I would like to have the LED blink at different rates, what is the best way to set this string to variable,
on Python or Arduino
Do you have a general code syntax that you can point me to on how to set string to int variable and then doing a comparison on the integer?
edit found my own answer :
>>> a = "545.2222"
>>> float(a)
545.22220000000004
>>> int(float(a))
545
now how do I pass this variable to arduino, or is the best to parse the string in arduino into a variable? as I am simply now only using the serial on the arduino to capture text to display on screen.
The arduino's Serial.read() function only reads letters (chars), so you'll have to convert it to a number on the arduino. A common way of doing it is by taking the output of Serial.read() and subtracting '0' (notice the single quotes). What this does is take the ASCII value of your character, say '5' and subtracts the ASCII value of '0', giving you a number from 0-9.
hi, I am now bit confused with your comment regarding ascii. Are you saying that the native text of "abc" i pass in python to arduino thru serial is actually not in human readable text but ascii? I am now monitoring my serial on my arduino and the text showing is all human readable as I passed it from python?
In my case, I am passing "%change: xxx.xx%" where the xxx.xx can change over time. Can i parse the serial text as is and convert xxx.xx to variable or do i need to seek the ascii equivelent?
can you do a quick example code on arduino to guide me in the right direction?
if(inChar == ':') // Now, read the number
{
index = 0;
while(Serial.available() > 0 && index < 10)
{
inDigit = Serial.read() - '0'; Convert '3' to 3, for instance
inNumber *= 10; // Multiply existing number by 10
inNumber += inDigit; // Add the new digit
index++; // Don't try to read too many digits
}
}
but how do I adapt the above code for floating points?
There are two ways to do it. Read the value that way, until the decimal point arrives. Then, read, and store in another variable, the rest of the number (the fractional part). Then, divide the fractional part by ten for each character read (not just until the value is less than 1), as a float, then add the whole part.
Below are ways to convert a character/string to an int for use.
int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the data from the serial port
void setup() {
pinMode(ledPin,OUTPUT); // declare the LED's pin as output
Serial.begin(9600); // connect to the serial port
Serial.println("Blink number 1-9"); // so I can keep track
}
void loop () {
val = Serial.read(); // read the serial port
// if the stored value is a single-digit number, blink the LED that number
if (val > '0' && val <= '9' ) {
val = val - '0'; // convert from character to number
for(int i=0; i<val; i++) {
Serial.print("blink! ");
Serial.println(i);
digitalWrite(ledPin,HIGH);
delay(150);
digitalWrite(ledPin, LOW);
delay(150);
}
//Serial.println();
}
}
if (readString.length() >0) {
Serial.println(readString); //see what was received
// expect a string like 07002100 containing the two servo positions
servo1 = readString.substring(0, 4); //get the first four characters
servo2 = readString.substring(4, 8); //get the next four characters
Serial.println(servo1); //print to serial monitor to see results
Serial.println(servo2);
int n1 = servo1.toInt();
int n2 = servo2.toInt();
myservo1.writeMicroseconds(n1); //set servo position
myservo2.writeMicroseconds(n2);
readString="";
}