16 in 1 LEDs & Buttons FUN
What Can be Done With Just Four Buttons & Four LEDs?
Fig 1 shows the dipswitch settings for the various modes of operation. After selecting the function on the switches, the reset button on the board should be pressed to initiate the start of the mode. Below is a quick overview of what can be expected from the functions.
Momentary Buttons
When an button is held down, its corresponding LED will illuminate.
LED Chaser Left / Right
LEDs 1- 4 sequentially shift and illuminate in (n) direction
.
LED Scanner
LEDs 1-4 sequentially shift and illuminate back and forth infinitely.
Toggle On / Off Buttons
When an button is pressed, its corresponding LED changes state.
BCD Counter UP / Down
The LEDs sequence up or down in 4-BIT binary coded decimal
.
Random LED
When any button is pressed, then one of the LEDs will illuminate at random.
Progressive LED Left / Right
The LEDs sequentially illuminate in (n) direction.
Simon Says Game
One LED at a time is illuminated and the user is expected to memorize them in order. After all of the
LEDs have been shown the user is required to repeat the sequence by pressing the corresponding
buttons.
Decision Maker Game
When any button is pressed, then one of the LEDs will illuminate at random. Label the LEDs with “Maybe”, “”YES”, “NO” and “Definitely”
Timers 1 / 3 Minute
The timer begins when an button is pressed. When the period has expired all LEDs Illuminate.
LED Flasher
All LEDs flash at a rate of about 1Hz
Alternate LED
Two of the four LEDs alternate between states at about 1Hz.
MikroC Code Here:
Parts List
PCB and hex file for PIC see attached!
What Can be Done With Just Four Buttons & Four LEDs?
Fig 1 shows the dipswitch settings for the various modes of operation. After selecting the function on the switches, the reset button on the board should be pressed to initiate the start of the mode. Below is a quick overview of what can be expected from the functions.
Momentary Buttons
When an button is held down, its corresponding LED will illuminate.
LED Chaser Left / Right
LEDs 1- 4 sequentially shift and illuminate in (n) direction
.LED Scanner
LEDs 1-4 sequentially shift and illuminate back and forth infinitely.
Toggle On / Off Buttons
When an button is pressed, its corresponding LED changes state.
BCD Counter UP / Down
The LEDs sequence up or down in 4-BIT binary coded decimal
.Random LED
When any button is pressed, then one of the LEDs will illuminate at random.
Progressive LED Left / Right
The LEDs sequentially illuminate in (n) direction.
Simon Says Game
One LED at a time is illuminated and the user is expected to memorize them in order. After all of the
LEDs have been shown the user is required to repeat the sequence by pressing the corresponding
buttons.
Decision Maker Game
When any button is pressed, then one of the LEDs will illuminate at random. Label the LEDs with “Maybe”, “”YES”, “NO” and “Definitely”
Timers 1 / 3 Minute
The timer begins when an button is pressed. When the period has expired all LEDs Illuminate.
LED Flasher
All LEDs flash at a rate of about 1Hz
Alternate LED
Two of the four LEDs alternate between states at about 1Hz.
MikroC Code Here:
Code:
// 16 in 1 LEDs 'n Buttons FUN
unsigned short nStart = 0;
unsigned short dir = 0;
unsigned short keyUp = 0;
unsigned short keyDown = 0;
unsigned short keyPress = 0;
unsigned int randomNo = 0;
unsigned short i = 0;
unsigned short j = 0;
unsigned short k = 0;
unsigned short e = 0;
unsigned short s = 0;
unsigned short simon[32]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0};
#define dSW1 PORTA.F2
#define dSW2 PORTA.F3
#define dSW3 PORTA.F0
#define dSW4 PORTA.F1
#define sw01 PORTB.F3
#define sw02 PORTB.F2
#define sw03 PORTB.F1
#define sw04 PORTB.F0
#define ld01 PORTB.F4
#define ld02 PORTB.F5
#define ld03 PORTB.F6
#define ld04 PORTB.F7
progressiveLed(int dir)
{
i++;
if (dir == 0)
{
switch (i)
{
case 1: ld01 = 1; break;
case 2: ld02 = 1; break;
case 3: ld03 = 1; break;
case 4: ld04 = 1; break;
}
}
else
{
switch (i)
{
case 1: ld04 = 1; break;
case 2: ld03 = 1; break;
case 3: ld02 = 1; break;
case 4: ld01 = 1; break;
}
}
if (i == 5)
{
i = 0;
ld01 = 0;
ld02 = 0;
ld03 = 0;
ld04 = 0;
}
Delay_ms(500);
}
void timer(int period)
{
do
{ // Wait for user to press a key
} while (sw01 == 1 && sw02 == 1 && sw03 == 1 && sw04 == 1);
// All leds on
ld01 = 1;
ld02 = 1;
ld03 = 1;
ld04 = 1;
// Timer interval
if (period == 0)
{
Delay_ms(10000); // 1 min
}
else
{
delay_ms(30000); // 3 min
}
// All leds off
ld01 = 0;
ld02 = 0;
ld03 = 0;
ld04 = 0;
}
void randomLed(int decision)
{
randomNo = rand();
if (decision == 1)
{
do
{
} while (sw01 == 1 && sw02 == 1 && sw03 == 1 && sw04 == 1);
for (j = 0; j < 10; j++)
{
for (i = 0; i < 4; i++)
{
ld01 = 0;
ld02 = 0;
ld03 = 0;
ld04 = 0;
Delay_ms(40);
switch (i)
{
case 0: ld01 = 1; break;
case 1: ld02 = 1; break;
case 2: ld03 = 1; break;
case 3: ld04 = 1; break;
}
Delay_ms(40);
}
}
}
ld01 = 0;
ld02 = 0;
ld03 = 0;
ld04 = 0;
if (randomNo < 8000)
{
ld01 = 1;
}
else if (randomNo < 16000)
{
ld02 = 1;
}
else if (randomNo < 24000)
{
ld03 = 1;
}
else
{
ld04 = 1;
}
if (decision == 0)
{
Delay_ms(500);
}
}
void upBCDCounter()
{
i++;
j = (i * 16);
PORTB = j;
if (i > 15)
{
i = 0;
}
Delay_ms(500);
}
void dnBCDCounter()
{
i++;
j = (240 - (i * 16));
PORTB = j;
if (i > 15)
{
i = 0;
}
Delay_ms(500);
}
void onOffButtons()
{
if (keyDown == 0)
{
if (sw01 == 0)
{
ld01 = ! ld01;
keyDown = 1;
}
if (sw02 == 0)
{
ld02 = ! ld02;
keyDown = 1;
}
if (sw03 == 0)
{
ld03 = ! ld03;
keyDown = 1;
}
if (sw04 == 0)
{
ld04 = ! ld04;
keyDown = 1;
}
}
if (sw01 == 1)
{
if (sw02 == 1)
{
if (sw03 == 1)
{
if (sw04 == 1)
{
keyDown = 0;
}
}
}
}
// Debounce period
Delay_ms(50);
}
void momentaryButtons()
{
Delay_ms(50); // Debounce period
ld01 =! sw01;
ld02 =! sw02;
ld03 =! sw03;
ld04 =! sw04;
}
void flasher()
{
Delay_ms(500);
ld01 = 0;
ld02 = 0;
ld03 = 0;
ld04 = 0;
Delay_ms(500);
ld01 = 1;
ld02 = 1;
ld03 = 1;
ld04 = 1;
}
void alternateFlasher()
{
Delay_ms(500);
ld01 = 1;
ld02 = 1;
ld03 = 0;
ld04 = 0;
Delay_ms(500);
ld01 = 0;
ld02 = 0;
ld03 = 1;
ld04 = 1;
}
void ledChaser(int dir)
{
if (i == 0)
{
if (dir == 0)
{
j = 16;
}
else
{
j = 128;
}
}
else
{
if (dir == 0)
{
j = j << 1;
}
else
{
j = j >> 1;
}
}
i++;
PORTB = j;
if (i == 4)
{
i = 0;
}
Delay_ms(500);
}
void ledScanner()
{
if (i == 3)
{
i = 0;
dir++;
if (dir == 2)
{
dir = 0;
}
}
if (nStart == 0)
{
j = 16;
nStart = 1;
}
PORTB = j;
if (dir == 0)
{
j = j << 1;
}
else
{
j = j >> 1;
}
Delay_ms(250);
i++;
}
void simonSays()
{
ld01 = 0;
ld02 = 0;
ld03 = 0;
ld04 = 0;
for (e = 0; e < k; e++)
{
randomNo = rand();
if (randomNo < 8000)
{
ld01 = 1;
simon[e] = 1;
}
else if (randomNo < 16000)
{
ld02 = 1;
simon[e] = 2;
}
else if (randomNo < 24000)
{
ld03 = 1;
simon[e] = 3;
}
else
{
ld04 = 1;
simon[e] = 4;
}
Delay_ms(1000);
ld01 = 0;
ld02 = 0;
ld03 = 0;
ld04 = 0;
Delay_ms(1000);
}
// User to repeat
for (e = 0; e < k; e++)
{
do
{
if (keyDown == 0)
{
if (sw01 == 0)
{
j = 1;
ld01 = 1;
keyDown = 1;
}
if (sw02 == 0)
{
j = 2;
ld02 = 1;
keyDown = 1;
}
if (sw03 == 0)
{
j = 3;
ld03 = 1;
keyDown = 1;
}
if (sw04 == 0)
{
j = 4;
ld04 = 1;
keyDown = 1;
}
}
if (keyDown == 1)
{
if (sw01 == 1)
{
if (sw02 == 1)
{
if (sw03 == 1)
{
if (sw04 == 1)
{
keyDown = 0;
keyPress = 1;
ld01 = 0;
ld02 = 0;
ld03 = 0;
ld04 = 0;
}
}
}
}
}
} while (keyPress == 0);
keyPress = 0;
Delay_ms(200);
if (simon[e] != j)
{
if (j != 0)
{
for (s = 0; s < 5; s++)
{
Delay_ms(500);
ld01 = 1;
ld02 = 1;
ld03 = 1;
ld04 = 1;
Delay_ms(500);
ld01 = 0;
ld02 = 0;
ld03 = 0;
ld04 = 0;
}
}
k = 0;
break;
}
j = 0;
}
k++;
}
void main()
{
// Configuration of ports etc ...
CMCON = 7; // Disable analog comparators
TRISA = 0x0F; // 4 inputs 4 outputs
TRISB = 0x0F; // 4 inputs 4 outputs
PORTA = 0x00; // Init port, all pins low
PORTB = 0x00; // Init port, all pins low
while(1)
{
if (dSW1 == 1 && dSW2 == 1 && dSW3 == 1 && dSW4 == 1)
{
momentaryButtons();
}
else if (dSW1 == 0 && dSW2 == 1 && dSW3 == 1 && dSW4 == 1)
{
ledChaser(0);
}
else if (dSW1 == 1 && dSW2 == 0 && dSW3 == 1 && dSW4 == 1)
{
ledChaser(1);
}
else if (dSW1 == 0 && dSW2 == 0 && dSW3 == 1 && dSW4 == 1)
{
ledScanner();
}
else if (dSW1 == 1 && dSW2 == 1 && dSW3 == 0 && dSW4 == 1)
{
onOffButtons();
}
else if (dSW1 == 0 && dSW2 == 1 && dSW3 == 0 && dSW4 == 1)
{
upBCDCounter();
}
else if (dSW1 == 1 && dSW2 == 0 && dSW3 == 0 && dSW4 == 1)
{
dnBCDCounter();
}
else if (dSW1 == 0 && dSW2 == 0 && dSW3 == 0 && dSW4 == 1)
{
randomLed(0);
}
else if (dSW1 == 1 && dSW2 == 1 && dSW3 == 1 && dSW4 == 0)
{
progressiveLed(0);
}
else if (dSW1 == 0 && dSW2 == 1 && dSW3 == 1 && dSW4 == 0)
{
progressiveLed(1);
}
else if (dSW1 == 1 && dSW2 == 0 && dSW3 == 1 && dSW4 == 0)
{
simonSays();
}
else if (dSW1 == 0 && dSW2 == 0 && dSW3 == 1 && dSW4 == 0)
{
randomLed(1); // Decision Maker
}
else if (dSW1 == 1 && dSW2 == 1 && dSW3 == 0 && dSW4 == 0)
{
timer(0);
}
else if (dSW1 == 0 && dSW2 == 1 && dSW3 == 0 && dSW4 == 0)
{
timer(1);
}
else if (dSW1 == 1 && dSW2 == 0 && dSW3 == 0 && dSW4 == 0)
{
flasher();
}
else if (dSW1 == 0 && dSW2 == 0 && dSW3 == 0 && dSW4 == 0)
{
alternateFlasher();
}
}
}
Code:
Resistors
· 9 x 100K
· 5 x 1K
Capacitors
· 2 x 22pF Ceramic
· 1 x 100nF Polyester
· 1 x 10uF Electrolytic
Semiconductors
· IC1 Pre-Programmed 16F628a
· IC2 LM78L05 Voltage Regulator
· D1 SSR 1N4007
· 4 x LED (LD1 – LD1) 3mm Red Diffused
· 1 x LED LD5 3mm Green Diffused
Miscellaneous
· IC1 Socket 18-Pin
· 5 x Tactile Buttons (SW1 – SW5)
· Slide Switch SPDT (SW6)
· 4-Way Dipswitch
· Battery Connector
· Wire Links
· XTAL 4MHz Crystal
Attachments
-
231.1 KB Views: 1,150
-
151.1 KB Views: 592
-
130.6 KB Views: 1,034
-
190.4 KB Views: 591
-
41.8 KB Views: 70
-
7.2 KB Views: 52