Go Down

Topic: Adding an on/off button with an existing sketch (Read 1 time) previous topic - next topic

fkeel

to be honest, I did not look at the functionality of what it does.

I think you might be better off writing code from scratch.

If you tell me what *exactly* you want the code to do I can give you some more pointers.

fkeel

#6
Dec 14, 2012, 10:29 pm Last Edit: Dec 14, 2012, 10:40 pm by fkeel Reason: 1
ok, I think I know what you want to do. I played with it for a bit. This will work, assuming the button code is still ok.

make sure you understand the code though.

Code: [Select]

const int buttonPin = 2;     // the number of the pushbutton pin
int ledPin[] = {
  13, 9, 10, 11};              // pwm pins only
int ledState[5];                 // last state of each led
int lastReading = 0; // -----> added this as you forgot to initialize it
int lastButtonState = LOW; // -----> added this as you forgot to initialize it

///////////////////////////
// this is a variable which decides whether to blinklights or not
boolean blinkLEDs = true;
///////////////////////////

long randNumber;
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  for (int i=0; i<=4; i++){      // set each led pin as an output
    pinMode(ledPin[i], OUTPUT); // -----> changed from ledPin to ledPin[i]
  }
  randomSeed(analogRead(0));     // seed the rnd generator with noise from unused pin

  for (int i=0; i<=4; i++){      // init each led with a random value
    ledState[i] = random(20, 201); // -----> changed from ledPin to ledPin[i]
  }
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);
  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise: 

  // If the switch changed, due to noise or pressing:
  if (reading != lastReading) {
    // reset the debouncing timer
    lastDebounceTime = millis();
    // save the reading.  Next time through the loop,
    // it'll be lastReading:
    lastReading = reading;
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so accept the button state change:

    // toggle the LED if the state of the button changes from LOW to HIGH:
    if (lastButtonState == LOW && reading == HIGH) { 
      ////////////////////////
      ///// if blinkLEDs is true, the following code will set it to false
      ////// if blinkLEDS is flase it will be set to true.
      ////// so everytime you push the button, it will switch state
      ///////////////////////
      if (blinkLEDs == true){
        blinkLEDS = false;
      }
      else {
        blinkLEDs = true;
      }
    }
    lastButtonState = reading;
  }

  ////////////
  //if blinkLEDs is true, awesomeNebulaEffect will happen
  if (blinkLEDs == true) {
    doAwesomeNebulaEffect(); // this calls the function "doAwseomNebulaEffect"
  }
  ///////////////
  // THis is where the loop ends
}


//////////////////////
// this is a function. its called from within the loop, but the code is outside of the loop
// here we declare the function which makes the nabula effect
void doAwesomeNebulaEffect () {
  for (int i=0; i<=4; i++){                  // for each led:
    analogWrite(ledPin[i], ledState[i]);     // set the pwm value of that pin determined previously -----> its an ARRAY tell it which index you need
    randNumber = random(-40, 41);            // generate new random number and add that to the current value
    ledState[i] += randNumber;               // that range can be tweaked to change the intensity of the flickering
    if (ledState[i] > 200) {                 // clamp the limits of the pwm values so it remains within
      ledState[i] = 200;                     // a pleasing range as well as the pwm range
    }
    if (ledState[i] < 10) {
      ledState[i] = 10;
    }
  }




}



bobmcinnis

Thanks! My son is very excited and appreciative. We'll give it a try. I'll make sure I take him through the code.

bobmcinnis

Hmmm. We're getting an error:

sketch_dec14b.cpp: In function 'void loop()':
sketch_dec14b:58: error: 'blinkLEDS' was not declared in this scope

If it helps, the button code was taken from the second sketch I posted and worked great with in that one. When I tried to move it over to the  twinkle one, that's where the problems seemed to start.

Sorry, I really wasn't looking for someone to write the code for us, but under the circumstances, I can't tell you how much we appreciate it!

Arrch


sketch_dec14b:58: error: 'blinkLEDS' was not declared in this scope


Note the case difference between the error message and what's declared.

Go Up