Registro de codigos Tkinter + Python + Arduino.

Inicio de programa usando KitLED v1

El primer ejemplo explica estructura básica, identificación de puertos y comunicación serial desde Python, configuramos las salidas para pruebas con KitLED v1.

Codigo Arduino

//identificadores para LED's
int ledPin1 = 6;  // choose the pin for the LED 
int ledPin2 = 7;  // choose the pin for the LED 
int ledPin3 = 8;  // choose the pin for the LED 
int ledPin4 = 9;  // choose the pin for the LED 
int val = 0;      // variable for reading the pin status  
char msg = '  ';   // variable to hold data from serial  
  
  
void setup() {  
  pinMode(ledPin1, OUTPUT);      // declare LED as output 
  pinMode(ledPin2, OUTPUT);      // declare LED as output
  pinMode(ledPin3, OUTPUT);      // declare LED as output 
  pinMode(ledPin4, OUTPUT);      // declare LED as output
  
  Serial.begin(9600);  
  Serial.print("Program Initiated\n");
}  
  
void loop(){ 
        msg = 'x';
    
        // While data is sent over serial assign it to the msg  
    while (Serial.available()>0){   
        msg=Serial.read();  
    }  
  switch (msg){
    
     case 'a':
    {
            digitalWrite(ledPin1, HIGH);  // turn HIGH
            delay (5000);
            digitalWrite(ledPin1, LOW);  // turn HIGH
            Serial.print("L1 Activado\n");
           msg=' '; 
      break;   

      }
      
    case'b':
    {
            digitalWrite(ledPin1, LOW);  // turn HIGH
            Serial.print("L1 Desactivado\n");
           msg=' '; 
         break;
    } 
    
    case'c':
    {
            digitalWrite(ledPin2, HIGH);  // turn LED ON  
            Serial.print("L2 Activado\n");
           msg=' '; 
         break;
    }
    
    case'd':
    {
            digitalWrite(ledPin2, LOW);  // turn LED ON  
            Serial.print("L2 Desactivado\n");
           msg=' '; 
         break;
    }
    
        case 'e':
    {
            digitalWrite(ledPin3, HIGH);  // turn LED ON  
            Serial.print("L3 Activado\n");
           msg=' '; 
      break;   

      }
    case'f':
    {
            digitalWrite(ledPin3, LOW);  // turn LED ON  
            Serial.print("L3 Desactivado\n");
           msg=' '; 
         break;
    } 
    case'g':
    {
            digitalWrite(ledPin4, HIGH);  // turn LED ON  
            Serial.print("L4 Activado\n");
           msg=' '; 
         break;
    }
    case'h':
    {
            digitalWrite(ledPin4, LOW);  // turn LED ON  
            Serial.print("L4 Desactivado\n"); 
            msg=' ';
         break;
    }
  }
}
	

Codigo Python

import serial # import the serial library
import time # import the time library
from Tkinter import * #import Tkinter GUI library 



print 'Connecting...'
arduino = serial.Serial('/dev/ttyACM1', 9600)
time.sleep(3)
print 'Connection established successfully'

def L1On(): # RED
	arduino.write('a')
def L1Off(): # green
	arduino.write('b')
def L2On(): # Green
	arduino.write('c')
def L2Off(): # Red
	arduino.write('d')
def L3On(): # RED
	arduino.write('e')
def L3Off(): # green
	arduino.write('f')
def L4On(): # Green
	arduino.write('g')
def L4Off(): # Red
	arduino.write('h')

def start_timerL1() :
    # Next line should be * 60000, but use 1000 to speed debugging:
   	v0.after(scaleL1.get() * 1000, L1Off)

def start_timerL2() :
    # Next line should be * 60000, but use 1000 to speed debugging:
    	v0.after(scaleL2.get() * 1000, L2Off)

def start_timerL3() :
    # Next line should be * 60000, but use 1000 to speed debugging:
   	v0.after(scaleL1.get() * 1000, L3Off)

def start_timerL4() :
    # Next line should be * 60000, but use 1000 to speed debugging:
    	v0.after(scaleL2.get() * 1000, L4Off)


v0 = Tk() # creates the application window (you can use any name)
v0.wm_title("Control de LX") # displays title at the top left
v0.geometry("580x350")
v0.config(bg ="#037481")

mainFrame = Frame(v0, bg="#037481") #define the main frame of the GUI
mainFrame.grid() #grid the frame into the App Window

btnFrame = Frame(mainFrame, bg="#037481") 
btnFrame.grid() 

control = Label(btnFrame, text="CONTROL  ON:")
control.grid(row=0, column=1)
control = Label(btnFrame, text="   OFF:")
control.grid(row=0, column=2)

minutes = Label(btnFrame, text="TIEMPO EN MINUTOS:")
minutes.grid(row=0, column=4)

# inicio de control L1

L1OnBt = Button(btnFrame, text="L1-On", command=L1On, bg="green") 
L1OnBt.grid(row=2, column=1) 
L1OffBt = Button(btnFrame, text="L1-Off", command=L1Off, bg="red") 
L1OffBt.grid(row=2, column=2) 

scaleL1 = Scale(btnFrame, from_=1, to=45, orient=HORIZONTAL, length=300, bg="yellow")
scaleL1.grid(row=2, column=4)

buttonL1 = Button(btnFrame, text="Start timing", command=start_timerL1, bg="white")
buttonL1.grid(row=2, column=6, pady=5, sticky=E)

# final control L1
# inicio de control L2

L2OnBt = Button(btnFrame, text="L2-On", command=L2On, bg="green") 
L2OnBt.grid(row=3, column=1) 
L2OffBt = Button(btnFrame, text="L2-Off", command=L2Off, bg="red") 
L2OffBt.grid(row=3, column=2)

scaleL2 = Scale(btnFrame, from_=1, to=45, orient=HORIZONTAL, length=300, bg="yellow")
scaleL2.grid(row=3, column=4)

buttonL2 = Button(btnFrame, text="Start timing", command=start_timerL2, bg="white")
buttonL2.grid(row=3, column=6, pady=5, sticky=E)
 
# final control L2
# inicio de control L3

L3OnBt = Button(btnFrame, text="L3-On", command=L3On, bg="green") 
L3OnBt.grid(row=4, column=1) 
L3OffBt = Button(btnFrame, text="L3-Off", command=L3Off, bg="red") 
L3OffBt.grid(row=4, column=2) 

scaleL3 = Scale(btnFrame, from_=1, to=45, orient=HORIZONTAL, length=300, bg="yellow")
scaleL3.grid(row=4, column=4)

buttonL3 = Button(btnFrame, text="Start timing", command=start_timerL3, bg="white")
buttonL3.grid(row=4, column=6, pady=5, sticky=E)
 
# final control L3
# inicio de control L4

L4OnBt = Button(btnFrame, text="L4-On", command=L4On, bg="green") 
L4OnBt.grid(row=5, column=1) 
L4OffBt = Button(btnFrame, text="L4-Off", command=L4Off, bg="red") 
L4OffBt.grid(row=5, column=2) 

scaleL4 = Scale(btnFrame, from_=1, to=45, orient=HORIZONTAL, length=300, bg="yellow")
scaleL4.grid(row=5, column=4)

buttonL4 = Button(btnFrame, text="Start timing", command=start_timerL4, bg="white")
buttonL4.grid(row=5, column=6, pady=5, sticky=E)
 
# final control L4

v0.mainloop()# begins main loop

GUI Tkinter

Basic Connecting

Arduino

Basic Connecting

Video