1

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);
  }
}
1
  • 1
    I think that code won't compile; it should get error messages about invalid character constants like '97' ... '104'. Either take away the single-quote marks so that you match against numbers (representing ASCII characters), or, better, substitute valid character constants like 'a', 'b', ... Also, as noted in answer, use Serial.read() to get actual data instead of number of buffered characters Jan 16, 2017 at 4:51

2 Answers 2

1

Adding comments to your code would help us understand your intentions.

It is assumed you are sending the ASCII values for 'a', 'b', 'c', ... for different buttons and their actions. And that you want to receive these on an Arduino over the USB using the USB serial port protocol. It is also assumed that your Python script works and the Linux OS is creating a /dev/ttyACM0 or some such serial device when you plug in your Arduino.

Looking at your Arduino code we see you are only checking for the number of bytes in the Arduino's serial port receive buffer by using the Serial.available() method call. If your intentions were to read a byte of data you would instead call the serial.read() method.

It is suggested that you check if there is any data available and if there is you would read it once and save it to a variable. Then you can check this variable many times and decide what action to take based on its value. If you read the buffer many times you would be pulling a new byte of data from the buffer with each read. If you run out of data to read you will be supplied with the value of -1 as an indication there is no more data.

0
  if(Serial.available() == '97') 

Make sure the comparison is actually valid and Serial.available() does what you think it does. If you wish to read a value from serial port, .available() doesn't seem to be the right function for that.

1
  • I think the author of this answer asked this question to direct attention to the validity of the comparison not to get an answer.
    – Avamander
    Apr 10, 2017 at 23:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.