So. Willie and made the Arduino blink. But not just blink. Blink and count up from 1 to 5, then start again.
So,
Blink.
Then Blink Blink.
Then Blink Blink Blink, etc.
Dig?
We got a little hung up with the nested for loops, but with a little help from our friends, we made it happen.
---
Here's the code:
// blink counting from 1 blink to 5
int counter = 0; // a counter for using with my blinking loop
int led = 13; // the led pin
void setup() {
pinMode(led, OUTPUT);
}
// my blink function
void blink(int g) {
for (int x = 0; x <= g; x++) {
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(200);
}
}
// my loop!
void loop() {
for (int counter = 0; counter < 5; counter++) {
blink(counter);
delay(800);
}
}
As computing becomes more ubiquitous in our objects, designers need to be more aware of how to design meaningful interactions into electronically enhanced objects. At the University of Washington, a class of junior Interaction Design majors is exploring this question. These pages chronicle their efforts.
Tuesday, April 30, 2013
The GIFt of Light
SAM: William ... come see thine latest creation
WILLIE: Ser Samuel what discovery of wonder and creation of magic hath you delivered here today?!
S: It ish a curcuit my simple friend. One that uses both the simple LED circuitry I have shown you previously and a three-post variable potentiometer -- a dial, Ser William -- to control the brightness of said light.
W: fffaassssssccinating Samuel
S: But there is more! As I control this system and output feed is transfered back to my computer operating system, displaying to me the changes in intensity and resistance of energy.
W: I am frozen in a state of amazement!
-------------------------------------
Below is a GIF of
a) the process in motion
b) sam represents what the LED is doing
c) willie represents what the dial is doing to control the LED
d) a gif of hands to represent the motion of electrons in our circuit
Our Code looks like this:
/*
Analog input, analog output, serial output
Reads an analog input pin, maps the result to a range from 0 to 255
and uses the result to set the pulsewidth modulation (PWM) of an output pin.
Also prints the results to the serial monitor.
The circuit:
* potentiometer connected to analog pin 0.
Center pin of the potentiometer goes to the analog pin.
side pins of the potentiometer go to +5V and ground
* LED connected from digital pin 9 to ground
created 29 Dec. 2008
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
// These constants won't change. They're used to give names
// to the pins used:
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);
// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(2);
}
Monday, April 29, 2013
AnalogRead Serial.print and light sensors
For this portion I was taking the components we were looking at in class and adding elements of the serial reading and printing in order to get a sense of what was going on behind the scenes. Did run into a problem where the data was scrolling side ways but it turned out that I needed to add a Serial.println(); which would start a new line.
Being able to see the read out helped me adjust the value so I got the best range for the LED which was One fourth of the analog readout.
Being able to see the read out helped me adjust the value so I got the best range for the LED which was One fourth of the analog readout.
code below
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int sensorPin = A0;
int ledPin = 9;
int sensorValue = 0;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // starts communication with computer
}
// the loop routine runs over and over again forever:
void loop() {
sensorValue = analogRead(sensorPin);
analogWrite(ledPin, sensorValue/4 );
Serial.print("sensor = " );
Serial.print(sensorValue/4);
Serial.println();
}
5 Blink ~ Nick Stoermer
Five blink sequence
For the brute force it was pretty straight forward groups of on/off separated by delays. For the more effective method I ended up using encapsulation to have a blink function which I could then pass a variable which would define how many time it blinked. I then put this inside a for loop which controlled the variable that was passed. I realized that this project could have been done with just nested for loops but for the purpose of learning the building blocks of Arduino this was more important.
code below
~~~~~~~~~~~
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
//1
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(300);
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(300);
delay(1000) // 1 sec pause for inbetween
//2
digitalWrite(led, HIGH);
delay(300);
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(300);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(300);
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(300);
delay(1000) // 1 sec pause for inbetween
//3
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(300);
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(300);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(300);
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(300);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(300);
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(300);
delay(1000) // 1 sec pause for inbetween
//4
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(300);
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(300);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(300);
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(300);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(300);
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(300);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(300);
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(300);
delay(1000) // 1 sec pause for inbetween
//5
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(300);
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(300);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(300);
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(300);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(300);
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(300);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(300);
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(300);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(300);
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(300);
delay(1000) // 1 sec pause for inbetween
// wait for a second
}
Pressure Sensors & Amy's Ego...
Pressure Sensor:
Our sensor was a paper thin strip with a circle pad that was a pressure sensor. The harder you squeeze the brighter the bulb got, but it never turned all the way off upon release.
Analog In/Out Serial: "Amy is ____ Beautiful"
Our sensor was a paper thin strip with a circle pad that was a pressure sensor. The harder you squeeze the brighter the bulb got, but it never turned all the way off upon release.
int sensorPin = A0;int ledPin = 9;int sensorValue = 0;
void setup(){ pinMode(ledPin, OUTPUT);}
void loop() { sensorValue = analogRead(sensorPin); sensorValue = sensorValue/3; analogWrite(ledPin, sensorValue); //delay();}
Analog In/Out Serial: "Amy is ____ Beautiful"
*/
// the pins used:
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
int count = 0;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
count = count + 1;
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);
// print the results to the serial monitor:
//Serial.print("sensor = " );
//Serial.print(sensorValue);
//Serial.print("\t output = ");
Serial.print("Amy is");
Serial.print(count);
Serial.println( "x beautiful");
// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(2);
}
Saturday, April 27, 2013
Serial Analog Output of Footstep Pacing — Mallika Simone, Sarah Churng
Quick recap:
Now we are tasked to combine the AnalogInOutSerial sketch with some kind of analog input. For our Drunk Walking Situation, Mallika and I have decided to explore the detection of pacing associated with inebriation."Left foot, right foot...lerf toot,,rghithoorshf... ..."
We have two Force Pressure Sensors (FPI) for measuring footsteps. Each FPI has a range of pressure sense for 0–100 lbs of pressure. The harder the footstep, the more force detected, and, by our setup, the more its corresponding LED lights up. The code takes analog inputs from A0 (Left foot) and A1 (Right foot), and produces output to the variable pins 10 and 9._A LEFT footstep lights up GREEN. _A RIGHT footstep lights up YELLOW.
Pressing on the right foot initiates the yellow LED light. |
How We Intend to Use this Data:
- PACING can be computed as a function of the inputs over time
- BALANCE can be derived through comparisons between the two inputs' pressure resistances
Action shots
Testing it with a sensor inside a sock. (Heel location works best.) Testing it with pacing: |
The code:
/*
AUTHORS: sarah churng; mallika simone
SUMMARY: Measures the force exerted in walking.
Reads in analog input via Force Pressure Sensors (FPS) from
pins A0 and A1, maps the results to 2 respective ranges from 0
to 255, and uses the result to set the pulsewidth modulation
of the output pins 9 and 10.
CIRCUITS:
1* FPS connected to analog pin A0.
Center pin of the potentiometer goes to the analog pin.
side pins of the potentiometer go to +5V and ground
* LED connected from digital pin 10 to ground
2* FPS connected to analog pin A1.
Center pin of the potentiometer goes to the analog pin.
side pins of the potentiometer go to +5V and ground
* LED connected from digital pin 9 to ground
USE AND POTENTIAL for the data collected:
* PACING: as a function of the inputs over time
* BALANCE: as a comparison between the two inputs' pressure resistances
*/
// ANALOG DATA: Left Foot
const int analogInPin1 = A0; // Force Pressure Sensor at A0
const int analogOutPin1 = 10; // LED at variable pin 10
int sensorValue1 = 0; // value from variable sensor
int outputValue1 = 0; // value from output to analog out
// ANALOG DATA: Right Foot
const int analogInPin2 = A1; // Force Pressure Sensor at A1
const int analogOutPin2 = 9; // LED at variable pin 9
int sensorValue2 = 0; // value from variable sensor
int outputValue2 = 0; // value from output to analog out
// INITIALIZE serial communications at 9600 bps
void setup(){
Serial.begin(9600);
}
void loop(){
// READ: analog values
sensorValue1 = analogRead(analogInPin1); // Read in input from left foot
outputValue1 = map(sensorValue1, 0, 1023, 0, 255); // map to range of analog 0 - 255
analogWrite(analogOutPin1, outputValue1); // change the analog out value
sensorValue2 = analogRead(analogInPin2); // Read in input from right foot
outputValue2 = map(sensorValue2, 0, 1023, 0, 255); // map to range of analog 0 - 255
analogWrite(analogOutPin2, outputValue2); // change the analog out value
// PRINT: outputs to serial monitor
if ( sensorValue1 = 0 ){ // If left foot is resting,
Serial.print("Left Foot = 0" ); // print 0 values
Serial.println("\t\t\t\tLeft force = 0");
}
if ( sensorValue2 = 0 ){ // If right foot is resting,
Serial.print("\t\tRight Foot = 0" ); // print 0 values
Serial.println("\t\t\t\tRight force = 0");
}
else{
Serial.print("Left Foot = " ); // When exerts pressure,
Serial.print(sensorValue1); // print left pressure and analog out
Serial.print("\t\t\t\tLeft force = ");
Serial.println(outputValue1);
Serial.print("\t\tRight Foot = " ); // When right foot exerts pressure,
Serial.print(sensorValue2); // print right pressure and analog out
Serial.print("\t\t\t\tRight force = ");
Serial.println(outputValue2);
}
// WAIT
delay(500); // Wait half a second for next loop.
}
Fading LED with Photoresistor : Kristina & Karin
Fading LED with Photoresistor
Kristina & Karin
*/
int sensorPin = 0; // select the input pin for the potentiometer
int sensorValue = 0; // variable to store the value coming from the sensor
int setPoint = 255; //trigger value
int ledPin = 13;
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
pinMode (sensorPin, INPUT);
Serial.begin (255); //setup serial
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
Serial.println (sensorValue);
if (sensorValue < setPoint) {
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
}
else {
// turn the ledPin off:
digitalWrite(ledPin, LOW);
}
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}
Friday, April 26, 2013
Kristina & Karin: struggling but not giving up...help needed later.
Count Sketch w/ Semi-button
AHHHH. ok. So, this code will blink up to 5 when button is pressed. When up to 5, we can't get it turn off with a button, but it'll do it automatically.. We will keep working on the forloop. More updates this weekend.
/*
Blinks count to 5
Kristina - Karin
*/
int buttonPin = 2; // the number of the pushbutton pin
int led = 13; // LED connected
int val = 0;
int state = 0;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop(){
val = digitalRead(buttonPin); // turn count on
if (val == HIGH) {
//ONE
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(300); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1300); // wait for a second
//TWO
digitalWrite(led, HIGH); // one
delay(300); //wait
digitalWrite(led, LOW); // off
delay(300); //wait
digitalWrite(led, HIGH); // two
delay(300); //wait
digitalWrite(led, LOW); // off
delay(1300); //wait
//THREE
digitalWrite(led, HIGH); // one
delay(300); //wait
digitalWrite(led, LOW); // off
delay(300); //wait
digitalWrite(led, HIGH); // two
delay(300); //wait
digitalWrite(led, LOW); // off
delay(300); //wait
digitalWrite(led, HIGH); //three
delay(300); //wait
digitalWrite(led, LOW); // off
delay(1300); //wait
//FOUR
digitalWrite(led, HIGH); // one
delay(300); //wait
digitalWrite(led, LOW); // off
delay(300); //wait
digitalWrite(led, HIGH); // two
delay(300); //wait
digitalWrite(led, LOW); // off
delay(300); //wait
digitalWrite(led, HIGH); // three
delay(300); //wait
digitalWrite(led, LOW); // off
delay(300); //wait
digitalWrite(led, HIGH); //four
delay(300); //wait
digitalWrite(led, LOW); // off
delay(1300); //wait
//FIVE
digitalWrite(led, HIGH); // one
delay(300); //wait
digitalWrite(led, LOW); // off
delay(300); //wait
digitalWrite(led, HIGH); // two
delay(300); //wait
digitalWrite(led, LOW); // off
delay(300); //wait
digitalWrite(led, HIGH); // three
delay(300); //wait
digitalWrite(led, LOW); // off
delay(300); //wait
digitalWrite(led, HIGH); //four
delay(300); //wait
digitalWrite(led, LOW); // off
delay(300); //wait
digitalWrite(led, HIGH); //five
delay(300); //wait
digitalWrite(led, LOW); // off
delay(1300); //wait
}
}
Sometimes we get this error message when we plug the arduino into either of our computers and we are unsure what exactly is causing it:
Here's our set-up:
Thursday, April 25, 2013
Randy & Charlie: Count Arduino
Count Blink with For Loop
Count Blink with Brute Force
int ledPin = 13; // LED connected to digital pin 13
int count = 1; // Keeps count of which number it's on
void setup() {
pinMode(ledPin, OUTPUT); // set pin 13 as output
}
void loop() {
for (int i = 0; i < count; i++) {
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(200);
}
count++; // increments count
if (count > 5) { // if count reaches 5, then start over from 1
count = 1;
}
delay(1500); // delay between next blinking sequence
}
Count Blink with Brute Force
int ledPin = 13; // LED connected to digital pin 13
int count = 1; // Keeps count of which number it's on
void setup() {
pinMode(ledPin, OUTPUT); // set pin 13 as output
}
void loop() {
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(200);
delay(1500); // delay between next blinking sequence
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(200);
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(200);
delay(1500); // delay between next blinking sequence
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(200);
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(200);
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(200);
delay(1500); // delay between next blinking sequence
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(200);
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(200);
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(200);
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(200);
delay(1500); // delay between next blinking sequence
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(200);
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(200);
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(200);
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(200);
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(200);
delay(1500); // delay between next blinking sequence
}
Counting code with button pressing action!!
For our Arduino code, we decided to attach the brute force 5 blink counting code with the button pushing code. This way when you press the button, the code will start the blink counting. This will continue in a loop forever until you press the button again to turn the blinking off. So basically we made and ON/OFF to count to five.
Here's the extremely un-elegant code with the button pressing action:
const int buttonPin = 9; // the number of pushbutton pin
const int led = 13; // the number of LED pin
// variables that will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// Pushbutton pin as the input
pinMode(buttonPin, INPUT);
// Make the LED pin an output
pinMode(led, OUTPUT);
}
void loop(){
buttonState = digitalRead (buttonPin); // read the state of the pushbutton value:
if (buttonState == HIGH) { // BUTTON PRESS
//BLINK ONCE
digitalWrite(led, HIGH); // ONE
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off
delay(1000);
//BLINK TWICE
digitalWrite (led, HIGH); // ONE
delay(500); // wait for a second
digitalWrite (led, LOW); // turn the LED off
delay(500); // wait for a second
digitalWrite(led, HIGH); // TWO
delay(500); // wait for a second
digitalWrite(led, LOW); // turn the LED off
delay(1000); // wait for a second
//BLINK THREE TIMES
digitalWrite(led, HIGH); // ONE
delay(500); // wait for a second
digitalWrite(led, LOW); // turn the LED off
delay(500); // wait for a second
digitalWrite(led, HIGH); // TWO
delay(500); // wait for a second
digitalWrite(led, LOW); // turn the LED off
delay(500); // wait for a second
digitalWrite(led, HIGH); // THREE
delay(500); // wait for a second
digitalWrite(led, LOW); // turn the LED off
delay(1000); // wait for a second
//BLINK FOUR TIMES
digitalWrite(led, HIGH); // ONE
delay(500); // wait for a second
digitalWrite(led, LOW); // turn the LED off
delay(500); // wait for a second
digitalWrite(led, HIGH); // TWO
delay(500); // wait for a second
digitalWrite(led, LOW); // turn the LED OFF
delay(500); // wait for a second
digitalWrite(led, HIGH); // THREE
delay(500); // wait for a second
digitalWrite(led, LOW); // turn the LED off
delay(500); // wait for a second
digitalWrite(led, HIGH); // FOUR
delay(500); // wait for a second
digitalWrite(led, LOW); // turn the LED off
delay(1000); // wait for a second
//BLINK FIVE TIMES
digitalWrite(led, HIGH); // ONE
delay(500); // wait for a second
digitalWrite(led, LOW); // turn the LED off
delay(500); // wait for a second
digitalWrite(led, HIGH); // TWO
delay(500); // wait for a second
digitalWrite(led, LOW); // turn the LED OFF
delay(500); // wait for a second
digitalWrite(led, HIGH); // THREE
delay(500); // wait for a second
digitalWrite(led, LOW); // turn the LED off
delay(500); // wait for a second
digitalWrite(led, HIGH); // FOUR
delay(500); // wait for a second
digitalWrite(led, LOW); // turn the LED off
delay(500); // wait for a second
digitalWrite(led, HIGH); // FIVE
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off
}
//Push the button again
else {
digitalWrite(led, LOW); // LIGHT OFF
}
}
Here is the code for the more elegant way of doing the counting loop, but we didn't include the button push:
const int led = 13; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
pinMode(led, OUTPUT);
}
void loop() {
int i = 1; //counter variable
int j = 1; //counter variable
for(i=1; i<6; ++i){ // tells the LED to blink 5 times
for(j=1; j<=i; ++j){ //tells the LED to restrict the number of blinks to the value of i/j
digitalWrite(led, HIGH); // 1 Flash
delay(200);
digitalWrite(led, LOW);
delay(400);
}
delay(1000);
}
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(led, ledState);
}
}
Here's the extremely un-elegant code with the button pressing action:
const int buttonPin = 9; // the number of pushbutton pin
const int led = 13; // the number of LED pin
// variables that will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// Pushbutton pin as the input
pinMode(buttonPin, INPUT);
// Make the LED pin an output
pinMode(led, OUTPUT);
}
void loop(){
buttonState = digitalRead (buttonPin); // read the state of the pushbutton value:
if (buttonState == HIGH) { // BUTTON PRESS
//BLINK ONCE
digitalWrite(led, HIGH); // ONE
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off
delay(1000);
//BLINK TWICE
digitalWrite (led, HIGH); // ONE
delay(500); // wait for a second
digitalWrite (led, LOW); // turn the LED off
delay(500); // wait for a second
digitalWrite(led, HIGH); // TWO
delay(500); // wait for a second
digitalWrite(led, LOW); // turn the LED off
delay(1000); // wait for a second
//BLINK THREE TIMES
digitalWrite(led, HIGH); // ONE
delay(500); // wait for a second
digitalWrite(led, LOW); // turn the LED off
delay(500); // wait for a second
digitalWrite(led, HIGH); // TWO
delay(500); // wait for a second
digitalWrite(led, LOW); // turn the LED off
delay(500); // wait for a second
digitalWrite(led, HIGH); // THREE
delay(500); // wait for a second
digitalWrite(led, LOW); // turn the LED off
delay(1000); // wait for a second
//BLINK FOUR TIMES
digitalWrite(led, HIGH); // ONE
delay(500); // wait for a second
digitalWrite(led, LOW); // turn the LED off
delay(500); // wait for a second
digitalWrite(led, HIGH); // TWO
delay(500); // wait for a second
digitalWrite(led, LOW); // turn the LED OFF
delay(500); // wait for a second
digitalWrite(led, HIGH); // THREE
delay(500); // wait for a second
digitalWrite(led, LOW); // turn the LED off
delay(500); // wait for a second
digitalWrite(led, HIGH); // FOUR
delay(500); // wait for a second
digitalWrite(led, LOW); // turn the LED off
delay(1000); // wait for a second
//BLINK FIVE TIMES
digitalWrite(led, HIGH); // ONE
delay(500); // wait for a second
digitalWrite(led, LOW); // turn the LED off
delay(500); // wait for a second
digitalWrite(led, HIGH); // TWO
delay(500); // wait for a second
digitalWrite(led, LOW); // turn the LED OFF
delay(500); // wait for a second
digitalWrite(led, HIGH); // THREE
delay(500); // wait for a second
digitalWrite(led, LOW); // turn the LED off
delay(500); // wait for a second
digitalWrite(led, HIGH); // FOUR
delay(500); // wait for a second
digitalWrite(led, LOW); // turn the LED off
delay(500); // wait for a second
digitalWrite(led, HIGH); // FIVE
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off
}
//Push the button again
else {
digitalWrite(led, LOW); // LIGHT OFF
}
}
Here is the code for the more elegant way of doing the counting loop, but we didn't include the button push:
const int led = 13; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
pinMode(led, OUTPUT);
}
void loop() {
int i = 1; //counter variable
int j = 1; //counter variable
for(i=1; i<6; ++i){ // tells the LED to blink 5 times
for(j=1; j<=i; ++j){ //tells the LED to restrict the number of blinks to the value of i/j
digitalWrite(led, HIGH); // 1 Flash
delay(200);
digitalWrite(led, LOW);
delay(400);
}
delay(1000);
}
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(led, ledState);
}
}
Noble + Gavia | Blinking LED and Button Switch
Here's the code for incrementing the LED up to five blinks. I merged it with the code for the button so every time you turn the LED on with the button it also runs the blinking loop.
/* Button Controlled Increment Blink to Five Hack */
#define LED 13 // the pin for the LED
#define BUTTON 7 // the input pin where the pushbutton is connected
int counter = 1;
int i;
int val = 0; // val will be used to store the state of the input pin
int old_val = 0; // this variable stores the previous value of "val"
int state = 0; // 0 = LED off and 1 = LED on
void setup() {
pinMode(LED, OUTPUT); // tell Arduino LED is an output
pinMode(BUTTON, INPUT); // and BUTTON is an input
}
void loop() {
val = digitalRead(BUTTON); // read input value and store it
if ((val == HIGH) && (old_val == LOW)){
state = 1 - state;
counter = 0; // Reset the counter so that you can run the for loop again when you press the button
delay(10);
}
old_val = val; // val is now old, let's store it
if (state == 1) { // If the button is telling the LED to turn on, run the blink loop
while(counter <= 5) {
for (i = 0; i < counter; i++) {
digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level)
delay(250); // wait
digitalWrite(LED, LOW); // turn the LED off by making the voltage LOW
delay(250); // wait
}
delay(1000);
counter++;
}
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
}
/* Button Controlled Increment Blink to Five Hack */
#define LED 13 // the pin for the LED
#define BUTTON 7 // the input pin where the pushbutton is connected
int counter = 1;
int i;
int val = 0; // val will be used to store the state of the input pin
int old_val = 0; // this variable stores the previous value of "val"
int state = 0; // 0 = LED off and 1 = LED on
void setup() {
pinMode(LED, OUTPUT); // tell Arduino LED is an output
pinMode(BUTTON, INPUT); // and BUTTON is an input
}
void loop() {
val = digitalRead(BUTTON); // read input value and store it
if ((val == HIGH) && (old_val == LOW)){
state = 1 - state;
counter = 0; // Reset the counter so that you can run the for loop again when you press the button
delay(10);
}
old_val = val; // val is now old, let's store it
if (state == 1) { // If the button is telling the LED to turn on, run the blink loop
while(counter <= 5) {
for (i = 0; i < counter; i++) {
digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level)
delay(250); // wait
digitalWrite(LED, LOW); // turn the LED off by making the voltage LOW
delay(250); // wait
}
delay(1000);
counter++;
}
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
}
Subscribe to:
Posts (Atom)