Controlling an LED using a switch

From ROBOTC API Guide
Jump to: navigation, search

Notepad.gif NOTE: This is a place holder for a video.
Please add the id for the YouTube video with description "Controlling an LED using a switch."

Concepts

So we know what a switch is, but what can we do with it?

One of the uses of a switch is to tell the controller to activate/deactivate different components. In this case we are going to use the switch to turn on and off an LED. We will do this by checking to see if the switch is pressed. When it is pressed, we will turn on the LED. when it is released, we will turn off the LED.

Wiring up the components

For this to work we will need to connect a switch as an input, to the Arduino. We will also need to connect an LED to the Arduino. This means that you will need a switch, an LED, a 10kΩ resistor, a 470Ω resistor, a blue or yellow jumper wire, and a red jumper wire.

parts for the Switch controlled LED circuit

Connecting the LED

The wiring for the LED is very similar to when we first introduced the LED.

LED schematic
LED connection on virtual breadboard
LED circuit connections in the breadboard

Connecting the switch

Now we need to connect the switch. First, let us look at the schematic of the circuit to get an idea of what we are doing.

switch schematic

Notice how the Arduino pin is connected to the circuit between the resistor and the switch. This is because the digital pin is configured such that it needs only the smallest amount of power to change states. This is useful since we don't want to draw too much power through the Arduino, from the circuit. However, this also means that if the pin is not connected to a known level in the circuit, it could have any value. This is called floating, since the value of the pin is floating.

To solve this problem we use the resistor to connect the pin to ground. We use a resistor and not just a wire so that we can limit how much current is flowing through the connection. Since we want to minimize the power draw, we use a large resistor, in this case 10kΩ. So when the switch is not pressed the pin is only connected to the ground through the resistor. Since the pin will have what ever value it applied to it, the resistor can be connsidered as a wire when the switch is not pressed. However, once the switch is pressed, the pin it directly connected to 5V, and the resistor limits the current flowing to just 0.5 mA, and thus prevents a short circuit.

Equivalent circuit when the switch is open (not pressed)
Equivalent circuit when the switch is closed (pressed)

Now that we understand how the circuit works. let us go ahead and connect the components.

LED schematic
LED connection on virtual breadboard
LED circuit connections in the breadboard

Programming

Configuring ROBOTC

Now that we have everything connected, we need to tell ROBOTC how to configure the pins before we can program. We need to tell ROBOTC that we are using the Parallax BOE Shield, and that we have the LED connected to pin 5. We also need to tell ROBOTC that pin 2 is a Digital High Impedance (the led switch) and name it "ledSwitch".

configuring the pin for the switch controlled LED

Once that is done you should have the following at the top of your source code file.

#pragma config(CircuitBoardType, typeCktBoardUNO)
#pragma config(PluginCircuitBoard, typeShieldParallaxBoeBot)
#pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0)
#pragma config(Sensor, dgtl2,  ledSwitch,      sensorDigitalHighImpedance)
#pragma config(Sensor, dgtl5,  led,            sensorLEDtoVCC)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//


Writing the Code

Now that ROBOTC knows what goes where, we can work on writing the code.

We want the Arduino to turn the LED on when the button is pressed, and turn the LED off when it is not pressed. To do this we will need an indefinite while loop, and a way to decide what to do based on the status of the switch. There are lots of different ways to do this, but for now, we will just use a second while loop that only runs when the button is pressed.

#pragma config(CircuitBoardType, typeCktBoardUNO)
#pragma config(PluginCircuitBoard, typeShieldParallaxBoeBot)
#pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0)
#pragma config(Sensor, dgtl2,  ledSwitch,      sensorDigitalHighImpedance)
#pragma config(Sensor, dgtl5,  led,            sensorLEDtoVCC)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
 
task main() 
{
  while(true)
  {
    while(the switch is pressed) //if the switch is pressed run the code in the loop
    {
      //the switch is pressed so turn the LED on.
      SensorValue[led] = 1;
    }
    //the switch is not pressed so turn the LED off
    SensorValue[led] = 0;
  }
}

We already know how to turn LEDs on and off, but how do we check the state of the button? Well, we use the following command.


SensorValue[ledSwitch] == 1

The "==" means to check if the values to either side are equal. If the values are equal, then it will return true, otherwise it will return false. So by placing that code inside the second while loop's condition, we can make that loop only run when the switch is pressed.

#pragma config(CircuitBoardType, typeCktBoardUNO)
#pragma config(PluginCircuitBoard, typeShieldParallaxBoeBot)
#pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0)
#pragma config(Sensor, dgtl2,  ledSwitch,      sensorDigitalHighImpedance)
#pragma config(Sensor, dgtl5,  led,            sensorLEDtoVCC)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
 
task main() 
{
  while(true)
  {
    while(SensorValue[ledSwitch] == 1) //if the switch is pressed run the code in the loop
    {
      //the switch is pressed so turn the LED on.
      SensorValue[led] = 1;
    }
    //the switch is not pressed so turn the LED off
    SensorValue[led] = 0;
  }
}