Pushbutton to start and stop loop

Hi,

I have been working on my project and I want the loop to run when you push the button once. I want it to stop when you push the button again. I got the code working for the vibration motor but for some reason when i push the button the RGB LED switches to white instead of red - orange - green. Does anybody know what is going wrong?

//
#include <FastLED.h>

//connections of the ChainableLED
#define DATA_PIN 5
#define CLOCK_PIN 4
#define NUM_LEDS 1

int MoPin = 3;
boolean state = false;
int buttonpin;

//an array to hold the led data
CRGB leds[NUM_LEDS];

void setup() {
//start the led library
FastLED.addLeds<P9813, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
buttonpin=5;
pinMode(buttonpin,INPUT_PULLUP);
}

void loop() {
while(state==false){
if(digitalRead(buttonpin)==HIGH){

// Turn the LED to Green
leds[0].setRGB( 0, 255, 0);
FastLED.show();
delay(5000);

//vibrate
digitalWrite(MoPin,HIGH);
delay(500);
digitalWrite(MoPin,LOW);

// Turn LED to orange
leds[0].setRGB( 255, 50, 0);
FastLED.show();
delay(5000);

//vibrate
digitalWrite(MoPin,HIGH);
delay(500);
digitalWrite(MoPin,LOW);

// turn LED to red
leds[0].setRGB( 255, 0, 0);
FastLED.show();
delay(5000);
state=false;
}

}
}

code_probeersel.ino (980 Bytes)

Apart from failure to read the instructions before posting, you misunderstand what the loop() is. There are at least two things it should not contain, one is "delay()" and the other is "while".

Timing is performed by repeatedly checking millis() to determine intervals from one value to another, while whether particular functions are enabled is done by "if" conditions determined by "switch" values.

You need to practice with one of these functions at a time before you may combine them. Study this reference regarding timing to begin with. :grinning:

Paul__B:
before posting, you misunderstand what the loop() is. There are at least two things it should not contain, one is "delay()" and the other is "while".

For clarification, loop() can contain "delay()" as a means of testing part of the script, it can help to pause the loop completely for a defined time to identify where issues may be. The finished item as Paul points out, should be devoid of "delay()".

You can also have ‘while’ in loop(). However you have to look at the longest delay that can result from executing it. For example, you could iterate through an array of known size, performing routine operations, if it can complete in a few microseconds for example. What is not acceptable, is ‘while’ code blocks that eat up large chunks of time, or depend on external asynchronous events like serial receive.

You have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don’t do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The “Code: [Select]” feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing.
If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

aarg:
open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message.

Unless I am unknowingly dyslexic, it is the lower right of the message. :astonished:

AJLElectronics:
The finished item as Paul points out, should be devoid of "delay()".

Is this an absolute? (Genuine question!)
I get that delay() potentially waists a lot of processor time, and in more complex situations can cause problems, so it's a good habit to use other methods.
But I can also see a situation where you may want to complete one task whilst preventing all others.
Is delay() really intended to only be available as development code?

The finished item as Paul points out, should be devoid of "delay()".

idrisdraig:
Is this an absolute?

Here's what ISO says:

"should" indicates a recommendation

... so sticking that in the original quote, and paraphrasing:

It is recommended that the finished item be devoid of delay.

The problem is, that no matter how ok it looks to use delay() now in some sketch, unless you have 20/20 foresight, you may one day wish you hadn't when you try to add a feature that precludes the use of delay().

That's why it would be recommended not compulsory: unless you're in a tearing hurry it makes sense to do timing with millis() upfront, just so you don't get painted into a corner down the path.

Paul__B:
Unless I am unknowingly dyslexic, it is the lower right of the message. :astonished:

The boilerplate is old, I will change it. The forum may have changed in the time when I was away - that’s my story, and I’m sticking to it. :slight_smile:

blomcrestlight:
Here's what ISO says:

Just when I thought I was doing ok with my native language, some international body goes and standardises it behind my back. OFFS! :slight_smile: