Python Tkinter Slider Widget通过I2C写入值

时间:2017-07-26 03:27:13

标签: python slider i2c

好的我一直在寻找一个例子现在似乎无法找到通过I2C将tkinter滑块值中继到Arduino的任何示例。到目前为止,我还没有尝试与Arduino进行通信。我越过那座桥接下来;现在我只想弄清楚如何编写幻灯片小部件值并通过I2C发送。

这是Python 2中的一个简单的GUI滑块小部件,我相信它是正确的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()  

2 个答案:

答案 0 :(得分: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);
  }
}

答案 1 :(得分: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);

}