The code below works perfectly AS LONG AS I have the "
TinyWireS.begin(I2C_SLAVE_ADDR)" commented out. If I uncomment that line, it seems that only the LED attached to pin 1 works. LEDs on pin 0 and 2 do not come on. Comment the TinyWireS.begin... line again and all is well.
What might I be doing wrong to cause this behaviour?
The I2C_SLAVE_ADDR value was stollen from some example code, so I don't know if that is an issue or not.
Any direction or questions would probably be helpful.
Once I get this figured out, I hope to send a value 0 - 5 from the Arduino Pro Mini using I2C. I have not coded the master yet because I wanted to make sure I could get this to work even if there was nothing to read yet. Is that a bad plan?
Thanks
Charlie
#include "TinyWireS.h"
#define I2C_SLAVE_ADDR 0x26 // i2c slave address (38)
int leftPin=2;
int backupPin=0;
int rightPin=1;
int lightMode=0;
int flash=0;
byte byteRcvd = 0;
int cnt=0;
void setup() {
pinMode(backupPin,OUTPUT);
pinMode(leftPin,OUTPUT);
pinMode(rightPin,OUTPUT);
// TinyWireS.begin(I2C_SLAVE_ADDR); // init I2C Slave lightMode
}
void loop() {
flash ^= 1;
// these lines will be used to retrieve a byte (0 - 5) once I can get this to work
//if (TinyWireS.available()) {byteRcvd=TinyWireS.receive();}
//else {byteRcvd=2;};
/*
lightModes
0=lignts off
1=brakes
2=backup
3=left
4=right
5=hazard
*/
switch(lightMode) {
case 0:
lights_off();
break;
case 1:
digitalWrite(backupPin,LOW);
digitalWrite(rightPin,HIGH);
digitalWrite(leftPin,HIGH);
break;
case 2:
digitalWrite(backupPin,HIGH);
digitalWrite(rightPin,LOW);
digitalWrite(leftPin,LOW);
break;
case 3:
digitalWrite(backupPin,LOW);
digitalWrite(rightPin,LOW);
digitalWrite(leftPin,flash);
break;
case 4:
digitalWrite(backupPin,LOW);
digitalWrite(leftPin,LOW);
digitalWrite(rightPin,flash);
break;
case 5:
digitalWrite(leftPin,flash);
digitalWrite(backupPin,LOW);
digitalWrite(rightPin,flash);
break;
default:
digitalWrite(leftPin,flash);
digitalWrite(backupPin,flash);
digitalWrite(rightPin,flash);
break;
}
delay(200);
// these lines are for testing only. This will cycle through all five modes
// for a few seconds each
if (cnt > 25) {
if (lightMode < 5)
{lightMode += 1;}
else
{lightMode=0;}
cnt=0;
}
cnt += 1;
}
void lights_off()
{
digitalWrite(rightPin,LOW);
digitalWrite(backupPin,LOW);
digitalWrite(leftPin,LOW);
}