A little help with PIND please

Hey guys,

I’m new to Arduino and would like a little help understanding what PIND is and how to Serial.println (PIND); to see what value it currently is. Currently I’m trying to print the value of PIND in step 7 to verify it’s value. Also why in each test step 1-8 the PIND doubles it’s value?

/ variables

int failCount = 0;
int inp = 0;      // generic digital port input var
int testing = 0;  // test number or 0 if not testing
int td = 100;  // time LED is on during power up test
int td2 = 20; // time LED is on during test so poeple can see them

int i = 0;     // generic indexin vars
int x = 0;

int ledGreen = 16;  // some port assigments here
int ledRed = 17;
int startButton = 18;
int cableSel = 19;


// the setup routine runs once when you press reset:
void setup() {                
 // initialize the digital pins
 Serial.begin(9600);
 pinMode(startButton, INPUT_PULLUP);
 pinMode(cableSel, INPUT_PULLUP);
 DDRD = DDRD | B00000000; // set D port as inputs 
 pinMode(ledGreen, OUTPUT);
 pinMode(ledRed, OUTPUT);
 for (int i=8; i <= 15; i++){   // set outputs
     pinMode(i, OUTPUT);
     delay(10);
  }
 
 outputsOff();
  
 digitalWrite(ledGreen, HIGH);  //LED has common cathode, high = off.
 digitalWrite(ledRed, HIGH); 
 
 // test lights
 for (int i=8; i <= 15; i++){   // cycle through leds at reset
    digitalWrite(i, HIGH);
    delay(td);
    digitalWrite(i, LOW);
    delay(td);  
  }
 digitalWrite(ledGreen, LOW); // cycle the red/green led now
 delay(500);
 digitalWrite(ledGreen, HIGH);
 delay(100);
 digitalWrite(ledRed, LOW);
 delay(500);
 digitalWrite(ledRed, HIGH);
 delay(500);
}

// the loop routine runs over and over again forever:
void loop() {
 while(testing == 0){
   inp = digitalRead(startButton); // wait for start button
   if (inp == 0){
     delay(20); // if start button pressed wait 20 more msec
     inp = digitalRead(startButton); // check start button again
     if (inp == 0){ //if it's still pressed...
       while(inp==0){ // wait for the start button to be released
         inp = digitalRead(startButton); // keep checking for release
       }
       testing = 1;
     }
   }
 }
 
 // ************** START OF TESTS *************
 // test 1
 if(testing == 1){
   digitalWrite(8, HIGH); // set input high
   delay(td2);            // make the led visible to operator
   inp = PIND;            // read all inputs
   if(inp != 1){          // if all inputs <> 1 (or whatever per test) 
     failCount = testing; // inc failcount for fail blinks
     testing = 99;        // go beyond all test numbers to fail
   }
   else{
     digitalWrite(8, LOW);// if pass, then turn off LED
     testing++;           // inc testing to advance to next test
   }
 }
 
 // test 2
 if(testing == 2){
   digitalWrite(9, HIGH);
   delay(td2);
   inp = PIND;
   if(inp != 2){
     failCount = testing;
     testing = 99;
   }
   else{
     digitalWrite(9, LOW);
     testing++;
   }
 } 
 
// test 3
 if(testing == 3){
   digitalWrite(10, HIGH);
   delay(td2);
   inp = PIND;
   if(inp != 4){
     failCount = testing;
     testing = 99;
   }
   else{
     digitalWrite(10, LOW);
     testing++;
   }
 }  
 
// test 4
 if(testing == 4){
   digitalWrite(11, HIGH);
   delay(td2);
   inp = PIND;
   if(inp != 8){
     failCount = testing;
     testing = 99;
   }
   else{
     digitalWrite(11, LOW);
     testing++;
   }
 }  

// test 5
 if(testing == 5){
   digitalWrite(12, HIGH);
   delay(td2);
   inp = PIND;
   if(inp != 16){
     failCount = testing;
     testing = 99;
   }
   else{
     digitalWrite(12, LOW);
     testing++;
   }
 }  

// test 6
 if(testing == 6){
   digitalWrite(13, HIGH);
   delay(td2);
   inp = PIND;
   if(inp != 32){
     failCount = testing;
     testing = 99;
   }
   else{
     digitalWrite(13, LOW);
     testing++;
   }
 }

// test 7
 if(testing == 7){
   digitalWrite(14, HIGH);
   delay(td2);
   inp = PIND;
   Serial.println(PIND);
   if(inp != 64){
     failCount = testing;
     testing = 99;
   }
   else{
     digitalWrite(14, LOW);
     testing++;
   }
 }
 
 // test 8
 if(testing == 8){
   digitalWrite(15, HIGH);
   delay(td2);
   inp = PIND;
   if(inp != 128){
     failCount = testing;
     testing = 99;
   }
   else{
     digitalWrite(15, LOW);
     testing++;
   }
 }

delay(500); // break up testing lights and status light for aesthetics
if(testing > 0){
     if(failCount == 0){ 
       blinkPass(); 
     }
     else{
       blinkError(failCount); 
     }
   }
   testing = 0;
   failCount = 0;
   delay(1000); 
}

