Go Down

Topic: digital staus pin read (Read 880 times) previous topic - next topic

ariesprg

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

johnwasser

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,
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

PaulS

Quote
The switch statement should have worked unless you were doing it wrong.

On the other hand, bitWrite() is pretty easy to use, to set one bit at a time. Much simpler code, that way.

CrossRoads

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;
}
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

PaulS

Quote
bitWrite doesn't really help for sending messages to an LCD does it?

No, but it could e used to set the bits in the byte that the switch statement is based on, where the cases decide what to write.

ariesprg

#5
Feb 25, 2012, 11:18 am Last Edit: Feb 27, 2012, 12:49 pm by AWOL Reason: 1
Thanks to all your suggestions , yes CrossRoads  Robert Ji , i followed your code and finished it ,below my sample  

Code: [Select]
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);
}


Moderator edit: CODE TAGS

God bless..

tuxduino

- When posting code, use the "#" button.
- Three bits give 8 combinations, not 5.

Do you really have to do something different for each of the 16 combinations of 4 pins or do you simply have to react to each pin status separately ?

James C4S

#7
Feb 25, 2012, 03:33 pm Last Edit: Feb 25, 2012, 07:02 pm by James C4S Reason: 1
Why do your cases only go to 4?  

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.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

PaulS

Quote
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.

James C4S


Quote
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.

You're right, I put too much though into it.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

ariesprg

:smiley-eek-blue: 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.

kf2qd

Are those 4 inputs all related to each other, or 4 seperate events? It would look like you have 4 seperate sensors that each need to send a message?

AWOL

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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up