Pushbutton Override noob question

Hi All,

New here with a question:

I'd like to add an override code to the program below. Using a momentary pushbutton, I'd like the override the code to turn the light on and hold until the button is pressed again, at which point it will go on to its regular programming.

I've done a lot of searching on this forum and on the web, but can't find a code. Most searches for "override code" result in some internal function to the Uno (board I'm using).

Can anyone point me in the right direction or help with writing the code?

Sorry if it's a totally newbie question, but been stuck at it for days.

Thanks!

Dweep

==============================
/* Photocell simple testing sketch.

Connect one end of the photocell to 5V, the other end to Analog 0.
Then connect one end of a 10K resistor from Analog 0 to ground

For more information see www.ladyada.net/learn/sensors/cds.html */

int photocellPin = 4; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the analog resistor divider

void setup(void) {
// We'll send debugging information via the Serial monitor
Serial.begin(9600);
pinMode(2, OUTPUT);
}

void loop(void) {
photocellReading = analogRead(photocellPin);

Serial.print("Analog reading = ");
Serial.print(photocellReading); // the raw analog reading

// We'll have a few threshholds, qualitatively determined
if (photocellReading < 10) {
Serial.println(" - Dark");
} else if (photocellReading < 200) {
Serial.println(" - Dim");
} else if (photocellReading < 500) {
Serial.println(" - Light");
} else if (photocellReading < 800) {
Serial.println(" - Bright");
} else {
Serial.println(" - Very bright");
}

//This code turns light on and off based on sensor reading
if (photocellReading < 200) {
digitalWrite(2, HIGH);
} else if (photocellReading > 400) {
digitalWrite(2, LOW);
}
delay(1000);
}

void loop(void) {
// try adding something like this here
overrideState = digitalRead (overRide);
while (overrideState == 0){
digitalWrite(2, LOW); // assuming LOW is on
overrideState = digitalRead (overRide);
}

photocellReading = analogRead(photocellPin);

@Dweep

You code look fine... but :

// you forgot those lines. And add CrossRoads code to your program.
 
int override = 10;   // that is digital pin 10

void setup()
{

  pinMode(override, INPUT); // that is digital pin 10
  pinMode(2, OUTPUT);  // That is digital pin 2  NOT analog pin 2
// the rest of the setup
}

// the rest of the program

Thanks for the responses!

I'll fix the code and then add the override stuff, see what happens :slight_smile:

Dweep