sleep function

I am modifying an old ir controlled toy into a simple light flashing, backwards, forwards, left, right, and plays sounds for 5 seconds then goes to sleep. This is for my special needs grandaughter. Action reaction toy. The problem I have, apart from being new to this, is inserting a sleeper timer. I have a NO/NC big red led button she can press. When she presses this button lights will flash, the toy will move and make a noise. After about 5 seconds it will all stop and she will have to press it again.

This is a motor control sketch I'm using:

/* http://www.bajdi.com L9110 motor driver controlling 2 small DC motors */

const int AIA = 9; // (pwm) pin 9 connected to pin A-IA const int AIB = 5; // (pwm) pin 5 connected to pin A-IB const int BIA = 10; // (pwm) pin 10 connected to pin B-IA const int BIB = 6; // (pwm) pin 6 connected to pin B-IB

byte speed = 255; // change this (0-255) to control the speed of the motors

void setup() { pinMode(AIA, OUTPUT); // set pins to output pinMode(AIB, OUTPUT); pinMode(BIA, OUTPUT); pinMode(BIB, OUTPUT); }

void loop() { forward(); delay(1000); backward(); delay(1000); left(); delay(1000); right(); delay(1000); }

void backward() { analogWrite(AIA, 0); analogWrite(AIB, speed); analogWrite(BIA, 0); analogWrite(BIB, speed); }

void forward() { analogWrite(AIA, speed); analogWrite(AIB, 0); analogWrite(BIA, speed); analogWrite(BIB, 0); }

void left() { analogWrite(AIA, speed); analogWrite(AIB, 0); analogWrite(BIA, 0); analogWrite(BIB, speed); }

void right() { analogWrite(AIA, 0); analogWrite(AIB, speed); analogWrite(BIA, speed); analogWrite(BIB, 0); }

You have at least two Options.

  1. You could simply test to see if the button has been pressed and execute the code you already have in your Loop function only if the button is pressed.
  2. You could truly do a sleep for one second inside a Loop to make it sleep a total of five seconds.
    There are samples in the Forum to Show how to do whichever you choose.