I have a simple raspberry Pi program that receives user input, either 1 or 0, that turns on or off pin13 on Arduino (taken from here). However, I can't print the messages in the serial monitor. I've tried in the receiveEvent() function and loop(). Why is this?
Raspberry Pi:
from smbus import SMBus
addr = 0x8
bus = SMBus(1) #indicates /dev/i2c-1
num = 1
print("enter 1 for on or 0 for off")
while num == 1:
state = input(">>>> ")
if state == "1":
bus.write_byte(addr, 0x1)
elif state == "0":
bus.write_byte(addr, 0x0)
else:
num = 0
Arduino:
#include <Wire.h>
char new_data;
char old_data;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(13, OUTPUT);
Wire.begin(0x8);
Wire.onReceive(receiveEvent);
}
void receiveEvent(int howMany) {
while(Wire.available()) {
old_data = new_data;
new_data = Wire.read();
digitalWrite(13,new_data);
Serial.print(new_data);
}
}
void loop() {
// put your main code here, to run repeatedly:
delay(100);
if(old_data!=new_data){
Serial.print(new_data);
}
}