First Arduino Code attempt

Hello,

This is my first attempt at using an Arduino to control a small DIY widget that I am constructing…

The components in my widget are a motion sensor, and a DC motor driven forward and reverse with two SSR (SP-NC)

Anyhow, here is the pseudo code that I am trying to get my sketch to accomplish:

Constantly read input from motion sensor

Once motion is detected, activate a “timer” for 5 min

After the elapsed 5 min turn the motor on forward for x amount of time(roll function)

Turn the motor off

Pause

Turn the motor on reverse for same x amount of time(unroll function)

Turn motor off

Return to reading input from motion sensor and start over

This is a very simple task, but since I am a noob to the whole coding aspect of this sparky fun game, I am looking for a litle input on what I have so far. Any and all input will be greatly appreciated!

Here is my code so far:

//Begin serial communications at 9600 bits/sec

//This is motor forward

const int forward = 3;

//This is motor reverse

const int reverse = 4;

//This is motion sensor

const int msense = 6;

void setup() {

Serial.begin(9600);

pinMode(msense, INPUT);

pinMode(forward, OUTPUT);

pinMode(reverse, OUTPUT);

}

void loop() {

int detect = digitalRead(msense);

delay(5);

if (detect == HIGH){

delay(300000);

void roll();

void unroll();

}

}

void roll () {

digitalWrite(forward, HIGH);

delay(60000);

digitalWrite(forward, LOW);

delay (1000);

}

void unroll() {

digitalWrite(reverse, HIGH);

delay(60000);

digitalWrite(reverse, LOW);

delay(1000);

}

void loop() {
int detect = digitalRead(msense);
delay(5);
if (detect == HIGH){
delay(300000);
void roll();
void unroll();

Why do you want to wait 5 minutes before it moves?

Yes, I want it to wait five minutes before anything happens. Also, I forgot to mention that I need to incorporate some code to ensure that my relays are receiving a signal from the controller on start up so that there is no chance of both relays being powered at the same time.

I think you’ve got a good start but before you go much further fine tuning it, I would advise you finish the hardware design as that will effect what you need to do for code. I’ll put comments re: that in your hardware thread.

FYI : this will make you code easier to read.

(click on to open and enlarge)

sounds like my “cat deterrent” mechanism I made a few years back, to keep the strays from entering my workshop and pissing on things. Essentially the same thing here, motion sensor triggers a simple sequence of switched / timed events…

some motor movements to startle any wayward cats (or anything else) along with a few delayed events (555 driven) to act as follow-up actions, like loud buzzer and doorbell. Code was Pbasic on a BS1 basic stamp. not much use here, but the arduino code wouldnt differ much aside from the translation.