Home > Projects > Arduino Programmer Projects > Making a program start using a switch

Making a program start using a switch

Summary of Making a program start using a switch


This article explains how to integrate a switch into an Arduino project using ROBOTC. It details the physical wiring of a switch, jumper wires, and a 10kΩ resistor on a breadboard. The guide then covers configuring pins for a Parallax BOE Shield, including two continuous rotation servos and a digital input named "startSwitch". Finally, it provides C code demonstrating a loop where the robot drives forward, stops, and reverses, utilizing the switch status to control program execution flow.

Parts used in the Arduino Switch Project:

  • Switch
  • Two jumper wires
  • 10kΩ (brown-black-orange) resistor
  • Breadboard
  • Arduino
  • Parallax BOE Shield
  • Continuous Rotation Servo (left)
  • Reversed Continuous Rotation Servo (right)

Connecting the Switch

To connect the switch to the Arduino, you will need the switch, two jumper wires, and a 10kΩ (brown-black-orange) resistor.

Once you have all the required parts, you will need to wire the switch to the Arduino using the breadboard.

NOTE: The virtual board may be slightly different from the physical one. This is because of some limitations of the program used to create the virtual layout. Both the physical and virtual layout will work the same way.

Making a program start using a switch

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 drive servos connected to pins 10 and 11. We also need to tell ROBOTC that pin 2 is a digital input (the start switch) and name it “startSwitch”.

  • Parallax Boe Shield
  • Continuous Rotation Servo named “leftServo” on pin 10
  • reversed Continuous Rotation Servo named “rightServo” on pin 11
  • Digital High Impedance named “startSwitch” on pin 2

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,  startSwitch, sensorDigitalHighImpedance)
#pragma config(Motor, servo_10, LeftServo, tmotorServoContinuousRotation, openLoop, IOPins, dgtl10, None)
#pragma config(Motor, servo_11, RightServo, tmotorServoContinuousRotation, openLoop, reversed, IOPins, dgtl11, None)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

Making a program start using a switch schematic

Programming

For this project we just want to demonstrate how the switch can be used to start a program. To keep it simple, we will just have the robot drive forwards, backwards, and stop. Since we already have code to drive forwards and backwards, we will just copy that code into the current file and modify it.

task main()
{

  while(true) //repeat indefinitely
  {
    // code to drive forward for 2 seconds
    motor[leftServo] = 20;   //Set the left servo to go forward at power level 20
    motor[rightServo] = 20;  //Set the right servo to go forward at power level 20
    wait1Msec(2000);         //pause code execution for 2000ms (2 seconds)

    // code to stop the robot for 1 second
    motor[leftServo] = 0;    //Set the left servo stop
    motor[rightServo] = 0;   //Set the right servo stop
    wait1Msec(1000);         //pause code execution for 1000ms (1 second)

    // code to drive backwards for 2 seconds
    motor[leftServo] = -20;  //Set the left servo to go backwards at power level -20 (absolute power of 20)
    motor[rightServo] = -20; //Set the right servo to go backwards at power level -20 (absolute power of 20)
    wait1Msec(2000);         //pause code execution for 2000ms (2 seconds)

    // code to stop the robot for 1 second
    motor[leftServo] = 0;    //Set the left servo stop
    motor[rightServo] = 0;   //Set the right servo stop
    wait1Msec(1000);         //pause code execution for 1000ms (1 second)
  }
}

To read the status of the switch we use the command

 

For more detail: Making a program start using a switch

Quick Solutions to Questions related to Arduino Switch Project:

  • What components are needed to connect the switch?
    You need the switch, two jumper wires, and a 10kΩ resistor.
  • How do you configure pin 2 in ROBOTC?
    Pin 2 is configured as a Digital High Impedance sensor named startSwitch.
  • Which pins are assigned to the drive servos?
    The left servo is connected to pin 10 and the right servo to pin 11.
  • Can I use a virtual board instead of a physical one?
    Yes, both physical and virtual layouts work the same way despite minor visual differences.
  • What does the main task loop make the robot do?
    The robot drives forward for 2 seconds, stops for 1 second, drives backward for 2 seconds, and stops again.
  • How do you stop the servos in the code?
    Servo power is set to 0 to stop the motors.
  • What command reads the status of the switch?
    The article indicates a specific command is used to read the switch status, though the exact syntax is represented by a placeholder in the text.
  • What is the baud rate configured for UART_Usage?
    The baud rate is set to 200000.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter
Scroll to Top