Pages: [1]   Go Down
Author Topic: Digital inputs and outputs  (Read 558 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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); }
              
      }
    }
}
« Last Edit: December 09, 2013, 05:22:43 pm by Paul39 » Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I only provide help via the forum - please do not contact me for private consultancy.

West Yorkshire, UK
Offline Offline
Edison Member
*
Karma: 48
Posts: 1440
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 545
Posts: 27357
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

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

Code:
if(digitalRead(10) == LOW){  // button that connects pin to Gnd when pressed
// do something
}
else {
//do something else - or don't
}
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.

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

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

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

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
Logged

UK
Offline Offline
Tesla Member
***
Karma: 135
Posts: 7690
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have the same question posted somewhere else! NAUGHTY

...R
Logged

Offline Offline
God Member
*****
Karma: 16
Posts: 601
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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  smiley

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26495
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if (Serial.available()) {
    delay(10);
For every character that you receive, you're wasting ten character periods.
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.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26495
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26495
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
this has stopped by output side.
Can you explain what that means please?
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.

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

This part of my code is not working:
Code:
{
  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
Logged

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

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26495
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Resolution?
BTW if... followed by while... with the same condition is usually redundant
« Last Edit: December 10, 2013, 02:57:26 pm by AWOL » 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: