Skip to main content
Arduino

Return to Answer

replaced http://arduino.stackexchange.com/ with https://arduino.stackexchange.com/
Source Link

No, your Mac doesn't have I2C support. It was pointed out in the comments in the comments that there might be some legacy port, but it won't be usable due to driver limitations.

No, your Mac doesn't have I2C support. It was pointed out in the comments that there might be some legacy port, but it won't be usable due to driver limitations.

No, your Mac doesn't have I2C support. It was pointed out in the comments that there might be some legacy port, but it won't be usable due to driver limitations.

replaced http://electronics.stackexchange.com/ with https://electronics.stackexchange.com/
Source Link

As far as debugging, there's a few options. First of all, there are a few ways to debug I2C using an oscilloscope there are a few ways to debug I2C using an oscilloscope. If that's too expensive, you could also get away with a logic analyzer. The one I just linked was at Sparkfun for 150ドル. If that's still too expensive and you're willing to risk 30,ドル you can find cheap ones on eBay (most with little/no documenation).

As far as debugging, there's a few options. First of all, there are a few ways to debug I2C using an oscilloscope. If that's too expensive, you could also get away with a logic analyzer. The one I just linked was at Sparkfun for 150ドル. If that's still too expensive and you're willing to risk 30,ドル you can find cheap ones on eBay (most with little/no documenation).

As far as debugging, there's a few options. First of all, there are a few ways to debug I2C using an oscilloscope. If that's too expensive, you could also get away with a logic analyzer. The one I just linked was at Sparkfun for 150ドル. If that's still too expensive and you're willing to risk 30,ドル you can find cheap ones on eBay (most with little/no documenation).

added 1759 characters in body
Source Link
Anonymous Penguin
  • 6.4k
  • 10
  • 34
  • 62

Edit: this seems like the Arduino code you want:

#include <Wire.h>
#define SLAVE_ADDRESS 0x04
int number = 0;
int num1 = 0;
int num2 = 0;
int state = 0;
boolean secondByte = false;
void setup() {
 pinMode(13, OUTPUT);
 Serial.begin(9600); // start serial for output
 // initialize i2c as slave
 Wire.begin(SLAVE_ADDRESS);
 // define callbacks for i2c communication
 Wire.onReceive(receiveData);
 Wire.onRequest(sendData);
 Serial.println("Ready!");
}
void loop() {
 delay(100);
}
// callback for received data
void receiveData(int byteCount){
 while(Wire.available()) {
 if(secondByte) {
 num2 = Wire.read();
 number = (num1 << 8) + num2;
 Serial.print("data received: ");
 Serial.println(number);
 
 if (number == 1){
 if (state == 0){
 digitalWrite(13, HIGH); // set the LED on
 state = 1;
 }
 else{
 digitalWrite(13, LOW); // set the LED off
 state = 0;
 }
 }
 } else {
 num1 = Wire.read();
 }
 secondByte != secondByte;
 }
}
// callback for sending data
void sendData(){
 Wire.write(num1);
 Wire.write(num2);
}

The Python code will have to do the similar thing. Note: the first part of the number (reading left to right) will be sent first.

Edit: this seems like the Arduino code you want:

#include <Wire.h>
#define SLAVE_ADDRESS 0x04
int number = 0;
int num1 = 0;
int num2 = 0;
int state = 0;
boolean secondByte = false;
void setup() {
 pinMode(13, OUTPUT);
 Serial.begin(9600); // start serial for output
 // initialize i2c as slave
 Wire.begin(SLAVE_ADDRESS);
 // define callbacks for i2c communication
 Wire.onReceive(receiveData);
 Wire.onRequest(sendData);
 Serial.println("Ready!");
}
void loop() {
 delay(100);
}
// callback for received data
void receiveData(int byteCount){
 while(Wire.available()) {
 if(secondByte) {
 num2 = Wire.read();
 number = (num1 << 8) + num2;
 Serial.print("data received: ");
 Serial.println(number);
 
 if (number == 1){
 if (state == 0){
 digitalWrite(13, HIGH); // set the LED on
 state = 1;
 }
 else{
 digitalWrite(13, LOW); // set the LED off
 state = 0;
 }
 }
 } else {
 num1 = Wire.read();
 }
 secondByte != secondByte;
 }
}
// callback for sending data
void sendData(){
 Wire.write(num1);
 Wire.write(num2);
}

The Python code will have to do the similar thing. Note: the first part of the number (reading left to right) will be sent first.

Source Link
Anonymous Penguin
  • 6.4k
  • 10
  • 34
  • 62
Loading

AltStyle によって変換されたページ (->オリジナル) /