Offline
Newbie
Karma: 0
Posts: 17
|
 |
« on: February 24, 2012, 08:11:44 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Massachusetts, USA
Offline
Tesla Member
Karma: 97
Posts: 6381
|
 |
« Reply #1 on: February 24, 2012, 08:20:54 am » |
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,
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35566
Seattle, WA USA
|
 |
« Reply #2 on: February 24, 2012, 09:30:16 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Boston area, metrowest
Online
Brattain Member
Karma: 249
Posts: 16563
Available for Design & Build services
|
 |
« Reply #3 on: February 24, 2012, 12:48:47 pm » |
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; }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35566
Seattle, WA USA
|
 |
« Reply #4 on: February 24, 2012, 01:01:44 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 17
|
 |
« Reply #5 on: February 25, 2012, 05:18:51 am » |
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); }
Moderator edit: CODE TAGS God bless..
|
|
|
|
« Last Edit: February 27, 2012, 06:49:17 am by AWOL »
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
|
 |
« Reply #6 on: February 25, 2012, 06:31:23 am » |
- 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 ?
|
|
|
|
|
Logged
|
|
|
|
|
Austin, TX
Offline
Faraday Member
Karma: 41
Posts: 5176
CMiYC
|
 |
« Reply #7 on: February 25, 2012, 09:33:53 am » |
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.
|
|
|
|
« Last Edit: February 25, 2012, 01:02:56 pm by James C4S »
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35566
Seattle, WA USA
|
 |
« Reply #8 on: February 25, 2012, 12:16:13 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Austin, TX
Offline
Faraday Member
Karma: 41
Posts: 5176
CMiYC
|
 |
« Reply #9 on: February 25, 2012, 01:01:24 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 17
|
 |
« Reply #10 on: February 27, 2012, 03:38:05 am » |
 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.
|
|
|
|
|
Logged
|
|
|
|
|
South Texas
Offline
God Member
Karma: 8
Posts: 976
|
 |
« Reply #11 on: February 27, 2012, 11:08:29 am » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #12 on: February 27, 2012, 12:31:52 pm » |
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.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
|