I'm doing a little program.
First, I used a PIR sensor to light a LED and it works fine.
Here is the code :
#define pirPin 2
#define ledPin 13
// Create variables:
int val = 0;
bool motionState = false; // We start with no motion detected.
void setup() {
// Configure the pins as input or output:
pinMode(ledPin, OUTPUT);
pinMode(pirPin, INPUT);
// Begin serial communication at a baud rate of 9600:
Serial.begin(9600);
}
void loop() {
// Read out the pirPin and store as val:
val = digitalRead(pirPin);
// If motion is detected (pirPin = HIGH), do the following:
if (val == HIGH) {
digitalWrite(ledPin, HIGH); // Turn on the on-board LED.
// Change the motion state to true (motion detected):
if (motionState == false) {
Serial.println("Motion detected!");
motionState = true;
}
}
// If no motion is detected (pirPin = LOW), do the following:
else {
digitalWrite(ledPin, LOW); // Turn off the on-board LED.
// Change the motion state to false (no motion):
if (motionState == true) {
Serial.println("Motion ended!");
motionState = false;
}
}
}
Now I want to use 2 PIR sensors and 2 led. I tried to use the first code and duplicate all elements but I have a message " too many arguments to function ". I used " ; " to use 2 arguments in the if ( because I have 2 leds and 2 pir sensors) but I'm not sure.
Here is the code :
#define pirPin 2
#define ledPin 13
#define pirPinn 4
#define ledPinn 12
// Create variables:
int val = 0;
int vall = 0;
bool motionState = false; // We start with no motion detected.
bool motionStatee = false;
void setup() {
// Configure the pins as input or output:
pinMode(ledPin, OUTPUT);
pinMode(pirPin, INPUT);
pinMode(ledPinn, OUTPUT);
pinMode(pirPinn, INPUT);
// Begin serial communication at a baud rate of 9600:
Serial.begin(9600);
}
void loop() {
// Read out the pirPin and store as val:
val = digitalRead(pirPin);
vall = digitalRead(pirPinn);
// If motion is detected (pirPin = HIGH), do the following:
if (val == HIGH ; vall==HIGH) {
digitalWrite(ledPin, HIGH && ledPinn,HIGH); // Turn on the on-board LED.
// Change the motion state to true (motion detected):
if (motionState == false ; motionStatee == false) {
Serial.println("Motion detected!");
motionState = true;
motionStatee = true;
}
}
// If no motion is detected (pirPin = LOW), do the following:
else {
digitalWrite(ledPin, LOW ; ledPinn, LOW ); // Turn off the on-board LED.
// Change the motion state to false (no motion):
if (motionState == true ; motionStatee == true) {
Serial.println("Motion ended!");
motionState = false;
motionStatee = false;
}
}
}
Pay particular attention to Item #6 which will tell you how to properly post your code using Code Tags.
Then, post your code properly along with the COMPLETE error message, not your interpretation of it. You can copy the error message directly from the black message window in the IDE.
When I use &&, it doesn't work. Here is the error message.
In function 'void loop()':
sketch_dec06c:28:46: error: too many arguments to function 'void digitalWrite(uint8_t, uint8_t)'
digitalWrite(ledPin, HIGH && ledPinn,HIGH); // Turn on the on-board LED.
^
In file included from sketch/sketch_dec06c.ino.cpp:1:0:
ARDUINO/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Arduino.h:134:6: note: declared here
void digitalWrite(uint8_t, uint8_t);
^~~~~~~~~~~~
Arduino/sketch_dec06c/sketch_dec06c.ino:31:9: warning: init-statement in selection statements only available with -std=c++1z or -std=gnu++1z
if (motionState == false ; motionStatee == false) {
^~~~~~~~~~~
sketch_dec06c:39:46: error: too many arguments to function 'void digitalWrite(uint8_t, uint8_t)'
digitalWrite(ledPin, LOW && ledPinn, LOW ); // Turn off the on-board LED.
^
In file included from sketch/sketch_dec06c.ino.cpp:1:0:
ARDUINO/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Arduino.h:134:6: note: declared here
void digitalWrite(uint8_t, uint8_t);
^~~~~~~~~~~~
exit status 1
too many arguments to function 'void digitalWrite(uint8_t, uint8_t)'[\code]
So in the DigitalWrite we can use just 1 argument.
I tried to write 2 times digitalwrite. The code works without errors but nothing is happening when I put my hand in front of the PIR sensor.
Here is the code :
#define pirPin 2
#define ledPin 13
#define pirPinn 4
#define ledPinn 12
// Create variables:
int val = 0;
int vall = 0;
bool motionState = false; // We start with no motion detected.
bool motionStatee = false;
void setup() {
// Configure the pins as input or output:
pinMode(ledPin, OUTPUT);
pinMode(pirPin, INPUT);
pinMode(ledPinn, OUTPUT);
pinMode(pirPinn, INPUT);
// Begin serial communication at a baud rate of 9600:
Serial.begin(9600);
}
void loop() {
// Read out the pirPin and store as val:
val = digitalRead(pirPin);
vall = digitalRead(pirPinn);
// If motion is detected (pirPin = HIGH), do the following:
if (val == HIGH && vall==HIGH) {
digitalWrite(ledPin, HIGH );
digitalWrite(ledPinn, HIGH); // Turn on the on-board LED.
// Change the motion state to true (motion detected):
if (motionState == false && motionStatee == false) {
Serial.println("Motion detected!");
motionState = true;
motionStatee = true;
}
}
// If no motion is detected (pirPin = LOW), do the following:
else {
digitalWrite(ledPin, LOW );
digitalWrite(ledPinn, LOW); // Turn off the on-board LED.
// Change the motion state to false (no motion):
if (motionState == true && motionStatee == true) {
Serial.println("Motion ended!");
motionState = false;
motionStatee = false;
}
}
}
rabahdz:
I used " ; " to use 2 arguments in the if
That is not valid C. In fact, I don't think it's valid in any programming language.
If you want to use more than one test in an if statement, look into the boolean operators ("&&" for and, "||" for or, and so on - you can find it in any guide to C or Arduino programming. If you need different behavior depending on which condition is true, use multiple if() statements or an if()/else if() construct.
you also can't call a function with multiple sets of arguments - the digitalWrite() line cited above is not valid C either. Use two consecutive calls to digitalWrite().
(note - digitalWrite(pin,HIGH) is two arguments, pin and HIGH. When you call a function, you must provide the number of arguments that it expects, otherwise it will be a compile error.)
As for your current issue - yeah, debug print statements are how you debug stuff. Print the values you're reading from the PIR sensors. Do they match what you expect? Yes? Then put some further down in the conditionals and see which code path it's following. Etc.