Pages: [1]   Go Down
Author Topic: digital staus pin read  (Read 800 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Tesla Member
***
Karma: 208
Posts: 8860
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 616
Posts: 49446
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27143
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 616
Posts: 49446
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks to all your suggestions , yes CrossRoads  Robert Ji , i followed your code and finished it ,below my sample  

Code:
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 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)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

- 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

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 616
Posts: 49446
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

South Texas
Offline Offline
Edison Member
*
Karma: 8
Posts: 1025
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Brattain Member
*****
Karma: 303
Posts: 26354
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: