3/3/2015: Feedback and Control with Arduinos
Part 1: BlinkA continuation of Friday's short attempt at programming the Arduino, my new partner, Brooke, and I set off on assignment: blinking LED lights and patterns. A built-in LED light attached to pin 13 already existed in the Arduino board, and our first mission was to simply run a default program, "Blink" on the micro-controller, attaching the USB cord from the computer to the Arduino.
1) Blink for 2 seconds, Off for half a second
2) No Delay
3) Create a LED pattern by initiating pin 12 and pin 13
For this task, we had to set up the Arduino board and breadboard first. Following Amy's drawing on the board, we were able to set up LED light activated by pin 12.
Attempt #1: We learned that we can only have one void loop(), and that we should initialize all pins in void setup()
Attempt #2: Twiddling around with the functions more, we figured out how to allow the LEDs to blink alternatively.
Afterwards, the class was asked to set up a total of 5 LEDs, including the one already built in to the Arduino board. Again, following the diagram from above, we set up three more LEDs activated by three different pins.
4) Create a LED pattern initiating 5 pins
Experimenting with the syntax. we were able to finish all the tasks with this program!
Part 2: Blink Without Delay
While creating modified programs with the first default "Blink" program, we realized that it would not be possible to have the LEDs overlap each other as they light up, because the program read itself one line at a time. We were introduced to another default program, "Blink Without Delay", with the task of modifying the code to achieve a cool pattern.
5) Using Blink Without Delay to create a pattern with the LEDs
It took us a while to figure out what all the functions meant and how to control the duration of each LED. We fiddled around with the program, tweaking syntax and various lines of code.
Attempt at Modification
------------------------------------------------------------------------------------------------------------------
/* Blink without Delay
Turns on and off a light emitting diode (LED) connected to a digital
pin, without using the delay() function. This means that other code
can run at the same time without being interrupted by the LED code.
The circuit:
* LED attached from pin 13 to ground.
* Note: on most Arduinos, there is already an LED on the board
that's attached to pin 13, so no hardware is needed for this example.
created 2005
by David A. Mellis
modified 8 Feb 2010
by Paul Stoffregen
modified 11 Nov 2013
by Scott Fitzgerald
This example code is in the public domain.
*/
// constants won't change. Used here to set a pin number :
const int ledPin1 = 13; // the number of the LED pin
const int ledPin2 = 12;
const int ledPin3 = 8;
const int ledPin4 = 7;
const int ledPin5 = 4;
// Variables will change :
int ledState1 = LOW; // ledState used to set the LED
int ledState2 = HIGH;
int ledState3 = LOW;
int ledState4 = HIGH;
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis1 = 0; // will store last time LED was updated
unsigned long previousMillis2 = 0;
// constants won't change :
const long interval1 = 400; // interval at which to blink (milliseconds)
const long interval2 = 100;
void setup() {
// set the digital pin as output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
}
void loop()
{
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis1 = millis();
if(currentMillis1 - previousMillis1 >= interval1) {
// save the last time you blinked the LED
previousMillis1 = currentMillis1;
if (ledState1 == LOW)
ledState1 = HIGH;
else
ledState1 = LOW;
if (ledState2 == HIGH)
ledState2 = LOW;
else
ledState2 = HIGH; }
unsigned long currentMillis2 = millis();
if(currentMillis2 - previousMillis2 >= interval2) {
// save the last time you blinked the LED
previousMillis2 = currentMillis2;
// if the LED is off turn it on and vice-versa:
if (ledState4 == HIGH)
ledState4 = LOW;
else
ledState4 = HIGH;
if (ledState3 == LOW)
ledState3 = HIGH;
else
ledState3 = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin1, ledState1);
digitalWrite(ledPin2, ledState4);
digitalWrite(ledPin5, ledState3);
digitalWrite(ledPin3, ledState3);
digitalWrite(ledPin4, ledState4);
}
------------------------------------------------------------------------------------------------------------------
This program didn't work, and was only able to turn on 1 LED light. As we were working with this program, we realized we didn't set enough intervals nor enough ledState to control each LED. In addition, since we linked the more than 1 ledState to different pins, and that we only had two intervals, we found our code pretty difficult to read and follow through. We needed to find a simpler approach to not only complete our task, but to understand the program.
This program didn't work, and was only able to turn on 1 LED light. As we were working with this program, we realized we didn't set enough intervals nor enough ledState to control each LED. In addition, since we linked the more than 1 ledState to different pins, and that we only had two intervals, we found our code pretty difficult to read and follow through. We needed to find a simpler approach to not only complete our task, but to understand the program.
Program
------------------------------------------------------------------------------------------------------------------
const int ledPin1 = 13;
const int ledPin2 = 12;
const int ledPin3 = 8;
const int ledPin4 = 7;
const int ledPin5 = 4;
int ledState1 = HIGH; //setting the first LED to be off first
int ledState2 = LOW; //setting the rest of the LEDs to be on first
int ledState3 = LOW;
int ledState4 = LOW;
int ledState5 = LOW;
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;
unsigned long previousMillis4 = 0;
unsigned long previousMillis5 = 0;
const long interval1 = 100; //setting the duration of each LED
const long interval2 = 150;
const long interval3 = 200;
const long interval4 = 250;
const long interval5 = 300;
void setup() { //initiates pins
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
}
void loop()
{
unsigned long currentMillis1 = millis();
if(currentMillis1 - previousMillis1 >= interval1) { //allows the duration of HIGH or LOW to accumulate and repeat
previousMillis1 = currentMillis1;
if (ledState1 == LOW)
ledState1 = HIGH;
else
ledState1 = LOW;
}
unsigned long currentMillis2 = millis(); //allows another pin to run in an if-loop
if(currentMillis2 - previousMillis2 >= interval2) {
previousMillis2 = currentMillis2;
if (ledState2 == LOW)
ledState2 = HIGH;
else
ledState2 = LOW;
}
unsigned long currentMillis3 = millis();
if(currentMillis3 - previousMillis3 >= interval3) {
previousMillis3 = currentMillis3;
if (ledState3 == LOW)
ledState3 = HIGH;
else
ledState3 = LOW;
}
unsigned long currentMillis4 = millis();
if(currentMillis4 - previousMillis4 >= interval4) {
previousMillis4 = currentMillis4;
if (ledState4 == LOW)
ledState4 = HIGH;
else
ledState4 = LOW;
}
unsigned long currentMillis5 = millis();
if(currentMillis5 - previousMillis5 >= interval5) {
previousMillis5 = currentMillis5;
if (ledState5 == LOW)
ledState5 = HIGH;
else
ledState5 = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin1, ledState1);
digitalWrite(ledPin2, ledState2);
digitalWrite(ledPin3, ledState3);
digitalWrite(ledPin4, ledState4);
digitalWrite(ledPin5, ledState5);
}
------------------------------------------------------------------------------------------------------------------Small Reflection: It was definitely difficult to understand the code as we were modifying the program, even if it was annotated, and I'm glad we went through the many trial & errors with this program. I feel like we understand the program thoroughly, and were able to break down pieces of the program. With more time, it would have been nice to use modifications with && and || or other functions within the Arduino program.








No comments:
Post a Comment