I have an atmega 328p connected to a Micro as slave over I2C, the 328 is meant to measure two LDRs and send the results, however all I get are squares, the ascii code looks like 254 or something. Before the current code I had blink working, as well as 'hello ' which you can see commented out still in the code, these all worked fine, what am I doing wrong? No matter how many bits I request I just get back the requested number of squares.
Code:
Master
// Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the "Wire Slave Sender" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Wire.h>
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
void loop() {
Wire.requestFrom(0x08, 2); // request 2 bytes from slave device 0x08
while (Wire.available()) { // slave may send less than requested
int c = Wire.read(); // receive a byte as character
Serial.println(c); // print the character
}
delay(2500);
}
Slave
// Wire Slave Sender
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Sends data as an I2C/TWI slave device
// Refer to the "Wire Master Reader" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Wire.h>
const byte SLAVE_ADDRESS = 0x08;
const int ldrPin1 = 17;
//const int ldrPin2 = 16;
void setup() {
Wire.begin(SLAVE_ADDRESS); // join i2c bus with address SLAVE_ADDRESS
Wire.onRequest(requestEvent); // register event
pinMode(ldrPin1, INPUT);
//pinMode(ldrPin2, INPUT);
}
void loop() {
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
int ldrStatus1 = analogRead(ldrPin1);
//int ldrStatus2 = analogRead(ldrPin2);
//Wire.write("hello "); // respond with message of 6 bytes
Wire.write(ldrStatus1);
//Wire.write(ldrStatus2);
}
Thanks for any help you can throw my way!