help reading simple code

Hello - I'm novice at Arduino and programming generally. I have an Elego Uno R3 board, and recently bought a PIR motion sensor. The sensor came with some example code, but it isn't working quite as I expected, so wondered if someone could help me understand three (probably) easy questions:

I've copied the full example code and attached the circuit diagram but these are my questions:

#define OUT_PIN 2
uint8_t output_value = 0;
bool motion_detected = false;
void setup() {
 Serial.begin(9600);
 pinMode(OUT_PIN, INPUT);
 delay(60000);
}
void loop() {
 output_value = digitalRead(OUT_PIN);
 if (output_value) {
 Serial.println("Object detected!");
 motion_detected = true;
 delay(3000);
 } else {
 Serial.println("No object!");
 }
 if (motion_detected) {
 Serial.println("Wait!");
 delay(6000);
 Serial.println("Ready...");
 motion_detected = false;
 }
}

Q1: I saw this line
uint8_t output_value = 0;

and stupidly plugged the PIR motion sensor into pin 0. But even so, it all worked fine. Why does that happen? It needs to be plugged into pin 2? Any ideas why the Arduino is detecting inputs from the sensor, even though it's plugged into the wrong pin?

Q2: For setting up pins in other projects, I've used code like this to read signals from sensors:

OUT_PIN = 2
pinMode(OUT_PIN, INPUT);

Then I would detect if the sensor had been triggered with code like this:

if (digitalRead(OUT_PIN) == HIGH)
{
//do something fun
}

My question is:
If I've understood the code right, then

#define OUT_PIN 2

is equivalent to

OUT_PIN = 2;

pinMode(OUT_PIN, INPUT);

Have i understood correctly - the code does the same thing, but written in a different way?

Q3: Does the variable "output_value" take the value 0 or 1, or TRUE or FALSE?

This makes me think it's 0/1
uint8_t output_value = 0;
This line makes me think it's TRUE/ FALSE
if (output_value) {

Thank you

KevMeister:
Q1: I saw this line
uint8_t output_value = 0;

and stupidly plugged the PIR motion sensor into pin 0. But even so, it all worked fine. Why does that happen? It needs to be plugged into pin 2? Any ideas why the Arduino is detecting inputs from the sensor, even though it's plugged into the wrong pin?

what do you mean worked fine?

why is a pin labeled OUTPUT_PIN configured as an INPUT

pinMode(OUT_PIN, INPUT);

KevMeister:
Q2: For setting up pins in other projects, I've used code like this to read signals from sensors:

Have i understood correctly - the code does the same thing, but written in a different way?

a #define is a preprocessor command that will replace OUT_PIN with a 2, in this case, before being compiled

you could define a variable with that value and use the variable (which consumes RAM)

byte OUT_PIN = 2;

KevMeister:
Q3: Does the variable "output_value" take the value 0 or 1, or TRUE or FALSE?

remember that everything is made up of bits.
what bit values would you assign to TRUE?

(C code does not define true/false. C++ does)

gcjr:
you could define a variable with that value and use the variable (which consumes RAM)

You should define a constant with that value and use the constant (which may not consume RAM)const byte OUT_PIN = 2;

First let me that was not a stupid mistook, I would guess at least 90% or more of the people using this board have done it sometime, many more than once with a lot more smoke. Your code is very hard to read, you have input and outputs confused in your statements. In this case your point of reference is the Microprocessor, not the sensor. Therefor input is data into the microprocessor, out is data sent from the processor. You tell the microprocessor if the pin is an input or output with the pinMode(pin, mode) statement. pin is the arduino pin you want to define in this case pin 2 as an input (you can use most any other pin). Next you need to tell it the mode you want it to operate in; these are the parameters you need to give it. INPUT, OUTPUT, or INPUT_PULLUP. Note use only one and they must be all upper case.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.