This week we moved beyond the On and Off states of the digital world to further explore the 8 bit capabilities of the Arduino. This expands our 'Black and White' states to give us 128 'Shades of Grey'.
This is achieved by utilising the analogue input pins on the arduino and also by giving virtual analogue outputs by using the magic of pulse width modulation.
Pulse width modulation is basically turning something on and off very fast, in a controlled manner, to approximate a partial 'On' state. This can be useful in dimming lights and running motors at a controlled speed.
Loops enable a task to be run repeatedly. Applying 'rules' to a loop enables it to be run a certain number of times, or relative to an input or output. A simple example can be see in the following code. This code tells a light to come on when both buttons are pressed. This is a momentary response. The light will turn off when the buttons stopped being pressed.
By adding the code for (int x = 0; x < 7; x++), the loop will continue blinking the light until the number of blinks is equal to a specified number; in this case 7.
// two buttons, one LED
int signalLED = 8;
int BUTTONAPIN = 10;
int BUTTONBPIN = 12;
void setup() {
// configure led as output
// configure buttons as input
pinMode (signalLED, OUTPUT);
pinMode (BUTTONAPIN, INPUT_PULLUP);
pinMode (BUTTONBPIN, INPUT_PULLUP);
}
void loop ()
{
int button1 = !digitalRead (BUTTONAPIN);
int button2 = !digitalRead (BUTTONBPIN);
if (button1 && button2) // if, or, and statements go here
{
// blink light 7 times
for (int x = 0; x < 7; x++) {
digitalWrite (signalLED, HIGH);
delay (500);
digitalWrite (signalLED, LOW); // turns the led off
delay (300);
}
}
else {
digitalWrite (signalLED, LOW);
} //loop
Loops can also be used to incrementally control the fade of a light or the speed of a motor.
We can also achieve 'Loop Inception' by placing loops within loops.
I'm feeling dizzy just thinking about the possibilities.
This is achieved by utilising the analogue input pins on the arduino and also by giving virtual analogue outputs by using the magic of pulse width modulation.
Pulse width modulation is basically turning something on and off very fast, in a controlled manner, to approximate a partial 'On' state. This can be useful in dimming lights and running motors at a controlled speed.
Loops enable a task to be run repeatedly. Applying 'rules' to a loop enables it to be run a certain number of times, or relative to an input or output. A simple example can be see in the following code. This code tells a light to come on when both buttons are pressed. This is a momentary response. The light will turn off when the buttons stopped being pressed.
By adding the code for (int x = 0; x < 7; x++), the loop will continue blinking the light until the number of blinks is equal to a specified number; in this case 7.
// two buttons, one LED
int signalLED = 8;
int BUTTONAPIN = 10;
int BUTTONBPIN = 12;
void setup() {
// configure led as output
// configure buttons as input
pinMode (signalLED, OUTPUT);
pinMode (BUTTONAPIN, INPUT_PULLUP);
pinMode (BUTTONBPIN, INPUT_PULLUP);
}
void loop ()
{
int button1 = !digitalRead (BUTTONAPIN);
int button2 = !digitalRead (BUTTONBPIN);
if (button1 && button2) // if, or, and statements go here
{
// blink light 7 times
for (int x = 0; x < 7; x++) {
digitalWrite (signalLED, HIGH);
delay (500);
digitalWrite (signalLED, LOW); // turns the led off
delay (300);
}
}
else {
digitalWrite (signalLED, LOW);
} //loop
Loops can also be used to incrementally control the fade of a light or the speed of a motor.
We can also achieve 'Loop Inception' by placing loops within loops.
I'm feeling dizzy just thinking about the possibilities.
Comments
Post a Comment