Flux Capacitor Lighting

So I have the opportunity to meet Christopher Lloyd next month and I want to built a Flux Capacitor for him to sign. I have found other people's sketches to control the lights and mixed them in with what I have learned. I want to be able to use a button to change between blinking modes but the motion sensor always controls if the thing is on. Ideally I would like each script to run for a set amount of time when it detects motion and that script becomes the default script the next time it detects motion.

On: LEDS chase in a pattern that speeds up as if heading towards 88mph
Press button 1 time: changes LEDs to chasing steady speed
Press button 3rd time: change LEDS to fading in and out slowly

I am also a middle school teacher and I would like to mount this on my wall, but moving lights are distracting, hence the different modes.

Currently I am getting errors about the "runlight show" not being declared in line 37 but I do not understand how this code works.

Please use small words if you have advice. I have about 5 hours into using these things so far.

You have the declaration for the runlights() function within the loop() function.
Here is the code formatted to show the problem

/*
 * PIR sensor tester
 */

int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
int buttonPin = 6;              //choose the pin for the button
int tim = 120;                  //sets time for delays
int brightness = 0;    // how bright the LED is
int fadeAmount = 1;    // how many points to fade the LED by

int ledPin1 = 9;
int ledPin2 = 6;
int ledPin3 = 5;
int ledPin4 = 3;
int surroundPin = 11;
int fadespeed = 10;
int timeTravelSpeed = 80;
int timeTravelLength = 25;

void setup()
{
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
  pinMode(buttonPin, INPUT);    //declare button as input

  //Serial.begin(9600);
}

void loop()
{
  while (digitalRead(buttonPin) == LOW)  //reads the button and uses this code
  {

    val = digitalRead(inputPin);  // read input value
    if (val == HIGH)              // check if the input is HIGH
    {
      digitalWrite(ledPin, HIGH);  // turn LED ON
      runlights(); //run the light show!
      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(ledPin, LOW); // turn LED OFF
      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;
      }
    }
  }

  void runlights()  //*** function declaration is within the loop() function ***
  {
    //Speed up loops
    for (int fadeIncrement = 20; fadeIncrement <= 100; fadeIncrement += 2)
    {
      for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += fadeIncrement)
      {
        analogWrite(ledPin1, fadeValue);
        delay(fadespeed);
      }
      for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= fadeIncrement)
      {
        analogWrite(ledPin1, fadeValue);
        delay(fadespeed);
      }
      analogWrite(ledPin1, 0);
      for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += fadeIncrement)
      {
        analogWrite(ledPin2, fadeValue);
        delay(fadespeed);
      }
      for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= fadeIncrement)
      {
        analogWrite(ledPin2, fadeValue);
        delay(fadespeed);
      }
      analogWrite(ledPin2, 0);
      for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += fadeIncrement)
      {
        analogWrite(ledPin3, fadeValue);
        delay(fadespeed);
      }
      for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= fadeIncrement)
      {
        analogWrite(ledPin3, fadeValue);
        delay(fadespeed);
      }
      analogWrite(ledPin3, 0);
      for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += fadeIncrement)
      {
        analogWrite(ledPin4, fadeValue);
        delay(fadespeed);
      }
      for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= fadeIncrement)
      {
        analogWrite(ledPin4, fadeValue);
        delay(fadespeed);
      }
      analogWrite(ledPin4, 0);
    }

    //time travel loops
    for (int timetravel = 1; timetravel <= timeTravelLength; timetravel += 1)
    {
      //turn on surround lights
      for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 15)
      {
        analogWrite(surroundPin, fadeValue);
        delay(fadespeed);
      }

      //Run time travel lights
      for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += timeTravelSpeed)
      {
        analogWrite(ledPin1, fadeValue);
        delay(fadespeed);
      }
      for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= timeTravelSpeed)
      {
        analogWrite(ledPin1, fadeValue);
        delay(fadespeed);
      }
      for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += timeTravelSpeed)
      {
        analogWrite(ledPin2, fadeValue);
        delay(fadespeed);
      }
      for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= timeTravelSpeed)
      {
        analogWrite(ledPin2, fadeValue);
        delay(fadespeed);
      }
      for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += timeTravelSpeed)
      {
        analogWrite(ledPin3, fadeValue);
        delay(fadespeed);
      }
      for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= timeTravelSpeed)
      {
        analogWrite(ledPin3, fadeValue);
        delay(fadespeed);
      }
      for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += timeTravelSpeed)
      {
        analogWrite(ledPin4, fadeValue);
        delay(fadespeed);
      }
      for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= timeTravelSpeed)
      {
        analogWrite(ledPin4, fadeValue);
        delay(fadespeed);
      }

      //fade out the surround lights
      for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 15)
      {
        analogWrite(surroundPin, fadeValue);
        delay(fadespeed);
      }
    }
    analogWrite(ledPin1, 0);
    analogWrite(ledPin2, 0);
    analogWrite(ledPin3, 0);
    analogWrite(ledPin4, 0);
  }
}
}
delay(500); //I suggest using this so it doesn't skip out of the next while loop if you hold the button a little too long

while (digitalRead(buttonPin) == LOW)
{

  //insert your solid light code here
  //once the button is pressed (==HIGH) it will stop looping and go to the next line
  digitalWrite(ledpin1 HIGH);
  delay(tim);
  digitalWrite(ledpin1, LOW);
  delay(tim);
  digitalWrite(ledpin2, HIGH);
  delay(tim);
  digitalWrite(ledpin2, LOW);
  delay(tim);
  digitalWrite(ledpin3, HIGH);
  delay(tim);
  digitalWrite(ledpin3, LOW);
  delay(tim);
  digitalWrite(ledpin4, HIGH);
  delay(tim);
  digitalWrite(ledpin4, LOW);
  delay(tim);

}

delay(500);

while (digitalRead(buttonPin) == LOW)
{

  //insert your solid light code here
  //once the button is pressed (==HIGH) it will stop looping and go to the next line

  // set the brightness of leds:
  analogWrite(ledPin1, brightness);
  analogWrite(ledPin2, brightness);
  analogWrite(ledPin3, brightness);
  analogWrite(ledPin4, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness >= 255)
  {
    fadeAmount = -fadeAmount;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(10);
}

delay(500);
}

Thanks. I was not quite sure what that meant, but I figured it out and moved my runlights code outside the loop and it worked.

Now I want to use a WHILE code to change between blink patterns. Problem is the runlights code runs about a minute and I can't figure out how to make the code look for the button. If I put the

while (digitalRead(buttonPin) == LOW)

code inside of the lightshow code so it is seen more often, then the light show stops at that code and restarts. So it never finishes the runlights code.

After hours of research I have tried a blink without delay and that did not work and I am not sure what else to do.

You people are wonderful

I have tried a blink without delay

The BWOD technique sounds like it would do what you want.
Please post the code that you tried and describe the problem

It is posted in the above, but here it is again.

Any help would be wonderful. Plus it will have pictures of the final result!

The code that you linked to (why not post it here ?) does not use millis()

Hi,
Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

Sorry, I am very new to all this. I am not sure what "mills" means. I would even take putting a toggle switch in and position 1 would run one code and position 2 another. I would rather have a push button though.
I thought I read the newbie section, sorry. At the bottom is my looping code that takes about a minute to run through. This is the code I would like to be able to push a button and exit. I have not figured out how to use the delay function as it runs and makes the lights blink progressively faster and faster.

/*
 * PIR sensor tester
 */

int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
int buttonPin = 7;              //choose the pin for the button
int tim = 120;                  //sets time for delays
int brightness = 0;    // how bright the LED is
int fadeAmount = 1;    // how many points to fade the LED by

int ledPin1 = 9;
int ledPin2 = 6;
int ledPin3 = 5;
int ledPin4 = 3;
int surroundPin = 11;
int fadespeed = 10;
int timeTravelSpeed = 80;
int timeTravelLength = 25;

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
  pinMode(buttonPin, INPUT);    //declare button as input
  pinMode(ledPin1, OUTPUT);    //you forgot to declare ledPins1-4! :P*********************************************************
  pinMode(ledPin2, OUTPUT);    //you also forgot the capitalize the P in ledPinX starting on line 63*******************************************************
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  //Serial.begin(9600);
}

void loop() {
  while (digitalRead(buttonPin) == LOW) { //reads the button and uses this code

    val = digitalRead(inputPin);  // read input value
    if (val == HIGH) {            // check if the input is HIGH
      digitalWrite(ledPin, HIGH);  // turn LED ON
      runlights(); //run the light show!
      if (pirState == LOW) {
        // we have just turned on
               pirState = HIGH;
      }
    } else {
      digitalWrite(ledPin, LOW); // turn LED OFF
      if (pirState == HIGH) {
        // we have just turned of
        pirState = LOW;
      }
    }
  }
delay(500); //so button doesnt skip

while (digitalRead(buttonPin) == LOW) { //reads the button and uses this code

  //insert your solid light code here
  //once the button is pressed (==HIGH) it will stop looping and go to the next line
  digitalWrite(ledPin1, HIGH);
  delay(tim);
  digitalWrite(ledPin1, LOW);
  delay(tim);
 

}

delay(500);

while (digitalRead(buttonPin) == LOW) {  //reads the button and uses this code

  //insert your solid light code here
  //once the button is pressed (==HIGH) it will stop looping and go to the next line

  // set the brightness of leds:
  analogWrite(ledPin1, 255);
  analogWrite(ledPin2, 150);
  analogWrite(ledPin3, 100);
  

 
}

delay(500);
}









