I am new to Arduino (and computer programming in general), so I apologize if this question looks silly.
Once I set up a basic arduino-LED connection, I have problems sending INTEGERS to arduino through the serial port. I can easily send characters such as 'm', 'o' and so on.. However if I send a number it looks like it simply does not get it.
Here is the Arduino code, ideally it should get a value from the usb port through python or the serial monitor and then adjust the brightness of the LED according to the value. (value must be in range [0,255]).
NOTE: I am using ARDUINO UNO and PYTHON 3
//--------------------------
int LED = 10;
int number;
void setup(){
pinMode(LED,OUTPUT);
Serial.begin(9600);
}
void loop(){
number = Serial.read();
Serial.print(number);
Serial.print('\n');
if(number == -1){
number = 0;
}
else if(number > 255){
number = 255;
}
else if(number < 0){
number = 0;
}
analogWrite(LED,number);
delay(1000);
}
However, when I input a value into the Serial port or through Python, for instance 0, it gives me 48 as answer (which, interestingly, is the ASCII code for 0!) and lights up the LED which is not what should happen since at 0 the LED should be off!! I am missing something but I cannot find what... Could you please tell me what is wrong?
Here is the code I use in Python:
import serial
import time
try:
arduino = serial.Serial(port,speed)
time.sleep(2)
print("Connection to " + port + " established succesfully!\n")
except Exception as e:
print(e)
#Note: for characters such as 'a' I set data = b'a' to convert the data in bytes
#However the same thing does not work with numbers...
data = 0
data = arduino.write(valueToWrite)
time.sleep(2)
arduino.close()
what am I doing wrong or misunderstanding? Thank you.
arduino.write(chr(valueToWrite))