Monday, February 17, 2014

Excercise 4: Digital Input/Output

Excercise 4: Digital Input/Output

Part 1

I am beginning to appreciate Fritzing more. It is probably the clearest way for a visual learner like myself to set up these circuits.

Const int buttonpin = 2;  This initiates the buttin pin as a constant meaning it won't change
Const int ledpin =13; This initates the led pin as a constant

Void setup = this is a comand for reading the pushbutton status
pinMode (ledPin, OUTPUT); makes the led pin an output
pinMode (buttonpin, INPUT) makes the button pin an input

void loop (loops the command)
button State - digitalRead (buttonPin) = this command can detect the state of the button
if (buttonState == HIGH)
Digitalwrite (LedPin, HIGH); if button state is high, led turns on
else
Digitalwrite (ledpin, LOW); led turns off



Part 2

My circuit is set up, and I have rewritten the code so that both leds flash when I push the button:

const int buttonPin = 2;    
const int ledPin =  3;
const int led2Pin = 4;

int buttonState = 0;        
void setup() {

  pinMode(ledPin, OUTPUT); 
pinMode(led2Pin, OUTPUT);  

  pinMode(buttonPin, INPUT);     
}
void loop(){
  
  buttonState = digitalRead(buttonPin);    //I can adjust the digitalWrite HIGHS and LOWS to determine which 
leds turn on whether or not I am pushing the button
  

  if (buttonState == HIGH) {     
    digitalWrite(ledPin, HIGH);                       
  digitalWrite(led2Pin, LOW);  
  } 
  else {
    digitalWrite(ledPin, LOW);
   digitalWrite(led2Pin, HIGH); 
  }
}

My modified Digitalinout
I have now added a Pizo Speaker as an additional Output
In my code, I have included 
const int pizopin = 10
pinMode(pizopin, OUTPUT)
digitalWrite(pizopin, LOW);
digitalWrite(pizopin, HIGH);
I have now modified the code so that when I push the button, the two LEDs turn off, and the speaker buzzes. 



Part 3

I decided to use the same fritzing schematic, using the Pizo speaker. I modified the code to simulate an alarm, complete with flashing LEDs and buzzing when the button is pushed.
const int buttonPin = 2;    
const int ledPin =  3;
const int led2Pin = 4;
const int pizoPin = 10'


int buttonState = 0;        
void setup() {

pinMode(ledPin, OUTPUT); 
pinMode(led2Pin, OUTPUT);
pinMode(pizoPin, OUTPUT);  


  pinMode(buttonPin, INPUT);     
}
void loop(){
  
  buttonState = digitalRead(buttonPin);  

 if (buttonState == HIGH) {     
digitalWrite(ledPin, HIGH);
delay(250);  
digitalWrite(ledPin, LOW);
delay(250);                             
digitalWrite(led2Pin, HIGH); 
delay(250)
digitalWrite(led2Pin, LOW); 
delay(250)
digitalWrite(pizoPin, HIGH)
delay(250)
digitalWrite(pizoPin, LOW)
delay(250)    

  } 
  else {
   digitalWrite(ledPin, LOW);
   digitalWrite(led2Pin, LOW);
   digitalWrite(pizoPin, LOW); 
 
  }
}



final thoughts: While this exercise was pretty daunting, everything is coming together. I am beginning to understand the Arduino language and how it goes hand-in-hand with the fritzing environment.



No comments:

Post a Comment