Hi there,
i'm trying to read the analog inputs A0, A1, A2 of my Arduino mega (as slave) via an raspberry pi (master) via an I2C bus.
My questions is whether it's possible to read more than 1 value from 1 slave or whether the other analog inputs need to be defined seperately in order to get a reading from them. Right now it returns the value of the A0 Channel 3 times instead of the readings from A0, A1, A2.
#include <Wire.h>
int SLAVE_ADDRESS = 0x08;
int analogPin1 = A0;
int analogPin2 = A1;
int analogPin3 = A2;
void setup(){
Wire.begin(SLAVE_ADDRESS);
Wire.onRequest(sendAnalogReading);
}
void loop(){
}
void sendAnalogReading(){
int reading1 = analogRead(analogPin1);
int reading2 = analogRead(analogPin2);
int reading3 = analogRead(analogPin3);
Wire.write(reading1);
Wire.write(reading2);
Wire.write(reading3);
}
Code Raspberry:
import smbus
import time
bus = smbus.SMBus(1)
SLAVE_ADDRESS = 0x08
def request_reading():
reading1 = int(bus.read_byte(SLAVE_ADDRESS))
print(reading1)
reading2 = int(bus.read_byte(SLAVE_ADDRESS))
print(reading2)
reading3 = int(bus.read_byte(SLAVE_ADDRESS))
print(reading3)
while True:
command = input("Enter command: r - read A0, A1, A2 ")
if command == 'r' :
request_reading()