Arduino Homework #3

I had a hard time understanding pull down and pull up resistor. I took some notes from my research to understand how it relates to our study, but hope we could go over this in class!

Digital Input

Serial Monitor

Fading Away

const int buttonPin = 2;

const int led = 9; // the PWM pin the LED is attached to
const int led2 = 10;
const int led3 = 11;

int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
int buttonState = 0; // variable for reading the pushbutton status
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(buttonPin, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
buttonState = digitalRead(buttonPin); // check if the pushbutton is pressed.
if (buttonState == HIGH){
analogWrite(led, brightness);
analogWrite(led2, brightness);
analogWrite(led3, brightness);
}

// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;

// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(10);
}

 

Attempts trying to turn on the LEDs using a button

I think i’m having a hard time with the looping that turns off one LED and then the other. I was googling to figure out how this might work but I think loop is a bit harder to do when there is a button involved.

Code 1

// constants won’t change. They’re used here to set pin numbers:
#define button 2
#define led1 12
#define led2 13
#define led3 11

int state = 0; //integer to hold current state
int old = 0; //integer to hold last state
int buttonPoll = 0; // integer to hold button state

void setup() {
// put your setup code here, to run once:
pinMode(button, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);

digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
}

void loop() {
buttonPoll = digitalRead(button); //poll the state of button
if(buttonPoll == 1){ //check if it has been pressed
delay(50); //wait 50ms
buttonPoll = digitalRead(button); //poll button again
if(buttonPoll == 0){ //if its 0 considered one press
state = old + 1; // increase state by 1
}
}
else{ // if the button has not been pressed
delay(100);
}
switch (state) {
case 1:
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
old = state; //set old state as current state
break;
case 2:
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
old = state;
break;
case 3:
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
break;
default:
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
old = 0; //reset to all off/state 0
break;
}
}

 

Code 2

const int buttonPin = 2;
const int led1 = 12;
const int led2 = 13;
const int led3 = 11;

//variables will change;

int buttonState = 0; //variable for reading the pushbutton status
int buttonPressCount = 0;
int numberOfLED = 3;

void setup(){
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH){
if (buttonPressCount % numberOfLED == 0){
digitalWrite(led1, HIGH);
}else{
digitalWrite(led1, LOW);
}
if (buttonPressCount % numberOfLED == 1){
digitalWrite(led2, HIGH);
}else{
digitalWrite(led2, LOW);
}
if (buttonPressCount % numberOfLED == 2){
digitalWrite(led3, HIGH);
}else{
digitalWrite(led3, LOW);
}
buttonPressCount++;
//delay(400);
}

}

 

Some of my notes for my own sake 🙂

Modern digital logic gates, IC’s and micro-controllers contain many inputs, called “pins” as well as one or more outputs, and these inputs and outputs need to be correctly set, either HIGH or LOW for the digital circuit to function correctly. circuits can only have one of two logic states, called the logic “0” state or the logic “1” state. (Logic Level of Pull Up:1 / Pull-down: 0)

Why???? Pullup resistors are used in electronic logic circuits to ensure that inputs to the arduino settle at expected logic levels if external devices are disconnected or high-impedance. ‘Just because you have nothing at all connected to an input pin doesn’t mean it is a logical zero.’

The difference between them is whether the resistor is connected to VCC or GND. Pull up resistor is when it’s being pulled up while it is being pulled down to connect to GND.

When a switch is closed (on), input is connected to ground and at this time, the logic level is 0.  To prevent accidental switching of digital circuits, any unconnected inputs called “floating inputs” should be tied to a logic “1” or logic “0” as appropriate for the circuit. We can easily do this by using what are commonly called Pull-up Resistors and Pull-down Resistors to give the input pin a defined default state, even if the switch is open, closed or there is nothing is connected to it.

As the input is now effectively unconnected from either a defined HIGH or LOW condition, it has the potential to “float” about between 0V and +5V (Vcc) allowing the input to self–bias at any voltage level whether that represents a HIGH or a LOW condition.  ->>>> this must be the floating stage..

——————

So it sounds like, by using what are commonly called Pull-up Resistors and Pull-down Resistors to give the input pin a defined default state, even if the switch is open, closed or there is nothing is connected to it.