/*************************************************************************
****                        SUBROUTINES                               ****
**************************************************************************/

// if all tests pass, flash green led, and wait for button press & release
void blinkPass(){
 int d = 0;
 do{
   inp=digitalRead(startButton);
   digitalWrite(ledGreen, LOW);
   delay(100);
   digitalWrite(ledGreen, HIGH);
   delay(100);
   inp = digitalRead(startButton);
   if (inp == 0){
     delay(20);
     inp = digitalRead(startButton);
     if (inp == 0){
       do{
         inp = digitalRead(startButton);
       }
       while(inp==0);
       d = 1;
     }
   } 
 }
 while(d == 0); 
}     

// if fail, flash the failing test number with the red led and wailt for button press
void blinkError(int bnum){
 //bnum = 5;
 int d = 0;
 do{
   inp=digitalRead(startButton);
   for (int i=1; i <= bnum; i++){
     digitalWrite(ledRed, LOW);
     delay(300);
     digitalWrite(ledRed, HIGH);
     delay(300);
     inp = digitalRead(startButton);
     if(inp == LOW){
       d = 1;
       break;
     }
   }
   for (int z = 0; z <= 10; z++){
     inp = digitalRead(startButton);
     if(inp == LOW){
       d = 1;
       outputsOff();
       break;
     }
     delay(200);
   }
 }
  while(d == 0); 
  digitalWrite(ledRed, HIGH);
 }

void outputsOff() {
for (int i=7; i <= 15; i++){   // turn outputs off
     digitalWrite(i, LOW);
     delay(10);
  }
}

I take it you have 8 connected to 0, 9 to 1, 10 to 2, etc?
The doubling is from the inputs increasing:
0b00000001, 1
0b00000011, 3
0b00000111, 7
0b00001111, etc. 15, 31, 63, 127, 255

Why not 8 to 15 here? 7 is an input, isn’t it?
void outputsOff() {
for (int i=7; i <= 15; i++){ // turn outputs off

@dhutchins, please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum Your code is too long to study quickly.

…R

Thanks for the tip :). Edited per your quest. I'll make sure to do this for the future!

Read this...

https://www.arduino.cc/en/Reference/PortManipulation

and you will know what PIND is and exactly why you get the values you do.

Is it okay to read the PIND with the command;

Serial.println (PIND);

?

dhutchins:
Is it okay to read the PIND with the command;

Serial.println (PIND);

?

Does it work?

Personally I would read it into a variable and print from the variable

byte myByte = PIND;
Serial.println(myByte);

Why waste time wondering if something works when there is a simpler way

...R
(Typo corrected in response to Reply #7

What's the point of the variable you don't use there @Robin2? Maybe that's a typo?

Delta_G:
Maybe that's a typo?

Thanks for spotting that. I have corrected it.

...R

Ok variable assigned and it’s printing a value now.

I read through Arduino - PortManipulation and I’m still a little confused as to how PIND comes up with the value.

In “test 1” The value is 1 and in “test 2” the value is 2 and it doubles each time throughout the tests.

What is exactly happening to get the PIND value? I want to know exactly what I should be expecting when reading all pins. Without having to read them every time.

Did you really read it? It is simple. PortD is pins 0 to 7. Each bit in PIND represents one pin. If pin 0 is high then the first bit is 1. If pin 1 is high then the second bit is a 1. So with only pin 0 high PIND = 1 and when only pin 1 is high PIND = 2 etc etc for the rest of the pins.

I reckon it is best to think of PIND as producing an 8-bit binary value (e.g. 0b01010101 - which is the character 'U' or the decimal value 85) where each bit represents the state of the pin at the moment it was read. Thinking of it as 'U' or 85 does not make much sense.

...R