Hi,
I am reading 4 digital pin status and writing the messages in to the 16x2 display
example : if 0000 ( all 4 pins reads 0 i am writing " NO ALARM" )
if 0001 i am need to write " Floor 1 leak "
i used switch - case for this but it is not succeeded , can you suggest any method , thanks in advance ,
-ariesprg
The switch statement should have worked unless you were doing it wrong. If you show the code you tried we might be able to point out how to get it working,
bitWrite doesn't really help for sending messages to an LCD does it?
I think perhaps bitRead would be more helpful.
messageCode = 0; // clear it out to start
// read the 4 bits and put them in place
messageCode = digitalRead(pinW)<<3 + digitalRead(pinX)<<2 + digitalRead(pinY) <<1 + digitalRead(pinZ);
switch (messageCode){
case 0:
// no alarm
break;
case 1:
// code for 1
break;
:
:
case 15:
// final code
break;
}
Thanks to all your suggestions , yes CrossRoads Robert Ji , i followed your code and finished it ,below my sample
int messageCode = 0; // clear it out to start
void setup() {
// initialize serial communication:
Serial.begin(9600);
pinMode(6,INPUT);
pinMode(7,INPUT);
pinMode(8,INPUT);
}
void loop()
{
// read the 4 bits and put them in place
int bitData1 = digitalRead(6)<<2 ;
int bitData2 = digitalRead(7)<<1 ;
int bitData3 = digitalRead(8) ;
messageCode = bitData1 + bitData2 + bitData3;
switch (messageCode)
{
case 0:
Serial.println("i am case 0");
break;
case 1:
Serial.println("i am case 1");
break;
case 2:
Serial.println("i am case 2");
break;
case 3:
Serial.println("i am case 3");
break;
case 4:
Serial.println("i am case 4");
break;
}
delay(1000);
}
An int is 16 bites wide. Without resetting messageCode at the end of the loop() (or before combing your 3 dataBits together), you're shifting the previous results into the higher bits. So you're probably only seeing a valid messageCode on the first loop(). Every iteration after that is a result you don't expect. If digitalRead(6) is high on the first iteration you'll get messageCode = 4. If it stays high on the next iteration, messageCode = 20.
Edit: Thanks Paul. Crossing out the incorrect explanation.
Without resetting messageCode at the end of the loop() (or before combing your 3 dataBits together), you're shifting the previous results into the higher bits. So you're probably only seeing a valid messageCode on the first loop(). Every iteration after that is a result you don't expect.
I'm not following this logic. Each dataBitN is based on a read and a shift. The data bits are then added and assigned as a new value for messageCode. That looks right to me.
Without resetting messageCode at the end of the loop() (or before combing your 3 dataBits together), you're shifting the previous results into the higher bits. So you're probably only seeing a valid messageCode on the first loop(). Every iteration after that is a result you don't expect.
I'm not following this logic. Each dataBitN is based on a read and a shift. The data bits are then added and assigned as a new value for messageCode. That looks right to me.
Bosses i respect all your responses , i am newbie to programming . basically biomedical guy . yes i need to react to all 16 combinations .thanks to all. god bless.
Your code in reply #5 decodes 3 bits, but only has 5 of the eight possible cases.
It would be trivial to extend this code to four bits and sixteen possible cases.