Buttoms codes

Hello! I have an adafruit-gemma and a arduino puchbuttom and wounder if someone will help me with a code if thats possible. :slight_smile:

The whole thing is for lightning some rgb leds.

I want it to be 3 step on the buttom, when you press it one time it will just light up white
When you press it a second time it will flash and Third time it will go to off. Can someone help me whit this :)? I am a really beginner at this.
And sorry for my bad english :relaxed:

What have you tried so far? Have you looked at any of the example codes? Have you tried any of them?

No i have't look at any example codes. Where can i find them? Like i said, i'm a really beginner at this

Eriix:
No i have't look at any example codes. Where can i find them? Like i said, i'm a really beginner at this

Try Google There are also several example sketches that come with the IDE found under the File - Examples menu. Then of course there is this site. Up at the top of this page, see where it says Learning? Click that.

Okey I can look there but if someone have an code or can write an simpel code for that now i will appreciate it

Eriix:
Okey I can look there but if someone have an code or can write an simpel code for that now i will appreciate it

They've already been written. If we had to rewrite every example for every person who starts using the Arduino we'd never have time for anything else. If you're too lazy to do the search or even click on the link I gave you that does the search for you, then I guess you're not going to get very far with this.

im basically where you are at.. this tut starts with basic button commands, im working on a midi controller and having some issues, watching this has helped a lot so far.

Something like this:

enum State {
  OFF, ON, FLASHING;
} state;

const int buttonPin = 3;
const int ledPin = 3;
const unsigned long flashTimeMs = 250;

int buttonState;

unsigned long flashMs;
booean flashState;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  state = OFF;
  buttonState = digitalRead(buttonPin);
}

void loop() {
  int buttonStateNow = digitalRead(buttonPin);
  boolean buttonPressed = (buttonState=HIGH) && (buttonStateNow == LOW);
  
  switch(state) {
  case OFF:
    if(buttonPressed) {
      digitalWrite(ledPin, HIGH);
      state = ON;
    }
    else {
      ; // do nothing
    }
    break; 

  case ON:
    if(buttonPressed) {
      digitalWrite(ledPin, LOW);
      state = FLASHING;
      flashMs = millis();
      flashState = false;
    }
    else {
      ; // do nothing
    }
    break; 

  case FLASHING:
    if(buttonPressed) {
      digitalWrite(ledPin, LOW);
      state = OFF;
    }
    else {
      flash_the_led();
    }
    break; 
  }


  buttonState = buttonStateNow;
}

void flash_the_led() {
  if(millis() - flashMs < flashTimeMs) return;
  digitalWrite(ledPin, flashState = !flashState);
  flashTimeMs = millis();
}

PaulMurrayCbr:
Something like this:

enum State {

OFF, ON, FLASHING;
} state;

const int buttonPin = 3;
const int ledPin = 3;
const unsigned long flashTimeMs = 250;

int buttonState;

unsigned long flashMs;
booean flashState;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  state = OFF;
  buttonState = digitalRead(buttonPin);
}

void loop() {
  int buttonStateNow = digitalRead(buttonPin);
  boolean buttonPressed = (buttonState=HIGH) && (buttonStateNow == LOW);
 
  switch(state) {
  case OFF:
    if(buttonPressed) {
      digitalWrite(ledPin, HIGH);
      state = ON;
    }
    else {
      ; // do nothing
    }
    break;

case ON:
    if(buttonPressed) {
      digitalWrite(ledPin, LOW);
      state = FLASHING;
      flashMs = millis();
      flashState = false;
    }
    else {
      ; // do nothing
    }
    break;

case FLASHING:
    if(buttonPressed) {
      digitalWrite(ledPin, LOW);
      state = OFF;
    }
    else {
      flash_the_led();
    }
    break;
  }

buttonState = buttonStateNow;
}

void flash_the_led() {
  if(millis() - flashMs < flashTimeMs) return;
  digitalWrite(ledPin, flashState = !flashState);
  flashTimeMs = millis();
}

SHHH your not supposed to give him the easy way out… hes supposed to watch the hours of tutorials like the rest of us :stuck_out_tongue: PaulS wouldnt help me with code… just vague tips witch are not much help to a noob :stuck_out_tongue:

Eriix:
No i have't look at any example codes. Where can i find them? Like i said, i'm a really beginner at this

Many programming examples are included in the Arduino-IDE and you can find them under the "File" menu option.

Programming example about a button reading the "HIGH" or "LOW" state:
File - Examples - Digital - Button

But what you want is to detect "when a button is pressed down", this is in technical speak a "state change detection". This example is to be found at:
File - Exemples - Digital - StateChangeDetection

RobTapps:
SHHH your not supposed to give him the easy way out.. hes supposed to watch the hours of tutorials like the rest of us :stuck_out_tongue: PaulS wouldnt help me with code.. just vague tips witch are not much help to a noob :stuck_out_tongue:

I do hope you're kidding. :slight_smile: One thing we might agree about - I rarely get anything useful from videos. They're usually just a show-and-tell of some code and wiring that I could read about more effectively.

It's almost axiomatic in the learning business that you have to do some things yourself or you won't learn. In computing, information gathering is an important skill. To do it effectively, you have to take control and not just let Google steer you to some crappy wanna be demos on YouTube.

I have been ramping up on Arduino for about 2-3 months now. I find videos useless, but built in examples are good to build on and pretty well documented. (File>examples) I express caution, getting libs outside of the core libs that come with Arduino 1.x and newer. Usually causes more problems than is worth it for the starter. Save those for the experienced. Unlike other boards, the Arduino forum, can be a hostile place, where arrogance reigns and code samples are sparse. I have received good support from the forum, once they understood, I didn’t have a clue…