Python Tkinter Slider Widget write Value over I2C -
ok have been searching example while can not seem find examples of relaying tkinter slider values on i2c arduino. far have not tried communicate arduino yet. ill cross bridge next; want figure out how write slide widget values , send on i2c.
here simple gui slider widget in python 2 believe correct setup i2c communications. ive updated rpi set i2c well. want in arduino read values 0 180 servo control. important writes values or in way can used input. have other code in arduino drives same servo, if other conditions met , ignored.
from tkinter import* import rpi.gpio gpio import time import smbus bus = smbus.smbus=(1) slave_address = 0x04 class app: def __init__(self, master): def sendscalereading(self): s = scale.get()# how write , scale value , send it?? bus(slave_address, ord('s'))#according example should #"bus.write_byte(slave_address, ord('s'))" frame = frame(master) frame.pack() scale = scale(frame, from_=0, to=180, orient=horizontal, command=sendscalereading) scale.grid(row=1, column=1) root = tk() root.wm_title('i2c servo control') app = app(root) root.geometry("200x50+0+0") root.mainloop()
ok friend helped me out , wasnt far off. once got far small issue io error. getting [errno 5] io errors. make sure have ground connected between arduino , pi. when searched seemed overlooked many fixes out there offered. need sda, sla , gnd connected.
so anyways here code working running arduino i2c blink sketch. blink led on pin 13 faster or slower based on slider input in rpi on i2c. going try , write code control servo next , if successful post code.
rpi / python2 code below:
from tkinter import* import rpi.gpio gpio import time import smbus bus = smbus.smbus(1) slave_address = 0x28 class app: def __init__(self, master): def sendscalereading(self): s = scale.get() print("we have" ); print( s ) bus.write_byte_data(slave_address, s, s ) frame = frame(master) frame.pack() scale = scale(frame, from_=0, to=180, orient=horizontal, command=sendscalereading) scale.grid(row=1, column=1) root = tk() root.wm_title('i2c servo control') app = app(root) root.geometry("200x50+0+0") root.mainloop()
arduino i2c wire blink sketch below:
#include <wire.h> // unique address i2c slave device #define address 0x28 // constants won't change. used here // set pin numbers: const int ledpin = 13; // number of led pin // variables change: int ledstate = low; // ledstate used set led long previousmillis = 0; // store last time led updated // follow variables long because time, measured in miliseconds, // become bigger number can stored in int. volatile long interval = 1000; // interval @ blink (milliseconds) void setup() { // set digital pin output: pinmode(ledpin, output); wire.begin(address); // join i2c bus address #4 wire.onreceive(receiveevent); // register event wire.onrequest(requestevent); // register request handler } //gets called when i2c read occurs void requestevent() { //any request data return 0x14 (random number picked testing) wire.write( 0x14 ); } // called when i2c write occurs void receiveevent(int howmany) { //just going support 1 byte commands if (howmany >0 ) { int c = wire.read(); interval = c * 10; } while (wire.available() > 0 ) { wire.read(); } } void loop() { // here you'd put code needs running time. // check see if it's time blink led; is, if // difference between current time , last time blinked // led bigger interval @ want // blink led. unsigned long currentmillis = millis(); if(currentmillis - previousmillis > interval) { // save last time blinked led previousmillis = currentmillis; // if led off turn on , vice-versa: if (ledstate == low) ledstate = high; else ledstate = low; // set led ledstate of variable: digitalwrite(ledpin, ledstate); } }
Comments
Post a Comment