-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathController 3
More file actions
77 lines (76 loc) · 2.13 KB
/
Controller 3
File metadata and controls
77 lines (76 loc) · 2.13 KB
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
#include "TimerOne.h"
const int NUMBER_OF_FIELDS = 4;
int fieldIndex = 0;
long int values[NUMBER_OF_FIELDS];
volatile char LEDSwitch_Row;
volatile char LEDSwitch_Column;
volatile unsigned long int period = 8000;
volatile unsigned long int pulse_time = 400;
int ledPins[] = {0, 12, 11, 2, 9, 4, 5, 6, 13, 3, 10, 7, 8};
void setup() {
timer_initialize();
Serial.begin(19200);
for(int thisPin = 2; thisPin < 14; thisPin++){
pinMode(thisPin, OUTPUT);
}
}
void timer_initialize(){
for(int i=1; i<8; i++){
digitalWrite(ledPins[i], LOW);
}
for(int i=8; i<13; i++){
digitalWrite(ledPins[i], HIGH);
}
Timer1.initialize(period);
Timer1.attachInterrupt(timer_ISR);
Serial.println("Timer initialized.");
}
void timer_ISR(){
LED(LEDSwitch_Row, LEDSwitch_Column);
delayMicroseconds(pulse_time);
digitalWrite(ledPins[LEDSwitch_Row], LOW);
digitalWrite(ledPins[LEDSwitch_Column+7], HIGH);
}
void LED(char LEDSwitch_Row, char LEDSwitch_Column){
digitalWrite(ledPins[LEDSwitch_Row], HIGH);
digitalWrite(ledPins[LEDSwitch_Column+7], LOW);
}
void loop(){
while(Serial.available()){
char ch = Serial.read();
if(ch >= '0' && ch <= '9'){
if(fieldIndex < NUMBER_OF_FIELDS){
values[fieldIndex] = (values[fieldIndex]*10) + (ch - '0');
}
}
else if(ch == ','){
fieldIndex++;
}
else{
if(1 <= values[0] <= 7 && 1 <=values[1] <= 5){
LEDSwitch_Row = values[0];
LEDSwitch_Column = values[1];
period = values[2];
pulse_time = values[3];
Serial.print("Row ");
Serial.print(values[0]);
Serial.print(", Column ");
Serial.println(values[1]);
Serial.print("Period: ");
Serial.print(values[2]);
Serial.println(" microseconds");
Serial.print("Pulse Duration: ");
Serial.print(values[3]);
Serial.println(" microseconds");
for(int i=0; i < min(NUMBER_OF_FIELDS, fieldIndex+1); i++){
values[i] = 0;
}
fieldIndex = 0;
timer_initialize();
}
else{
Serial.print("LED Position specified invalid. Please try again.");
}
}
}
}