Blink + User button example code:

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

Blink + User button example code:

Post by 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
  }


}

User avatar
aa1ww
Posts: 1
Joined: Thu Jun 09, 2016 2:01 am

Re: Blink + User button example code:

Post by aa1ww » Mon Aug 28, 2017 5:26 am

@ stmNoob0xFF , Thank You!

I have an old STM32 Design Challenge board (a.k.a. STM32 XL Density something or other) and was able to modify "File --> Examples --> 01.Basics --> Blink" and get my blue and green LEDs (PC8 & PC9, respectively) and my User Pushbutton (PA0) working thereby proving this whole thing works. This old design challenge board is only a Cortex-M3 (with an STLink w/SWD) but it has an STM32F103RGT6B with 1MB of flash and 96KB of SRAM and a lot of peripherals and being M3, runs @ up to 72MHz.

I posted my code below in case it helps somebody as clueless as myself,

Anyway,
Thanks Again,
Coop, aa1ww

Code: Select all

// ****************************************************************************************
// *                                                                                      *
// *  STM32F103R-series_Blink_0001.ino                                                    *
// *                                                                                      *
// *  Blink+                                                                              *
// *                                                                                      *
// *    Turns an Green for 1/4th LED on for one second, then off for 3/4th second,        *
// *    and if Blue User Pushbutton is depressed the Blue LED will turn on at the         *
// *    same time as the Green LED staying on for 1/2 second and off for 1/2 second       *
// *    repeatedly.                                                                       *
// *                                                                                      *
// *    This example code is in the public domain and was adapted from:                   *
// *        http://www.arduino.cc/en/Tutorial/Blink by jlcooper@stcc.edu                  *
// *                                                                                      *
// *    Note:                                                                             *
// *      I am using Board:   "Generic STM32F103R series"                                 *
// *                 Variant: "STM32F103RE (64k RAM, 512k Flash)"                         *
// *      but my actual board is an STM32 Design Challenge board using an STM103RGT6B     *
// *      with 96k RAM, 1024k Flash.  I should probably figure out how to make an option  *
// *      for this board.                                                                 *
// *                                                                                      *
// ****************************************************************************************

#define LED_BUILTIN_G PC9
#define LED_BUILTIN_B PC8
#define USER_PUSHBUTTON PA0

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN_B (PC8) &  LED_BUILTIN_B (PC8) as outputs.
  pinMode(LED_BUILTIN_G, OUTPUT);
  pinMode(LED_BUILTIN_B, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN_G, HIGH);     // turn the Green LED on  (HIGH) unconditionally.
  if (digitalRead(USER_PUSHBUTTON))
  {                                      // If the Blue User Pushbutton is depressed,
    digitalWrite(LED_BUILTIN_B, HIGH);   // turn the Blue  LED on (HIGH).
  }
  delay(250);                            // wait for a 1/4th second.
  digitalWrite(LED_BUILTIN_G, LOW);      // turn the Green LED off (LOW).
  delay(250);                            // wait for a 1/4th second.
  digitalWrite(LED_BUILTIN_B, LOW);      // turn the Blue  LED off (LOW).
  delay(500);                            // wait for a 1/2 second.
                                         // Rinse, Repeat.
}


Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests