Weekly Journal 2 – (Janosch Spohner) – Digital I/O

Digital I/O

Spencer and I worked together to create the three effects by writing code in the Arduino software. Below are Gifs of the effects happening along with screenshots of the code.

 

General Code Setup

For each of the effects, our code for  {void set up} was very similar. We made constant integer variables (just variables that can’t be changed) for the LED’s and the button and then one or two integer variables for the delay function called timers. In set up we used a for loop to designate all the LED’s as Outputs for their specific pin and then we did it for the button as well. The {loop} portion varied and for each effect so I will describe it under each effect separately.

Slow/ Fast Blink

Slow-Fast gif.gif

const int led1 = 8;
const int led2 = 9;
const int led3 = 10;
const int led4 = 11;
const int led5 = 12;

const int btn = 2;

int timer1 = 500;
int timer2 = 100;

void setup() {
  for (int pin = led1; pin <= led5; pin++) {
    pinMode(pin, OUTPUT);
  }

  pinMode(btn, INPUT);
}

void loop() {
  int btnValue = digitalRead(btn);

  if (btnValue == HIGH) {
    for (int pin = led1; pin <= led5; pin++) {
      digitalWrite(pin, HIGH);
    }
    delay(timer2);
    for (int pin = led5; pin >= led1; pin--) {
      digitalWrite(pin, LOW);
    }
    delay(timer2);
  }

  for (int pin = led1; pin <= led5; pin++) {
    digitalWrite(pin, HIGH);
  }
  delay(timer1);

  for (int pin = led5; pin >= led1; pin--) {
    digitalWrite(pin, LOW);
  }
  delay(timer1);
}

In the loop portion of this code we have a variable called btnValue that keeps track of whether or not the button is pressed. Then based off whether or not the button is pressed we ran some different for loops. In the case that the button is not pressed, there is a for loop that turns on all the LED’s, then there is a delay of timer1 (500 ms), then the LED’s are all turned off and there is another delay, creating a flashing effect. This same pattern is run if the button is pressed however we use timer2 (100 ms) to create the faster flashing effect.

 

Faster, Faster Blink

Faster, Faster gif.gif

const int led1 = 8;
const int led2 = 9;
const int led3 = 10;
const int led4 = 11;
const int led5 = 12;

const int btn = 2;
int timer = 500; 

void setup() {

  for (int pin = led1; pin <= led5; pin++) {
    pinMode(pin, OUTPUT);
  }

  pinMode(btn, INPUT);

}

void loop() {
  int btnValue = digitalRead(btn);

  if (btnValue == HIGH) {
    timer = timer*.9;
    }

  for (int pin = led1; pin <= led5; pin++) {
      digitalWrite(pin, HIGH);
    }
    delay(timer);
    for (int pin = led5; pin >= led1; pin--) {
      digitalWrite(pin, LOW);
    }
    delay(timer);
}

For this code we the same pattern as above to create the flashing effect. This time we only have one of the patterns and the flashing speed is all based on the timer variable. We define timer to be 500 ms above, however every time the button is pressed the timer becomes 10% less (500 ms > 450 ms > 405 ms > 364.5 ms). This makes the flashing faster every time the button is pressed and when held (as shown in the video) the flashing get faster exponentially until we cannot see the flashing anymore because it is happening so fast.

Back and Forth

Back and Forth gif.gif

const int led1 = 8;
const int led2 = 9;
const int led3 = 10;
const int led4 = 11;
const int led5 = 12;

const int btn1 = 2;
const int btn2 = 3;

int timer = 50;

void setup() {
  // put your setup code here, to run once:

  for (int pin = led1; pin <= led5; pin++) {
    pinMode(pin, OUTPUT);
  }

  pinMode(btn1, INPUT);
  pinMode(btn2, INPUT);

}

void loop() {
  int btnValue1 = digitalRead(btn1);
  int btnValue2 = digitalRead(btn2);

  if (btnValue1 == HIGH) {
    for (int pin = led5; pin >= led1; pin--) {
      digitalWrite(pin, HIGH);
      delay(timer);
      digitalWrite(pin, LOW);
    }
  }
  if (btnValue2 == HIGH) {
    for (int pin = led1; pin <= led5; pin++) {
      digitalWrite(pin, HIGH);
      delay(timer);
      digitalWrite(pin, LOW);
    }
  }
}

This code is a little different because we used two buttons but things are still pretty similar. We have re-used the pattern that makes LED’s flash and based it off of two if statements for the buttons. When button 1 is pressed the LED’s flash one way and when button 2 is pressed the LED’s flash the other way. This is super easy to do since all we had to do was make some small changes to the for loops in the flashing pattern code.

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