Adding a Switch

COURSE FEATURES

  • Duration 8:24
  • Hardware Wire
  • Hardware 7 Segment LED
  • Hardware 595 Shift register
  • Hardware SPDT Switch

ABOUT THE COURSE

Switches are probably the most crucial part of the build. Everything from the start & finish lines are switches, to every button on the betting station. Learning how they work is a very important part of the program.

What You Will Learn

In this tutorial, we will hook up a switch to the 7 segment LED, and shift register we previously built.



Video #4

int datapin = 13;
int latchpin = 12;
int clockpin = 11;

switchpin = 8;

byte number[] = { 0b11111100, 0b01100000, 0b10110110, 0b11100110, 0b01101010, 0b11001110 };

void setup()
{
	pinMode(datapin, OUTPUT);
	pinMode(latchpin, OUTPUT);
	pinMode(clockpin, OUTPUT);
	pinMode(switchpin, INPUT);
}

void loop()
{
	digitalWrite(latchpin, LOW);
	shiftOut(datapin, clockpin, MSBFIRST, number[0]);
	digitalWrite(latchpin, HIGH);
	
	if (digitalRead(switchpin) == HIGH)
	{
		count++;
		delay(500);
	}
	
	if (count > 5)
	{
		count = 0;
	}

}


Review

In my derby I use 54 switches. 5 for the start line, 5 for the finish line, 10 on each betting station for betting input, and 1 on each station for cash out.

Here is an image of the 5 switches used for the starting and 5 for the finishing line.


This is the switch used.


The buttons for betting and cash out are also switches.



Here is the other side of the button showing the switch. It looks a little different, but the wiring and coding are the same.


Bottom line is all switches have 3 wires; one for power, one for ground, and one for signal. They all may look a little different, but they all work the same.