Go Down

Topic: Read Binary value from multiple pins at once (Read 160 times) previous topic - next topic

iber

I'm trying to connect my arduino to a coin counter (like the ones in vending machines).
Depending on the coin I put in, the counter will give a binary value spread over 6 output lines.
So now I need to read these values on my Arduino.

Will this work when I use the PIND and connect the counter outputs to my arduino ports 0-6?
Will using the PIND variable in the loop trigger a 'DigitalRead' on all pins at once?

so
Code: [Select]

void setup(){}

void loop()
{
  Serial.println(PIND); // will this trigger the read on pins 0-7?
}

groundFungus

#1
Nov 24, 2016, 01:26 pm Last Edit: Nov 24, 2016, 01:26 pm by groundfungus
Code: [Select]
void setup(){}

Without Serial.begin(BAUD?) in setup() there will be no serial output.
https://www.arduino.cc/en/Reference/Serial


J-M-L

#2
Nov 24, 2016, 01:31 pm Last Edit: Nov 24, 2016, 01:33 pm by J-M-L
Probably better to use Port registers B which maps to digital pin 8 to 13 rather than D (digital pins 0 to 7) because you have the Serial Port in that one so you need to be a bit more carful how you write there (if you need to write). if it's only reading, then indeed D2 to D7 would give you 6 easy pins to read too (as would D8 to D13)

see PortManipulation
Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

outsider

#3
Nov 24, 2016, 01:44 pm Last Edit: Nov 24, 2016, 02:12 pm by outsider
Put the coin acceptor lines on pins 2 - 7, then: EDIT: added prntBits()
Code: [Select]

void setup(){
 Serial.begin(9600);
}
void loop(){
  byte val = PIND >>2;
  prntBits(val);
  delay(500);

void prntBits(byte b)
{
  for(int i = 7; i >= 0; i--)
    Serial.print(bitRead(b,i));
  Serial.println();
}

}

J-M-L

#4
Nov 24, 2016, 02:01 pm Last Edit: Nov 24, 2016, 02:02 pm by J-M-L
Quote
The way I understand the registers by the way, is that they are the pins
While it's a bit of a technical shortcut blasphemy :), you can indeed intellectually picture such a register that way

So when you take a snapshot of the register values, you are actually looking in one go at the values of all the pins at that "instant" (at that cycle of the processor which takes some time, not an infinitely short moment)


Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

Go Up