void runlights() {
  //Speed up loops

  for (int fadeIncrement = 20; fadeIncrement <= 100; fadeIncrement += 2) {
    for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += fadeIncrement)
    {
      analogWrite(ledPin1, fadeValue);
      delay(fadespeed);
    }
   
    for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= fadeIncrement) {
      analogWrite(ledPin1, fadeValue);
      delay(fadespeed);
     
    }
    
    analogWrite(ledPin1, 0);
    for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += fadeIncrement) {
      analogWrite(ledPin2, fadeValue);
      delay(fadespeed);
    }
    for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= fadeIncrement) {
      analogWrite(ledPin2, fadeValue);
      delay(fadespeed);
    }
    analogWrite(ledPin2, 0);
    for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += fadeIncrement) {
      analogWrite(ledPin3, fadeValue);
      delay(fadespeed);
    }
    for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= fadeIncrement) {
      analogWrite(ledPin3, fadeValue);
      delay(fadespeed);
    }
    analogWrite(ledPin3, 0);
    for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += fadeIncrement) {
      analogWrite(ledPin4, fadeValue);
      delay(fadespeed);
    }
    for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= fadeIncrement) {
      analogWrite(ledPin4, fadeValue);
      delay(fadespeed);
    }
    analogWrite(ledPin4, 0);
  }
  
  //time travel loops
     for (int timetravel = 1; timetravel <= timeTravelLength; timetravel += 1) {
    //turn on surround lights
    for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 15) {
      analogWrite(surroundPin, fadeValue);
      delay(fadespeed);
    }

    //Run time travel lights
    for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += timeTravelSpeed) {
      analogWrite(ledPin1, fadeValue);
      delay(fadespeed);
    }
    for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= timeTravelSpeed) {
      analogWrite(ledPin1, fadeValue);
      delay(fadespeed);
    }
    for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += timeTravelSpeed) {
      analogWrite(ledPin2, fadeValue);
      delay(fadespeed);
    }
    for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= timeTravelSpeed) {
      analogWrite(ledPin2, fadeValue);
      delay(fadespeed);
    }
    for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += timeTravelSpeed) {
      analogWrite(ledPin3, fadeValue);
      delay(fadespeed);
    }
    for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= timeTravelSpeed) {
      analogWrite(ledPin3, fadeValue);
      delay(fadespeed);
    }
    for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += timeTravelSpeed) {
      analogWrite(ledPin4, fadeValue);
      delay(fadespeed);
    }
    for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= timeTravelSpeed) {
      analogWrite(ledPin4, fadeValue);
      delay(fadespeed);
    }

    //fade out the surround lights
    for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 15) {
      analogWrite(surroundPin, fadeValue);
      delay(fadespeed);
    }
  }
  analogWrite(ledPin1, 0);
  analogWrite(ledPin2, 0);
  analogWrite(ledPin3, 0);
  analogWrite(ledPin4, 0);
}

Hi,
How have you got your button wired.

If the button switches gnd to digital input have you got a 10K resistor between digital input and 5V, this pulls the digital input HIGH when the button is OFF.

If the button switches 5V to digital input have you got a 10K resistor between digital input and gnd, this pulls the digital input LOW when the button is OFF.

If you do not have the resistor the input will be what is called floating when your switch is OPEN, and it can be any state, HIGH or LOW or oscillating between them.

Tom... :slight_smile:
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Hey guys, thanks so much! I have not done any coding since I wrote a few web pages in wordpad back in 1998.

I installed ExpressSCH and it is a great little program for making these. In the long run it was probably faster then a ruler and pen. There are "ends" on the end of the wires on the program, not sure why they are not showing here.

Thanks again for helping getting a push button to change between cycles.

Hi,
Good to see your project is running, Express is good, I like the easy way to make custom components, and that you can edit existing ones to make new devices.

Tom.. :slight_smile: