I'm trying to make a tuner. 3 leds are connected to the Arduino Uno board, each one corresponding to one situation (too high, low or correct height). Python handles the detecting of a note according to the mic input, and determines which led should be turned on, but as it's a live tuner, Python sends the information pretty fast. I've figured out how to turn a led on one at a time and keep it on as long as the input doesn't change, but it only works at a low speed when I do it manually, with a Python program like this :
code=1
while code!=0:
code=input("Code?")
ser.write(code.encode('utf-8'))
If I try it with a Python code such as :
code= '5'
while 1:
ser.write(code.encode('utf-8'))
then the LEDs just stay off.
Here is my Arduino Code :
int hauteur = 0;
void setup() {
pinMode (2, OUTPUT); //red pin
pinMode (6, OUTPUT); //green pin
pinMode (5, OUTPUT); //yellow pin
Serial.begin(9600);
}
void loop() {
if(Serial.available ()) {
hauteur = Serial.parseInt();
while (hauteur==2) {
(digitalWrite(2,HIGH));
if (Serial.available ()) {
hauteur=Serial.parseInt();
}
}
digitalWrite(2,LOW);
while (hauteur==5){
(digitalWrite(5,HIGH));
if (Serial.available ()) {
hauteur=Serial.parseInt();
}
}
digitalWrite(5,LOW);
while (hauteur==6){
(digitalWrite(6,HIGH));
if (Serial.available ()) {
hauteur=Serial.parseInt();
}
}
digitalWrite(6,LOW);
Serial.flush();
}
}
If 2, 5 or 6 are sent by Python with enough time in between them, then the board does what it's supposed to do and turns on the according LED. However sent too fast nothing happens anymore. How can I reduce that necessary lapse of time to get a real time change of lights according to what's sent by Python?