Coding a Switch in Arduino – Part One

My teacher has asked these two questions –

• Which coding concepts were introduced by coding a switch?

• Describe the computational thinking that you are practicing when creating/coding a switch:

I used a new (to me) book that I’m finding very handy to set up two types of switches, Introduction to Arduino: A piece of cake!, by Alan G. Smith, September 30, 2011.

The first is a simple one button switch:

Breadboard with one LED, one switch, one resistor. +/- are wired into right column.

Breadboard with one LED, one switch, one resistor. +/- are wired into right column.

const int Button1 = 2;
const int Led = 9;

void setup()
{
pinMode(Button1, INPUT);
digitalWrite(Button1, HIGH);
pinMode(Led, OUTPUT);
}

void loop()
{
if(digitalRead(Button1) == LOW){
digitalWrite(Led, HIGH);
}
else{
digitalWrite(Led, LOW);
}
}

• Which coding concepts were introduced by coding a switch?

Two things here:
1) HIGH = ‘On’ | high voltage & LOW = ‘Pressed ‘ | low voltage.
2) if/else statements

• Describe the computational thinking that you are practicing when creating/coding a switch:

I will be the first to admit that my brain isn’t ‘wired’ for the computational thinking that coding requires, it neither comes naturally or easily and it takes many iterations in different forms for me to start to take on concepts and show understanding.

It isn’t easy for me to divorce the physical thing that I see, the button being up or “HIGH” to the electronic reality of the circuit. It is when the button is down that the circuit is complete and ground is connected to pin 2. When it is not pushed down the circuit is from the +5V through the resistor and the Arduino sees the +5V. (HIGH). Again, I will need to this type of thing a few more times before it really takes hold.

I’m working on understanding if/else statements.

From the book: “An if statement can have an else clause which handles what should be done if the if statement isn’t true.”

If the button is pressed then turn on the LED. If not (else) keep the LED off.

Leave a Reply