Monday, October 24, 2016

LA 6-3 Weekly Coding Activity

This week we added a switch to our coding repertoire.  A switch is just another variable the code must account for, and it gives us an opportunity to control when one (or more) components of our physical object is receiving instructions from the computer.

I kept mine pretty simple.  The physical "switch" I used was just a piece of wire connected to my A5 pin with an alligator clip.  When I pressed that wire to my negative bus, the circuit was closed (completed) and my LED's came on.  When I removed the switch wire from the negative bus, the circuit was opened (not connected) and my LED's stayed off.



Here is what my code looked like:

int BlueLED  = 11;  
int RedLED = 3;  
int switchPin = A5;  
int switchValue;    


void setup()

{
      pinMode(BlueLED, OUTPUT);      
      pinMode(RedLED, OUTPUT);        
      pinMode(switchPin, INPUT);        
      digitalWrite(switchPin, HIGH);    
 }


 void loop()                                      
{                                                  

      switchValue = digitalRead(switchPin);        




      if (switchValue == LOW) {                    
          digitalWrite(BlueLED, HIGH);              
          digitalWrite(RedLED, HIGH);
      }                                              

      else {                                        
          digitalWrite(BlueLED, LOW);                
          digitalWrite(RedLED, LOW);
      }                                              

}

This was really the first time we were introducing input into our code (with the Arduino, anyway.)  I had to physically do something in order for my code to work.  That will open up a world of possibilities for coding in the future.

The computational thinking I am practicing when I code a switch would be: algorithms and procedures, because my switch has to have closed the circuit before any other functions of the code will work; automation, because I want the computer to always be checking the switch pin to see if the circuit is closed, I don't want to have to tell the computer to look each time I complete the circuit, I want it always looking; and it got me thinking a little about parallelization, because I wonder if there's a way to combine my LED's on different pins into one variable, so that if my circuit is closed, the code will illuminate all pins.  Hmmm....

No comments:

Post a Comment