Blink + User button example code:

Post your cool example code here.
stmNoob0xFF
Posts: 5
Joined: Mon Aug 01, 2016 2:23 pm

Blink + User button example code:

Postby stmNoob0xFF » Mon Aug 01, 2016 3:37 pm

Hi!

I only recently joined this forum, and finally got my STM32F4 - Discovery to link in (last night in fact!).

I've had this board for at least 3 years now, and always wanted to get it running in the Arduino IDE as this board has some amazing looking features, not to mention the sheer amount of onboard GPIO.

Anyhow, I quickly wrote up a bit of code, and thought I'd share it, so that other people that are just starting out at least have something to test with or understand how to get something happening from the outset.

(I understand there maybe other examples, but I thought I would share this anyway.

This code turns on the four LEDs on or off in order. Upload the sketch and try it out!

I hope this is useful for someone.

(I have labelled everything up so that people can clearly see what happens in each line).

Enjoy!


Code: Select all

//Counter + Delay

int counter = 0;                                                           //Set the counter to zero (This is so that we can cycle through the LED array and other things
int delTime = 30;                                                          //Delay timer to debounce User button presses

//userButton - Using the onboard User Button

int userButton = PA0;                                                      //User Button = PA0 on Discovery
int buttonState = 0;                                                       //Set the buttonState to 0
int buttonMem = 0;                                                         //Set the buttonMem to 0 (This is to store the last userButton press)

//leds - Using the onboard LEDs
//LED colour / PIN
int greenLed = PD12;                                                       //Green      / PD12
int orangeLed = PD13;                                                      //Orange     / PD13
int redLed = PD14;                                                         //Red        / PD14
int blueLed = PD15;                                                        //Blue       / PD15

//LED Array

int ledArray[5] = {greenLed, orangeLed, redLed, blueLed};                  //Setup of LED array


void setup() {

  pinMode(greenLed, OUTPUT);                                               //Set Green LED pin to output
  pinMode(orangeLed, OUTPUT);                                              //Set Orange LED pin to output
  pinMode(redLed, OUTPUT);                                                 //Set Red LED pin to output
  pinMode(blueLed, OUTPUT);                                                //Set Blue LED pin to output

  pinMode(userButton, INPUT);                                              //Set User button pin as input

}


void loop() {

  buttonState = digitalRead(userButton);                                   //Read the userButton

  if (buttonState == HIGH) {                                               //If the User button is pressed, then..
    if (buttonMem == 0) {                                                  //..check to see if the User button is still pressed. If it isn't, then..
      digitalWrite(ledArray[counter], HIGH);                               //Switch on whichever LED the counter has reached in the array
      buttonMem = 1;                                                       //After the last press, flag that this has happened so that it doesn't cycle on and off
      delay(delTime);                                                      //Delay timer to debounce User button press
    }
  }

  if (buttonState == LOW) {                                                //If the User button is released, then..
    if (buttonMem == 1) {                                                  //..check to see if the User button is still released. If it isn't, then..
      digitalWrite(ledArray[counter], LOW);                                //Switch off whichever LED the counter has reached in the array
      buttonMem = 0;                                                       //After the last release, flag that this has happened so that it doesn't cycle on and off
      counter = counter + 1;                                               //Step the counter up by 1
    }
    delay(delTime);                                                         //Delay timer to debounce User button release                                                   
  }


  if (counter == 4) {                                                       //If the counter has reached the end of the LED array, then..
    counter = 0;                                                            //Reset the counter to zero to start over
  }


}

Return to “Code snipplets”

Who is online

Users browsing this forum: No registered users and 3 guests