Go Down

Topic: Digital inputs and outputs (Read 648 times) previous topic - next topic

Paul39

Dec 09, 2013, 11:08 pm Last Edit: Dec 09, 2013, 11:22 pm by Paul39 Reason: 1
Hi, I am a first time user of the UNO board. My first project is to read 4 pins input into a PC and write  4 output pins to UNO board. Can the serial on the UNO board handle this. Can you give me some programing tips. The code below I have written for outputs, not sure how to work the inputs  Thanks

void setup(){
 
 pinMode(2,OUTPUT);
 pinMode(3,OUTPUT);
 pinMode(4,OUTPUT);
 pinMode(5,OUTPUT);
 pinMode(6,OUTPUT);
 pinMode(7,OUTPUT);
 pinMode(8,OUTPUT);
 pinMode(9,OUTPUT);
 pinMode(10,INPUT);
 pinMode(11,INPUT);
 pinMode(12,INPUT);
 pinMode(13,INPUT);
 Serial.begin(9600);
}

void loop(){
 int val;
 if (Serial.available()) {
   delay(10);
   
   while (Serial.available() >0)  {
     val=Serial.read();
     
      if(val=='A') { digitalWrite(2,HIGH); }
     else if (val=='B') {digitalWrite(2,LOW); }
     
     else if(val=='C') { digitalWrite(3,HIGH); }
     else if (val=='D') {digitalWrite(3,LOW); }
     
     else if (val=='E') { digitalWrite(4,HIGH); }
     else if (val=='F') {digitalWrite(4,LOW); }
     
     else if(val=='G') { digitalWrite(5,HIGH); }
     else if (val=='H') {digitalWrite(5,LOW); }
     
     else if(val=='I') { digitalWrite(6,HIGH); }
     else if (val=='J') {digitalWrite(6,LOW); }
     
     else if(val=='K') { digitalWrite(7,HIGH); }
     else if (val=='L') {digitalWrite(7,LOW); }
     
     else if(val=='M') { digitalWrite(8,HIGH); }
     else if (val=='N') {digitalWrite(8,LOW); }
     
     else if(val=='O') { digitalWrite(9,HIGH); }
     else if (val=='P') {digitalWrite(9,LOW); }
             
     }
   }
}

PeterH

The issue is that you need to decide how to encode the states when you send them over the serial connection.

In the PC->Arduino direction, consider using upper case for 'high' and lower case for 'low' commands, so that your messages become 'A' / 'a', 'B' / 'b' and so on.

The code you posted looks as if it will work, but the long sequence of mutually exclusive if/else statements would also work neatly as a switch/case statement:
Code: [Select]

switch(val)
{
case 'A': digitalWrite(2, HIGH); break;
case 'a': digitalWrite(2, LOW); break;
case 'B': digitalWrite(3, HIGH); break;
case 'b': digitalWrite(3, LOW); break;
... etc
}


Bear in mind that some serial clients won't send anything until you enter a linefeed, the Arduino Serial Monitor is one of these. You can configure it to send combinations of CR/NL and the end of each line, but it won't actually send anything to the Arduino until you hit enter/return.

On the receiving side you will need to decide what application is going to receive the Arduino->PC status data and what it's going to do with it. If it's just displaying it in a serial client then you can format that status data however you want it to be displayed. Also on the Arduino->PC side you presumably only want to report changes in the input states rather than continually reporting the current status. The StateChangeDetection example sketch demonstrates how to remember the previous state of an input and compare that to the new state to see whether it has changed.

Given that you want to apply this logic to a whole set of input pins, IMO it would be sensible to put the pin numbers in an array, put the pin states in an array, and use a for loop to process each input.
I only provide help via the forum - please do not contact me for private consultancy.

PaulRB

Hi Paul,

Your sketch looks ok (but you should always post your sketch inside code tags - look for the button with a # symbol).

To read the inputs, use digitalRead on each of your inputs. For each one, use Serial.print to send one character (eg. H) if the input is high and another (eg. L) If it is low. Use Serial.println to go to a new line.

Paul

CrossRoads

Code: [Select]

  pinMode(10,INPUT_PULLUP);
  pinMode(11,INPUT_PULLUP);
  pinMode(12,INPUT_PULLUP);
  pinMode(13,INPUT_PULLUP);


Code: [Select]

if(digitalRead(10) == LOW){  // button that connects pin to Gnd when pressed
// do something
}
else {
//do something else - or don't
}
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Paul39

I need to transmit the switch state, back to the PC, but I think I'm being held by the WHILE statement!!

Paul39

Could you check my code. I think my loop is being held by the WHILE statement!!

Is there a way round this part of the code, so the UNO is looking for Rx data, but can send at the same time (Tx)!

Paul39

Robin2

You have the same question posted somewhere else! NAUGHTY

...R

alnath

Actually, it works, and you can check it, with Serial.print() statements as shown below .
Also, as PeterH says, using 'case' in this situation makes your code more readable.

Once the sketch is uploaded, open the serial monitor and enter 'A' and 'return'  etc....

Code: [Select]

void setup(){

  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(10,INPUT);
  pinMode(11,INPUT);
  pinMode(12,INPUT);
  pinMode(13,INPUT);
  Serial.begin(9600);
}

void loop()
{
  int val;
  if (Serial.available())
  {
  //  delay(10);
   
    while (Serial.available() >0)
    {
         val=Serial.read();
          switch (val)
          {
               case 'A' :  Serial.println("You entered A");   
               break;
               case 'B' :  Serial.println("You entered B");
               break;
           }
     }
  }
}



once you have confirmed it answers what you want, just replace the Serial.println() statements with
digitalWrite() ones  :)


AWOL

Code: [Select]
if (Serial.available()) {
    delay(10);

For every character that you receive, you're wasting ten character periods.
"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.

AWOL

Topics merged.
DO NOT CROSS-POST, IT WASTES EVERYONE'S TIME.

Please read the posting guidelines, and use CODE TAGS.
"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.

Paul39

Sorry if I've upset some people by listing twice, but I am new to this software and forum!

I have made some progress, on running four inputs, but this has stopped by output side.

Code: [Select]

int buttonState = 0;         
int lastButtonState = 0;     
int buttonState1 = 0;       
int lastButtonState1 = 0;
int buttonState2 = 0;         
int lastButtonState2 = 0;   
int buttonState3 = 0;       
int lastButtonState3 = 0;



void setup(){

  pinMode(2,INPUT_PULLUP);   
  pinMode(3,INPUT_PULLUP);
  pinMode(4,INPUT_PULLUP);
  pinMode(5,INPUT_PULLUP);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  int val;
  if (Serial.available())
  {   
    while (Serial.available() >0)
    {
         val=Serial.read();
          switch (val)
          {
           
            // Output control on pin 13
               case 'A' :  Serial.println("OUT13ON"); 
             digitalWrite(13,HIGH); 
               break;
               case 'B' :  Serial.println("OUT13OFF");
             digitalWrite(13,LOW); 
               break;
           }
     }
  }
 
  // input pin 2
buttonState = digitalRead(2);
  if (buttonState != lastButtonState) {
       if (buttonState == HIGH) {
       Serial.println("IN2ON");
      delay(50);
    }
    else {       
      Serial.println("IN2OFF");
      delay(50);
    }
  }
  lastButtonState = buttonState;
 
  //Input pin 3
    buttonState1 = digitalRead(3);
  if (buttonState1 != lastButtonState1) {
        if (buttonState1 == HIGH) {

      Serial.println("IN3ON");
      delay(50);
    }
    else {       
      Serial.println("IN3OFF");
      delay(50);
    }
  }
  lastButtonState1 = buttonState1;
 
  //Input pin 4
   buttonState2 = digitalRead(4);
  if (buttonState2 != lastButtonState2) {
    if (buttonState2 == HIGH) {
      Serial.println("IN4ON");
      delay(50);
    }
    else {       
      Serial.println("IN4OFF");
      delay(50);
    }
  }
  lastButtonState2 = buttonState2;

  //Input pin 5
    buttonState3 = digitalRead(5);
  if (buttonState3 != lastButtonState3) {
    if (buttonState3 == HIGH) {
      Serial.println("IN5ON");
      delay(50);
    }
    else {       
      Serial.println("IN5OFF");
      delay(50);
    }
  }
  lastButtonState3 = buttonState3;
}


Can anyone help. Paul39

AWOL

Quote
this has stopped by output side.

Can you explain what that means please?
"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.

Paul39

This part of my code is not working:
Code: [Select]

{
  int val;
  if (Serial.available())
  {   
    while (Serial.available() >0)
    {
         val=Serial.read();
          switch (val)
          {
           
            // Output control on pin 13
               case 'A' :  Serial.println("OUT13ON"); 
             digitalWrite(13,HIGH); 
               break;
               case 'B' :  Serial.println("OUT13OFF");
             digitalWrite(13,LOW); 
               break;
           }
     }
  }


Sorry Paul39

Paul39

Sorted thanks for your Help. :smiley-roll-sweat:

AWOL

#14
Dec 10, 2013, 08:55 pm Last Edit: Dec 10, 2013, 08:57 pm by AWOL Reason: 1
Resolution?
BTW if... followed by while... with the same condition is usually redundant
"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