Hey fellows,
i think the title says it all, I'm trying to read from a sensor connected through i2c but as soon as wire.requestFrom() is called, my arduino mega 2560 reboots. Here's the code, if anybody sees something dubious, please tell me.
#include <LiquidCrystal.h>
#include <Wire.h>
#define ADRESSE_SUIVEUR 0x28
const int PIN_OUT = 43;
const int C1 = 31;
const int C2 = 33;
const int C3 = 35;
const int C4 = 37;
const int C5 = 39;
const int power = 40;
const int ground = 50;
byte donnees = B00000000;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup(){
Wire.begin();
pinMode(C1,OUTPUT);
pinMode(C2,OUTPUT);
pinMode(C3,OUTPUT);
pinMode(C4,OUTPUT);
pinMode(C5,OUTPUT);
pinMode(power, OUTPUT);
digitalWrite(power,HIGH);
pinMode(ground, OUTPUT);
digitalWrite(ground, LOW);
lcd.print("INIT SUIVEUR");
delay(2000);
lcd.clear();
}
void loop(){
traitementSuiveur(updateSuiveur());
delay(250);
}
byte updateSuiveur(){
lcd.print(" update");
byte byteSuiveur = B00000000; //byte storing sensor state
lcd.print(" byte request");
delay(500);
Wire.requestFrom(ADRESSE_SUIVEUR,1);
lcd.clear();
lcd.print(" req sent");
if (Wire.available()){
lcd.print("wire connected");
byteSuiveur = Wire.read();
}
else{
lcd.print("wire not connected");
}
return byteSuiveur = byteSuiveur & 0x1f; // set first 3 bits at 000
}
void traitementSuiveur(byte byteSuiveur){
int signalOut = 0;
switch(byteSuiveur){
case B00000110:
signalOut = 132; //2.5V
lcd.clear();
lcd.print("--x--");
analogWrite(PIN_OUT, signalOut);
break;
case B00000010:
signalOut = 110; //2V
lcd.clear();
lcd.print("---x-");
analogWrite(PIN_OUT, signalOut);
break;
case B00001010:
signalOut = 155; //3V
lcd.clear();
lcd.print("-x---");
analogWrite(PIN_OUT, signalOut);
break;
case B00010010:
signalOut = 205; //4V
lcd.clear();
lcd.print("x----");
analogWrite(PIN_OUT, signalOut);
break;
case B00000011:
signalOut = 51; //1V
lcd.clear();
lcd.print("----x");
analogWrite(PIN_OUT, signalOut);
break;
case B00011111:
signalOut = 0;
lcd.clear();
lcd.print("all black");
analogWrite(PIN_OUT, signalOut);
break;
case B00000000:
signalOut = 0;
lcd.clear();
lcd.print("all white");
analogWrite(PIN_OUT, signalOut);
break;
}
}