2017-07-26 73 views
0

好的我一直在尋找一個例子,現在似乎找不到任何通過I2C向Arduino傳遞tkinter滑塊值的例子。到目前爲止,我還沒有嘗試與Arduino進行溝通。我接下來穿過那座橋;現在我只想弄清楚如何編寫幻燈片widget值並通過I2C發送。Python Tkinter Slider Widget通過I2C寫入數值

下面是Python 2中的一個簡單的GUI slider部件,我相信這是I2C通信的正確設置。我更新了Rpi以建立I2C。我想在Arduino中執行的操作只是讀取伺服控制的值0到180。重要的是它只是寫入值或以某種方式可以用於輸入。如果其他條件得到滿足,我會在驅動相同伺服器的arduino中有其他代碼,然後這些代碼將被忽略。

from Tkinter import* 
import RPi.GPIO as GPIO 
import time 

import smbus 

bus = smbus.SMBus=(1) 

SLAVE_ADDRESS = 0x04 



class App: 



    def __init__(self, master): 

     def SendScaleReading(self): 
      S = scale.get()# Now how do we write this and get the Scale Value and send it??  
      bus(SLAVE_ADDRESS, ord('S'))#According to an example this should be 
             #"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() 

回答

0

好吧,一個朋友幫助了我,並沒有太多太遠。一旦我得到這一點,我只有一個IO錯誤的小問題。我得到[Errno 5] IO錯誤。確保Arduino和Pi之間有接地。當我搜索這似乎被許多修補程序提供的忽視。您需要SDA,SLA和Gnd全部連接。

因此,無論如何這裏是工作運行arduino i2c眨眼草圖的代碼。這將根據I2C上Rpi的滑動輸入,將引腳13上的LED指示燈快速或慢速閃爍。我將嘗試編寫這段代碼來控制下一個伺服器,如果成功,我還會發布該代碼。

的Rpi/python2以下代碼:

from Tkinter import* 
import RPi.GPIO as 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線眨下面素描:

#include <Wire.h> 
// unique address for this I2C slave device 
#define ADDRESS 0x28 


// constants won't change. Used here to 
// set pin numbers: 
const int ledPin = 13;  // the number of the LED pin 

// Variables will change: 
int ledState = LOW;    // ledState used to set the LED 
long previousMillis = 0;  // will store last time LED was updated 

// the follow variables is a long because the time, measured in miliseconds, 
// will quickly become a bigger number than can be stored in an int. 
volatile long interval = 1000;   // interval at which to blink (milliseconds) 

void setup() { 
    // set the digital pin as output: 
    pinMode(ledPin, OUTPUT);  
    Wire.begin(ADDRESS);    // join i2c bus with address #4 

    Wire.onReceive(receiveEvent); // register event 
    Wire.onRequest(requestEvent); // register the request handler 


} 

//gets called when I2C read occurs 
void requestEvent() { 
//any request for data will return 0x14 (random number i picked for testing) 
    Wire.write(0x14 ); 
} 

// get called when I2C write occurs 
void receiveEvent(int howMany) { 
//just going to support 1 byte commands for now 
    if (howMany >0 ) { 
    int c = Wire.read(); 
    interval = c * 10; 
    } 
    while (Wire.available() > 0) { 
    Wire.read(); 
    } 
} 


void loop() 
{ 
    // here is where you'd put code that needs to be running all the time. 

    // check to see if it's time to blink the LED; that is, if the 
    // difference between the current time and last time you blinked 
    // the LED is bigger than the interval at which you want to 
    // blink the LED. 
    unsigned long currentMillis = millis(); 

    if(currentMillis - previousMillis > interval) { 
    // save the last time you blinked the LED 
    previousMillis = currentMillis; 

    // if the LED is off turn it on and vice-versa: 
    if (ledState == LOW) 
     ledState = HIGH; 
    else 
     ledState = LOW; 

    // set the LED with the ledState of the variable: 
    digitalWrite(ledPin, ledState); 
    } 
} 
0

玉以及伺服碼結束了是相當簡單的。我確定它可以清理乾淨,所以任何知道如何的人都可以隨時進行改進。

Arduino的I2C驅動伺服草圖低於:

#include <Wire.h> 
#include <Servo.h> 
// unique address for this I2C slave device 
#define ADDRESS 0x28 

Servo myservo; 


int pos = 0; 
void setup() { 

myservo.attach(9); 

    Wire.begin(ADDRESS);    // join i2c bus with address #4 

    Wire.onReceive(receiveEvent); // register event 
    Wire.onRequest(requestEvent); // register the request handler 


} 

//gets called when I2C read occurs 
void requestEvent() { 
//any request for data will return 0x14 (random number i picked for testing) 
    Wire.write(0x14 ); 
} 

// get called when I2C write occurs 
void receiveEvent(int Pos) { 

    int val = Wire.read(); 
    pos = val; 

    while (Wire.available() > 0) { 
    Wire.read(); 
    } 
} 


void loop() 
{ 

myservo.write(pos); 
delay(15); 

}