I am using a Python3 GUI on my RPi3 to control my DFRobot Bluno Mega 2560 via USB. The Mega is hooked up to a breadboard with 4 LED's and I am trying to control each LED with their own toggle switch. I was able to do this with one LED but for some reason when I try to achieve the same with more than one it doesn't work, I get absolutely no response from the LED's whatsoever. It is something to do with how the arduino is receiving the ASCII code, or I think it may be a problem with how I am implementing tkinter as I have little experience with it. If anyone can tell me what I am doing wrong please let me know.
Here is the Python3 GUI code:
import serial # import the serial library
import time # import the time library
from tkinter import * #import Tkinter GUI library
def toggleRed():
if redBtn.config('relief')[-1] == 'sunken':
redBtn.config(relief="raised")
arduino.write('a'.encode())
else:
redBtn.config(relief="sunken")
arduino.write('b'.encode())
def toggleGreen():
if greenBtn.config('relief')[-1] == 'sunken':
greenBtn.config(relief="raised")
arduino.write('c'.encode())
else:
greenBtn.config(relief="sunken")
arduino.write('d'.encode())
def toggleBlue():
if blueBtn.config('relief')[-1] == 'sunken':
blueBtn.config(relief="raised")
arduino.write('e'.encode())
else:
blueBtn.config(relief="sunken")
arduino.write('f'.encode())
def toggleYellow():
if yellowBtn.config('relief')[-1] == 'sunken':
yellowBtn.config(relief="raised")
arduino.write('g'.encode())
else:
yellowBtn.config(relief="sunken")
arduino.write('h'.encode())
print ('Connecting...')
arduino = serial.Serial("/dev/ttyACM0", 9600)
time.sleep(3)
print ('Connection established successfully')
appWindow = Tk() # creates the application window (you can use any name)
appWindow.wm_title("RGB LED Control") # displays title at the top left
appWindow.config(bg ="#037481")
mainFrame = Frame(appWindow, bg="#037481") #define the main frame of the GUI
mainFrame.grid() #grid the frame into the App Window
btnFrame = Frame(mainFrame, bg="#037481") #define the frame within the main frame that holds buttons
btnFrame.grid() #grid the frame into the main frame
redBtn = Button(btnFrame, text="RED", width=12, bg="red", relief="raised", command=toggleRed)
redBtn.grid(column=0,row=0,sticky='EW')
greenBtn = Button(btnFrame, text="GREEN", width=12, bg="green", relief="raised", command=toggleGreen)
greenBtn.grid(column=0,row=1,sticky='EW')
blueBtn = Button(btnFrame, text="BLUE", width=12, bg="blue", relief="raised", command=toggleBlue)
blueBtn.grid(column=0,row=2,sticky='EW')
yellowBtn = Button(btnFrame, text="YELLOW", width=12, bg="yellow", relief="raised", command=toggleYellow)
yellowBtn.grid(column=0,row=3,sticky='EW')
appWindow.mainloop()# begins main loop
Here is the arduino code:
int ledRed = 13;
int ledGreen = 11;
int ledBlue = 9;
int ledYellow = 7;
//int ledRedStatus;
//int ledGreenStatus;
//int ledBlueStatus;
//int ledYellowStatus;
void setup() {
pinMode(ledRed, OUTPUT);
pinMode(ledGreen, OUTPUT);
pinMode(ledBlue, OUTPUT);
pinMode(ledYellow, OUTPUT);
Serial.begin(9600);
}
void loop() {
if(Serial.available() == '97') {
digitalWrite(ledRed, LOW);
}
if(Serial.available() == '98') {
digitalWrite(ledRed, HIGH);
}
if(Serial.available() == '99') {
digitalWrite(ledGreen, LOW);
}
if(Serial.available() == '100') {
digitalWrite(ledGreen, HIGH);
}
if(Serial.available() == '101') {
digitalWrite(ledBlue, LOW);
}
if(Serial.available() == '102') {
digitalWrite(ledBlue, HIGH);
}
if(Serial.available() == '103') {
digitalWrite(ledYellow, LOW);
}
if(Serial.available() == '104') {
digitalWrite(ledYellow, HIGH);
}
}