-
Notifications
You must be signed in to change notification settings - Fork 0
/
sender.c
153 lines (127 loc) · 3.01 KB
/
sender.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//Reciever code for CS 122A Final Project. Fall 2019.
#include <util/delay.h>
#include <avr/io.h>
#include <usart_ATmega1284.h>
#include <timer.h>
#include <bit.h>
unsigned char stageOneInProgress = 0;
unsigned char stageTwoInProgress = 0;
unsigned char toggle = 0x00;
unsigned char L1 = 0;
unsigned char L2 = 0;
unsigned char count = 0;
unsigned char sendCount = 0;
enum states1 {send} currState1;
void sendData()
{
switch (currState1) //Actions
{
case send:
if (USART_IsSendReady(0))
{
USART_Send(sendCount, 0);
}
//Wait until done sending data
while (!USART_HasTransmitted(0)) {}
break;
}
}
enum states2 {wait, someoneEntered, increment, hold} currState2;
void checkEnter()
{
L1 = (PINB & 0x01);
L2 = (PINB & 0x02);
switch(currState2)
{
case wait:
//stageOneInProgress = 0;
if (L1 && L2) currState2 = wait;
else if (!L1 && L2) currState2 = someoneEntered;
else currState2 = wait;
break;
case someoneEntered:
//stageOneInProgress = 1;
if (!L1 && L2) currState2 = someoneEntered;
else if (!L1 && !L2) currState2 = increment;
else currState2 = wait;
break;
case increment:
//stageOneInProgress = 1;
sendCount++;
currState2 = hold;
break;
case hold:
//stageOneInProgress = 1;
if (!L1 && !L2) currState2 = hold;
else if (L1 && L2) currState2 = wait;
else currState2 = wait;
break;
default:
currState2 = wait;
}
}
enum states3 {waitLeave, someoneLeft, incrementLeave, holdLeave} currState3;
void checkLeave()
{
switch(currState3)
{
case waitLeave:
//stageTwoInProgress = 0;
if (L1 && L2) currState3 = waitLeave;
else if (L1 && !L2) currState3 = someoneLeft;
else currState3 = waitLeave;
break;
case someoneLeft:
//stageTwoInProgress = 1;
if (L1 && !L2) currState3 = someoneLeft;
else if (!L1 && !L2) currState3 = incrementLeave;
else currState3 = waitLeave;
break;
case incrementLeave:
//stageTwoInProgress = 1;
if (sendCount > 0) sendCount--;
currState3 = holdLeave;
break;
case holdLeave:
//stageTwoInProgress = 1;
if (!L1 && !L2) currState3 = holdLeave;
else if (L1 && L2) currState3 = waitLeave;
else currState3 = waitLeave;
break;
default:
currState3 = waitLeave;
}
}
unsigned char portCval = 0x00;
void checkLasersSetOutputs()
{
unsigned char LaserDetectorOne = (PINB & 0x01);
unsigned char LaserDetectorTwo = (PINB & 0x02);
if (LaserDetectorOne) portCval = SetBit(portCval, 1, 1);
else if (!LaserDetectorOne) portCval = SetBit(portCval, 1, 0);
if (LaserDetectorTwo) portCval = SetBit(portCval, 0, 1);
else if (!LaserDetectorTwo) portCval = SetBit(portCval, 0, 0);
PORTC = portCval;
}
int main(void)
{
DDRB = 0x00;
PORTB = 0xFF;
DDRC = 0xFF;
PORTC = 0x00;
initUSART(0);
TimerSet(100);
TimerOn();
currState1 = send;
currState2 = wait;
currState3 = waitLeave;
/* Replace with your application code */
while (1)
{
checkEnter();
checkLeave();
checkLasersSetOutputs();
sendData();
_delay_ms(50);
}
}