Independent Loops with Arduino
Download serial_debugging.txt file
// Serial_Debugging
// Using serial.println to debug your code
//
// Arduino can talk back to the computer with information
// about itself using serial communications. This can be really
// handy for debugging your code by checking the value of certain
// variable while the Arduino is running.
// Set variables
int myVariable = 0;
// Setup
void setup()
{
//"Serial.begin(somenumber)" tells the Arduino to start using serial
// communications. You have to tell it what "baud" rate to use.
// "baud" (named after Emile Baudot, a pioneer in telecommuncations
// from the 19th century) is the speed at which the data flows over
// pins 0 and 1 between the Arduino board and the Arduino application.
// The baud rates must match each other to work. You set the baud rate
// in the Arduino application by clicking on the Serial Monitor button
// in the top menu in the application window and chosing the rate
// from the popup menu in the lower left of this screen. 9600 is a
// mid-range speed and is pretty reliable.
Serial.begin(9600);
}
// Loop
void loop()
{
//"Serial.print" and "Serial.println" tells the Arduino board to send
// the value of whatever is in its parenthesis to the Arduino application.
//"Serial.println" is like "Serial.print" except it adds a "line"
// each time its called.
Serial.print("The value of myVariable is: ");
Serial.println(myVariable);
myVariable = myVariable + 1;
}
Download delay-free_timing01.txt file
// Delay-Free Timing 01
//
// The Arduino has a built in clock that gives out the number of
// milliseconds the Arduino has been running since the last reset
// (it actually is a finite number, and resets to 0 after about
// 9 hours and 32 minutes). We can use this number, millis(), to
// check how long it has been since something has happened, like
// the last time an LED was turned on. This can eliminate the bottleneck
// caused by the delay() command.
// Set variables
// Set up three timers, each with a different offset to check against.
long timer1 = 0; // Timers are measured in milliseconds, which quickly get quite large.
// We'll use "long" instead of "int" because int can only store a number
// from -32,768 to 32,768, or + or - 2 to the 15th power, or 2 bytes.
// "long" extends the range to 4 bytes, or +/- 2,147,483,684
int timer1offset = 1000; // timer1offest will hold the length of time were using instead of a delay()
// Setup pins
int ledPin1 = 13;
void setup() { // The standard stuff...
Serial.begin(9600); // Setup the serial communications so we can debug
pinMode(ledPin1, OUTPUT);
timer1 = millis(); // Setting the timer to the milliseconds since the board was reset
}
// Das Loop...
void loop() {
// Timer 1
if((timer1 + timer1offset) < millis() ) { // In other words, if timer1 plus an offset, say
// 123 + 1000, is less than the current time, say 1300,
// then the timer is finished, so do something and reset
// the timer to the current time
doFlash(); // doFlash is a custom function we wrote ourselves,
// as opposed to a built-in command.
timer1 = millis();
}
}
// A new function!
void doFlash() {
Serial.println(digitalRead(ledPin1)); // Output the status to the serial port
digitalWrite(ledPin1, !digitalRead(ledPin1)); // This is a neat trick. "!" means "not"
// in Arduino language. When we say
// !digitalRead(ledPin1) we're saying
// "make this statement the opposite
// of what is says"
}
Download delay-free_timing02.txt file
// Delay-Free Timing 02
//
// In this case, we will set up a bunch of independent timers
// Set variables
// Set up three timers, each with a different offset to check against.
long timer1 = 0;
int timer1offset = 1000;
long timer2 = 0;
int timer2offset = 900;
long timer3 = 0;
int timer3offset = 800;
long timer4 = 0;
int timer4offset = 700;
long timer5 = 0;
int timer5offset = 600;
long timer6 = 0;
int timer6offset = 500;
// Setup pins
int ledPin1 = 13;
int ledPin2 = 12;
int ledPin3 = 11;
int ledPin4 = 10;
int ledPin5 = 9;
int ledPin6 = 8;
void setup() { // The standard stuff...
Serial.begin(9600); // Setup the serial communications so we can debug
// Save some vertical space by combining lines
pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT); pinMode(ledPin5, OUTPUT); pinMode(ledPin6, OUTPUT);
timer1 = millis(); timer2 = millis(); timer3 = millis();
timer4 = millis(); timer5 = millis(); timer6 = millis();
// Start with all pins on
digitalWrite(ledPin1,HIGH); digitalWrite(ledPin2,HIGH); digitalWrite(ledPin3,HIGH);
digitalWrite(ledPin4,HIGH); digitalWrite(ledPin5,HIGH); digitalWrite(ledPin6,HIGH);
}
// Das Loop...
void loop() {
if((timer1 + timer1offset) < millis() ) { //Timer 1
doLED1();
timer1 = millis();
}
if((timer2 + timer2offset) < millis() ) { //Timer 2
doLED2();
timer2 = millis();
}
if((timer3 + timer3offset) < millis() ) { //Timer 3
doLED3();
timer3 = millis();
}
if((timer4 + timer4offset) < millis() ) { //Timer 4
doLED4();
timer4 = millis();
}
if((timer5 + timer5offset) < millis() ) { //Timer 5
doLED5();
timer5 = millis();
}
if((timer6 + timer6offset) < millis() ) { //Timer 6
doLED6();
timer6 = millis();
}
}
// The doLED functions
void doLED1() {
Serial.print("LED 1: ");
Serial.println(!digitalRead(ledPin1));
digitalWrite(ledPin1,!digitalRead(ledPin1));
}
void doLED2() {
Serial.print("LED 2: ");
Serial.println(!digitalRead(ledPin2));
digitalWrite(ledPin2,!digitalRead(ledPin2));
}
void doLED3() {
Serial.print("LED 3: ");
Serial.println(!digitalRead(ledPin3));
digitalWrite(ledPin3,!digitalRead(ledPin3));
}
void doLED4() {
Serial.print("LED 4: ");
Serial.println(!digitalRead(ledPin4));
digitalWrite(ledPin4,!digitalRead(ledPin4));
}
void doLED5() {
Serial.print("LED 5: ");
Serial.println(!digitalRead(ledPin5));
digitalWrite(ledPin5,!digitalRead(ledPin5));
}
void doLED6() {
Serial.print("LED 6: ");
Serial.println(!digitalRead(ledPin6));
digitalWrite(ledPin6,!digitalRead(ledPin6));
}
