I'm working on a project and can't seem to think of a solution to my situation. I hope the following description is clear.
I've got a queue system with four boxes (each has a sw and gr/rd leds) connected to the 16F84. If someone wants to "go", the switch is in the "next" state; otherwise all red leds are lit in the "done" state. When the switch is in the next state, the green led turns on while the red led turns off. If a box is in the next state with the green led lit and the other(s) flip their switch to next, they remain in queue with the red led remaining lit. Once the first box turns to done, the green led lights from the box that's next in line (and red turns off).
Right now, the boxes toggle left-to-right instead of going to the box that went "next" first. i.e. box1 green led is lit and box3,2,4 all go into "next" state. when box1 is done, box2 is lit, not three.
Here's my code:
I think I need to use another array to store the order, though I can't think of effective methods w/o long if statements. Any help is much appreciated. Thanks!!!
Jon
I've got a queue system with four boxes (each has a sw and gr/rd leds) connected to the 16F84. If someone wants to "go", the switch is in the "next" state; otherwise all red leds are lit in the "done" state. When the switch is in the next state, the green led turns on while the red led turns off. If a box is in the next state with the green led lit and the other(s) flip their switch to next, they remain in queue with the red led remaining lit. Once the first box turns to done, the green led lights from the box that's next in line (and red turns off).
Right now, the boxes toggle left-to-right instead of going to the box that went "next" first. i.e. box1 green led is lit and box3,2,4 all go into "next" state. when box1 is done, box2 is lit, not three.
Here's my code:
Code:
/*Inputs: PB.B0-B3
Next state: logic 0
Done state: logic 1
Outputs: Green LEDs: PB.B4-B7 Red LEDs: PA.B0-B3
LEDs on with logic 0
*/
#include <P16F84.h>
#include <delays.h>
void main()
{
TRISB = 0x0F;
TRISA = 0x00;
unsigned char order[4] = {0, 0, 0, 0};
unsigned char i;
while(1)
{
if (PB.B0 == 1 && PB.B1 == 1 && PB.B2 == 1 && PB.B3 == 1) //if all switches are in done state, turn on all red leds
{
PA.B0 = 0;
PA.B1 = 0;
PA.B2 = 0;
PA.B3 = 0;
}
if (PB.B0 == 0) //run loop if sw1 is in next state
for(i=0; i<=3; i++)
{
if (order[i] == 0 && order[i] != 1) //if in first loop, declare sw1 next to be order 1
order[i] == 1;
break;
}
if (PB.B1 == 0) //run loop if sw2 is in next state
for(i=0; i<=3; i++)
{
if (order[i] == 0 && order[i] != 2) //if in first loop, declare sw2 next to be order 2
order[i] == 2;
break;
}
if (PB.B2 == 0) //run loop if sw3 is in next state
for(i=0; i<=3; i++)
{
if (order[i] == 0 && order[i] != 3) //if in first loop, declare sw3 next to be order 3
order[i] == 3;
break;
}
if (PB.B3 == 0) //run loop if sw4 is in next state
for(i=0; i<=3; i++)
{
if (order[i] == 0 && order[i] != 4) //if in first loop, declare sw4 next to be order 4
order[i] == 4;
break;
}
if (order[0] = 1) //order 1
{
while (PB.B0 != 1) { //verifying sw is not in done state
PB.B4 = 0; //turning on green led
PB.B5 = 1; //turn off all other green leds
PB.B6 = 1;
PB.B7 = 1;
PA.B0 = 1; //turn off red led
PA.B1 = 0; //turn on all other red leds
PA.B2 = 0;
PA.B3 = 0; };
}
if (order[0] = 2) //order 2
{
while (PB.B1 != 1) {
PB.B5 = 0;
PB.B4 = 1;
PB.B6 = 1;
PB.B7 = 1;
PA.B1 = 1;
PA.B0 = 0;
PA.B2 = 0;
PA.B3 = 0; };
}
if (order[0] = 3) //order 3
{
while (PB.B2 != 1) {
PB.B6 = 0;
PB.B4 = 1;
PB.B5 = 1;
PB.B7 = 1;
PA.B2 = 1;
PA.B1 = 0;
PA.B0 = 0;
PA.B3 = 0; };
}
if (order[0] = 4) //order 4
{
while (PB.B3 != 1) {
PB.B7 = 0;
PB.B4 = 1;
PB.B5 = 1;
PB.B6 = 1;
PA.B3 = 1;
PA.B1 = 0;
PA.B2 = 0;
PA.B0 = 0; };
}
order[0] = order[1]; //first element assigned to order 1
order[1] = order[2]; //order 1 assigned to order 2
order[2] = order[3];
order[3] = 0; //clear array
PORTA = 1; //turn off all leds
PB.B4 = 1;
PB.B5 = 1;
PB.B6 = 1;
PB.B7 = 1;
};
}
Jon