Go Down

Topic: Code for Python GUI (tkinter) to control output pins on Arduino UNO (Read 115 times) previous topic - next topic

woodcncwnc

Program is partial working. I am able to turn pin 13 on and off.  What I am trying to do is have "true" feed back when the pin changes state. I am trying to send pin 13 status ("1" or "0") to a tkinter lable widget in python (below the "Exit" buttton) If I run using serial monitor everthing works correctly. I get the text "Hello" and when I pus the on button I get a "1" (no quotes) and when I push the off button I get a "0" (no quotes). Pushing Exit quits the program. I believe this is a python problem, I will also post there. Thanks for you help in advance

Jim



Python 3.6

#! /usr/bin/env python

import serial
import time
from tkinter import *

ArduinoSerial = serial.Serial('com3', 9600, timeout=.1)
time.sleep(2)

status1 = 0


def led_on():
    global status1
    ArduinoSerial.write(b'1')
    status1 = (ArduinoSerial.readline() ) # .decode('utf-8').strip())

def led_off():
    global status1
    ArduinoSerial.write(b'0')
    status1 = (ArduinoSerial.readline() ) # .decode('utf-8').strip())

def led_Exit():
    global status1
    ArduinoSerial.write(b'0')
    status1 = "Quit"
    quit()

root = Tk()
root.title("Arduino Push Buttons")
btn1 = Button(root, text="Led on", command=led_on)
btn2 = Button(root, text="Led off", command=led_off)
btn3 = Button(root, text="Exit", command=led_Exit)
msg1 = Label (root, textvariable = status1, relief=RAISED,width = 10)
btn1.pack()
btn2.pack()
btn3.pack()
msg1.pack()
root.mainloop()

:::::::::::::::::::::::::::::::::::::::::::::

Arduino 1.6.12

int data;

void setup()
{
  Serial.begin(9600); //initialize serial COM at 9600 baudrate
  pinMode(13, OUTPUT); //make the LED pin (13) as output
  digitalWrite (13, LOW);
  Serial.println("Hello");
}

void loop()
{
  if (Serial.available()> 0)
  {
    data = Serial.read();
  }
   if (data == '1')
    {
    digitalWrite (13, HIGH);
    Serial.println(digitalRead(13));
    data = '2';
    }
   else if (data == '0')
    {
    digitalWrite (13, LOW);
    Serial.println(digitalRead(13));
    data = '2';
    }
}



Robin2

This Python - Arduino demo and this Python GUI demo may be of interest.

They may need a little modification for Python 3 as it was written with 2.7

...R

Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up