Digital IO Lab – Tyler Tilton

This week in lab we covered Digital input and output techniques on the Arduino. Debouncing and troubleshooting techniques are important for everyone to know when starting with Arduino.

Slow/Fast Blink

slowfast

This one was pretty basic, I just read the button state and set up an if/else statement to control blinking speed based on the button state.

I forgot at first, but its important to do digitalReads in your loop, because otherwise, the read will only happen once, and your state variable will be set just to that one state.

int button = 2; //button pin
int dly = 0; //delay variable

void setup() {
 for(int i = 8; i <= 12; i++){ //sets the pinmode of all LEDs on pins 8-12
 pinMode(i, OUTPUT);
 }
 pinMode(button, INPUT); //sets button as input
}

void loop() {
 int buttonState = digitalRead(button); //this reads the button state, 
                                        //gotta be done in the loop

 if(buttonState == LOW){
 dly = 500; //when the button is not pressed the speed is twice/second
 digitalWrite(8, HIGH);
 digitalWrite(9, HIGH);
 digitalWrite(10, HIGH);
 digitalWrite(11, HIGH);
 digitalWrite(12, HIGH);
 delay(dly);
 digitalWrite(8, LOW);
 digitalWrite(9, LOW);
 digitalWrite(10, LOW);
 digitalWrite(11, LOW);
 digitalWrite(12, LOW);
 delay(dly);
 } else {
 dly = 100; //when the button is pressed speed is 10/second
 digitalWrite(8, HIGH); 
 digitalWrite(9, HIGH);
 digitalWrite(10, HIGH);
 digitalWrite(11, HIGH);
 digitalWrite(12, HIGH);
 delay(dly);
 digitalWrite(8, LOW);
 digitalWrite(9, LOW);
 digitalWrite(10, LOW);
 digitalWrite(11, LOW);
 digitalWrite(12, LOW);
 delay(dly);
 }

}

Faster, Faster Blink

fasterfaster.gif

This one was a bit more challenging. I didn’t want to keep writing the same thing over and over again, so I ended up making a blink() function. I basically just set the LEDs to blink, and just updated the delay variable every time the button was pressed.

int button = 2; //button pin
int dly = 500; //original delay variable

void setup() {
 for(int i = 8; i <= 12; i++){
 pinMode(i, OUTPUT); //sets all LED pins to outputs
 }
 pinMode(button, INPUT); //sets button as input
}
void blink() { //this is my blink function so I don't write the same code over
 digitalWrite(8, HIGH);
 digitalWrite(9, HIGH);
 digitalWrite(10, HIGH);
 digitalWrite(11, HIGH);
 digitalWrite(12, HIGH);
 delay(dly);
 digitalWrite(8, LOW);
 digitalWrite(9, LOW);
 digitalWrite(10, LOW);
 digitalWrite(11, LOW);
 digitalWrite(12, LOW);
 delay(dly);
}

void loop() {
 int buttonState = digitalRead(button); //reads the button state

 blink(); //calls the blink function

 if(buttonState == HIGH){ //delay updater
 dly = dly * .9; //when the button is pressed, reduces the blink delay by 90%
                 //by updating the delay variable, its the only thing about 
                 //the blink function that changes
 }
}

 

Back and Forth

backandforth

Not sure why this GIF turned out the way it did……….

This one was fun, I’d actually been playing around with a similar styled loop between classes just to see what kind of patterns I could make. Originally I just had the LEDs going back and forth down the line, but with out the button. Think Knight Rider-esque. I just kinda split that apart and added button controls.

int button1 = 2; //button 1 pin
int button2 = 4; //button 2 pin
int del = 50; //delay variable

void setup() {
 for(int i = 8; i <= 12; i++){
 pinMode(i, OUTPUT); //sets all LED pins as outputs
 }
 pinMode(button1, INPUT); //sets buttons as inputs
 pinMode(button2, INPUT);
}

void blink (const byte which) { //this blink function is a lil different...
 digitalWrite(which, HIGH);
 delay(del);
 digitalWrite(which, LOW);
 delay(del);
} 

void loop() {
 int but1State = digitalRead(button1); //reads state of button 1
 int but2State = digitalRead(button2); //reads state of button 2

 if(but2State == HIGH){ //button 2 is first, I got the breadboard orientation 
                        //mixed up
   for(int i = 8; i <= 12; i++){
     blink(i);  //loops through pins and blinks individual LEDs when b2 is high
   }
 } else if(but1State == HIGH){
    for(int i = 12; i >= 8; i--){
      blink(i); //other direction for b1
  }
 }
}

So the blink function is a little different in this one than in the others. In this sketch I wanted to blink the LEDs individually, not in a group like I had done in the other sketches. So I set up the blink function to take in an number (that’s the “const byte which” part) the number is the “i” supplied by the for loops in the loop() function when blink is called. Then there’s the obvious if/else statement that adds button functionality.

A note on debouncing

I didn’t debounce my buttons in any of these sketches mostly because I didn’t think about it while I was working on it, but also because I didn’t really have any problems with bouncing on these buttons. I’m not sure why that is, but I never noticed any kind of stuttering when I was testing them. I’ll definitely have to on the next one though!

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s