Terry thanks for the schematic
I'm using 2 terminals of the motor GND and FAST.
It took me a little while, but i think it does make sense now. Because i cannot read circuit schematics correctly, i always like to make my own visual image that makes sensce to me (that's what art-school makes you do haha).
Yet another drawings! I think i got it right this time . Please tell me if i'm wrong :).
Alright, i had a little bit of time today so i started to think about writing the code for my art project. I've been reading starterguides and tried to come up with a simple code to control my arduino using a 2-channel relay (hooked up to a dc motor) and a PIR-sensor. It's all really difficult for me, but yopu have to start somewhere. Of course a have a few questions:
In the post before this one, i was working with the different handware components.
Can anyone cofirm i did hook up the different parts correctly?
This is the first code i've ever written, go easy on me :* Am i going in the right direction here?
/*
PIR sensor tester
*/
int inputPin = 13; // choose the input pin (for PIR sensor)
int r1Pin = 12; // choose the pin for the relay-channel 1 (up)
int r2Pin = 11; // choose the pin for the relay-channel 2 (down)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
void setup() // SETUP:runs once
{
pinMode(inputPin, INPUT); // declare PIR-sensor as input
pinMode(r1Pin, OUTPUT); // declare RELAY-channel 1 as output
pinMode(r2Pin, OUTPUT); // declare RELAY-channel 2 as output
Serial.begin(9600);
}
void loop()
{
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(r1Pin, HIGH); // turn relay-1 (up) ON
delay(5000); // for 5 seconds
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(r2Pin, HIGH); // turn relay-2 (down) ON
delay(5000); // for 5 seconds
if (pirState == HIGH){
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
After a long and hectic time, i'm starting again with the project. Still need to order some parts, but i'm having correspondance about it. Ik would like to ask someone to check out my code, it woul be a great help!
Greetings an overexited art student from the netherlands.
Alright, after I got the parts, i started to experimate some with teh arduino and pir sensors.
The first sketch for 1 pir 1 two-channel relay and 1 motor worked, now i'm trying to get more parts working. Eventually 5 pairs of pir sensor, relay and motor have to work independantly.
The sketch below is based on 2 pir sensors, 2 two channel relays which drive two motors.
The problems:
I would like to get more pairs (pir, relay and motor) working separetely from each other. One pair worked fine, when i added another, it skipped some of the code. ( it goes from motion detected pir 1 to motion ended pir 2, skipping the 'else' part from pair pir1+relay1, and skipping the if part from pir2+relay2
I would like to get the pir's working separately, at the moment if one pir is activated the other cannot be activated.
This is my code so far:
/*
* PIR sensor tester
*/
int PIR1 = A0; // choose the input pin (for PIR sensor 1)
int PIR2 = A1; // choose the input pin (for PIR sensor 2)
int r1Pin = 2; // choose the pin for the relay1-channel 1 (up)
int r2Pin = 3; // choose the pin for the relay1-channel 2 (down)
int r3Pin = 4; // choose the pin for the relay2-channel 1 (up)
int r4Pin = 5; // choose the pin for the relay2-channel 2 (down)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
void setup() // SETUP:runs once
{
Serial.begin(9600);
// PIR
pinMode(PIR1, INPUT); // declare PIR-sensor1 as input
pinMode(PIR2, INPUT); // declare PIR-sensor2 as input
// RELAY 1
pinMode(r1Pin, OUTPUT);
digitalWrite(r1Pin, LOW); // switch off by default
pinMode(r2Pin, OUTPUT);
digitalWrite(r2Pin, LOW); // switch off by default
// RELAY 2
pinMode(r3Pin, OUTPUT);
digitalWrite(r3Pin, LOW); // switch off by default
pinMode(r4Pin, OUTPUT);
digitalWrite(r4Pin, LOW); // switch off by default
}
void loop()
{
// PIR-1+RELAY-1
val = analogRead(PIR1); // read input value
if (val > 500)
{
if (pirState == LOW)
{
// FEEDBACK
Serial.println("Motion detected pir 1!");
// UP
digitalWrite(r1Pin, HIGH); // turn relay-1 (up) ON
delay(5000); // for 5 seconds
digitalWrite(r1Pin, LOW); // turn relay-1 (up) OFF
// NEW STATE
pirState = HIGH;
}
} else {
if (pirState == HIGH){
// FEEDBACK
Serial.println("Motion ended pir 1!");
// DOWN
digitalWrite(r2Pin, HIGH); // turn relay-2 (down) ON
delay(5000); // for 5 seconds
digitalWrite(r2Pin, LOW); // turn relay-2 (down) OFF
// NEW STATE
pirState = LOW;
}
}
// PIR-2+RELAY-2
val = analogRead(PIR2); // read input value
if (val > 500)
{
if (pirState == LOW)
{
// FEEDBACK
Serial.println("Motion detected pir 2!");
// UP
digitalWrite(r3Pin, HIGH); // turn relay-1 (up) ON
delay(5000); // for 5 seconds
digitalWrite(r3Pin, LOW); // turn relay-1 (up) OFF
// NEW STATE
pirState = HIGH;
}
} else {
if (pirState == HIGH){
// FEEDBACK
Serial.println("Motion ended pir 2!");
// DOWN
digitalWrite(r4Pin, HIGH); // turn relay-2 (down) ON
delay(5000); // for 5 seconds
digitalWrite(r4Pin, LOW); // turn relay-2 (down) OFF
// NEW STATE
pirState = LOW;
}
}
}