STEM Across the Bay – Arduino Code

Some Arduino Code for STEM Across the Bay

Blink One LED

This code will blink a single LED for one second.

The main features of this code are:

  • pinMode(LED_BUILTIN, OUTPUT);
  • digitalWrite(LED_BUILTIN, HIGH);
  • delay(1000);
  • digitalWrite(LED_BUILTIN,LOW);

The pinMode line is there to tell the Arduino that the LED_BUILTIN pin will be used to send a signal.  This goes in the “void setup” portion of the code because it is only run once.

The digitalWrite portion will then tell the pin to be set to one of two states: HIGH or LOW.  This is equivalent to on or off, and those are the only states that are available to us in digital output.

The delay portion is just a delay.  In this case it builds in a delay of 1000 milliseconds before moving on to the next line of code.

In this code, a light will turn on for one second and go off for one second, and repeat.

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

Fade an LED with PWM

The code below will use the analog features of the Arduino to make an LED light up with different levels of brightness.  To do this we will need to attach an LED to Pin9 and march it through various levels of brightness from 0 to 255.  Here ‘0’ corresponds to all the way off, and ‘255’ corresponds to all the way on.

This code feature some constants, and some loops.  We will translate them below.

Constants

  • int led = 9;  This makes a variable (led) have an integer value of 9.
  • int brightness = 0; This makes a variable (brightness) have an integer value of 0.
  • int fadeAmount = 5;  This makes a variable (fadeAmount) have an integer value of 5.

Loop

  • brightness = brightness + fadeAmount;
    This can seem confusing.  In programming, we tend to read from right to left when it comes to equations.  So here we take the variable brightness and the variable fadeAmount to it.  So it becomes 0+5 which equals 5.  At this point brightness now has a value of 5.  The next time this loops through it will be 5+5 which equals 10.  This will repeat until brightness has a value of 255.
  • if (brightness <= 0 || brightness >=255) {fadeAmount = -fadeAmount}
    This is a check on the value of brightness.  When the value of brightness is either 0 or 255, fadeAmount will switch its sign.  For example, when brightness is 255, then fadeAmount becomes -5.  When brightness is 0, then fadeAmount becomes +5.  This is what allows the LED to fade in and out with respect to brightness.

int led = 9;           // the PWM pin the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // set the brightness of pin 9:
  analogWrite(led, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(30);
}

 

Blink an Array of LEDs

The code below will blink 6 LEDs in succession.  That is, in a string of six LEDs, the first one will go on and then off.  Then the next one, and so on.  It is a great example of a ‘for’ loop.  Lets translate this condition into English.

“for (int thisPin=2; thisPin < 8; thisPin++)”

Here thisPin is an iteration variable, and in this case it starts at the number 2.  The variable is compared to the number 8 to see if it is less than 8.  If it is, then we add 1 to the value of thisPin, and compare it to the number 8.  This repeats until thisPin has a value of 8, at which point it is no longer less than 8, and the loop is now done.

This loop is used three times.  It is used once to declare pins for output.  It is used again to light up the LEDs in one direction, and again to light up the LEDs in the opposite direction.  This makes it look like the Knight Rider LEDs from a pretty OK 80’s TV show.


int timer = 100;           // The higher the number, the slower the timing.

void setup() {
// use a for loop to initialize each pin as an output:
for (int thisPin = 2; thisPin < 8; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}

void loop() {
// loop from the lowest pin to the highest:
for (int thisPin = 2; thisPin < 8; thisPin++) { // turn the pin on: 
  digitalWrite(thisPin, HIGH); 
  delay(timer); // turn the pin off: 
  digitalWrite(thisPin, LOW); 
} 

// loop from the highest pin to the lowest: 
for (int thisPin = 7; thisPin >= 2; thisPin--) {// turn the pin on:
  digitalWrite(thisPin, HIGH);
  delay(timer);// turn the pin off:
  digitalWrite(thisPin, LOW);
}
}

Blinking the LED with a Potentiometer

In this example we will use a potientiometer (an analog device) to change the value of a variable.  That variable will determine the blink rate of an LED.  The code is pretty simple, but there are some new parts that we should look at.

Constants

  • int potPin = 2;  This is the input pin for the potentiometer
  • int ledPin = 13; This is the pin for the LED or the BUILTIN LED
  • int val = 0; This is variable that will store the value coming from the potentiometer

Setup

  • pinMode(ledPin, OUTPUT); This tells the arduino that the value of ledPin (13 in this case) will be made an OUTPUT.

Loop

  • val = analogRead(potPin);  Initially the value of val was set to 0.  Now, analogRead will read the voltage on that pin and assign to the variable val.
  • delay(val); This will create a delay that is equivalent to val in milliseconds.

int potPin = 2;    // select the input pin for the potentiometer
int ledPin = 13;   // select the pin for the LED
int val = 0;       // variable to store the value coming from the sensor

void setup() {
  pinMode(ledPin, OUTPUT);  // declare the ledPin as an OUTPUT
}

void loop() {
  val = analogRead(potPin);    // read the value from the sensor
  digitalWrite(ledPin, HIGH);  // turn the ledPin on
  delay(val);                  // stop the program for some time
  digitalWrite(ledPin, LOW);   // turn the ledPin off
  delay(val);                  // stop the program for some time
}

Leave a Reply

Your email address will not be published. Required fields are marked *