So I am trying to create a simple program that allows me to control the colors of a RGB LED with my computer. I created a little window with tkinter on python 3 in order to control the color, but the problem is that when I try to change the color it simply doesn't respond. I have no idea what is going on. I tried to put the string in the arduino code and it worked out, but it simply doesn't respond when I send through a serial communication.
Arduino code
//pin layout
int red = 12;
int green = 11;
int blue = 10;
//string that will receive
String data;
String subData;
//Color values
int value[3];
void setup() {
Serial.begin(9600);
pinMode(red,OUTPUT);
pinMode(green,OUTPUT);
pinMode(blue,OUTPUT);
}
void loop() {
while(Serial.available() == 0);
data = Serial.readString();
int initialVal =0;
int val;
int pos = 0;
do{
val = data.indexOf(',',initialVal);
subData = data.substring(initialVal,val);
value[pos] = subData.toInt();
pos = pos + 1;
initialVal = val + 1;
}while(val != -1);
Serial.println(data);
analogWrite(red,value[0]);
analogWrite(green,value[1]);
analogWrite(blue,value[2]);
}
And here is the python code:
from tkinter import *
from serial import *
window = Tk()
#all definitions for the window
window.title("RGB LED control Panel")
window.geometry("300x180")
window.resizable(False,False)
Title = Label(window, text = "RGB control", width = 15)
Title.grid(row = 0, column = 0, columnspan = 3)
Explanation = Label(window, text = " This window controls the \ncolor of an RGB LED. Have \n fun!!!")
Explanation.grid(row =1 , column = 3)
RedTitle = Label(window, text = "Red", width = 5, bg = "Red")
RedTitle.grid(row = 1, column = 0)
GreenTitle = Label(window, text = "Green", width = 5, bg = "Green")
GreenTitle.grid(row = 1, column = 1)
BlueTitle = Label(window, text = "Blue", width = 5, bg = "Blue")
BlueTitle.grid(row = 1, column = 2)
RedScale = Scale(window, from_ = 0, to = 255, orient = VERTICAL)
RedScale.grid(row = 2, column = 0)
GreenScale = Scale(window, from_ = 0, to = 255, orient = VERTICAL)
GreenScale.grid(row = 2, column = 1)
BlueScale = Scale(window, from_ = 0, to = 255, orient = VERTICAL)
BlueScale.grid(row = 2, column = 2)
#now the serial com with the arduino
arduino = Serial()
arduino.baudrate = 9600
arduino.port = "COM3"
arduino.open()
while 1:
window.update_idletasks()
window.update()
RED = str(RedScale.get())
GREEN = str(GreenScale.get())
BLUE = str(BlueScale.get())
finalString = RED + "," + GREEN + "," + BLUE
arduino.write(finalString.encode("utf-8"))
print(finalString)
print("\n")
Update
So changing the arduino code (at the part that receives the string) for this:
while(Serial.available() == 0);
data = Serial.readStringUntil('\n');
Serial.setTimeout(0.01);
And the part of the python code which sends the string to this: while 1: window.update_idletasks() window.update()
RED = str(RedScale.get())
GREEN = str(GreenScale.get())
BLUE = str(BlueScale.get())
finalString = RED + "," + GREEN + "," + BLUE + "\n"
if lastMsg != finalString:
finalString= finalString.encode("utf-8")
arduino.write(finalString)
lastMsg = finalString
print(finalString)
The LED changes it's color, but it, sometimes, changes to other colors and the the python program crashes!!!! Is there anything missing in Serial.readStringUntil("\n") or in the arduino.write(finalString)?