-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharduino_library.ino
128 lines (114 loc) · 4.17 KB
/
arduino_library.ino
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
/*
*
* - RD13 -> Reads the Digital input at pin 13
* - RA4 - > Reads the Analog input at pin 4
* - WD13:1 -> Writes 1 (HIGH) to digital output pin 13
* - WA6:125 -> Writes 125 to analog output pin 6 (PWM)
*/
char operation; // Holds operation (R, W, ...)
char mode; // Holds the mode (D, A)
int pin_number; // Holds the pin number
int digital_value; // Holds the digital value
int analog_value; // Holds the analog value
int speed_value; // Holds the converted analog value
int value_to_write; // Holds the value that we want to write
int wait_for_transmission = 5; // Delay in ms in order to receive the serial data
void set_pin_mode(int pin_number, char mode){
/*
* Performs a pinMode() operation depending on the value of the parameter
* mode :
* - I: Sets the mode to INPUT
* - O: Sets the mode to OUTPUT
* - P: Sets the mode to INPUT_PULLUP
*/
switch (mode){
case 'I':
pinMode(pin_number, INPUT);
break;
case 'O':
pinMode(pin_number, OUTPUT);
break;
case 'P':
pinMode(pin_number, INPUT_PULLUP);
break;
}
}
void digital_read(int pin_number){
/*
* Performs a digital read on pin_number and returns the value read to serial
* in this format: D{pin_number}:{value}\n where value can be 0 or 1
*/
digital_value = digitalRead(pin_number);
Serial.print('D');
Serial.print(pin_number);
Serial.print(':');
Serial.println(digital_value); // Adds a trailing \n
}
void analog_read(int pin_number){
/*
* Performs an analog read on pin_number and returns the value read to serial
* in this format: A{pin_number}:{value}\n where value ranges from 0 to 1023
*/
analog_value = analogRead(pin_number);
speed_value = map(analog_value, 0, 1023, 0, 120);
Serial.print('A');
Serial.print(pin_number);
Serial.print(':');
Serial.println(analog_value); // Adds a trailing \n
}
void digital_write(int pin_number, int digital_value){
/*
* Performs a digital write on pin_number with the digital_value
* The value must be 1 or 0
*/
digitalWrite(pin_number, digital_value);
}
void analog_write(int pin_number, int analog_value){
/*
* Performs an analog write on pin_number with the analog_value
* The value must be range from 0 to 255
*/
analogWrite(pin_number, analog_value);
}
void setup() {
Serial.begin(9600); // Serial Port at 9600 baud
Serial.setTimeout(100); // Instead of the default 1000ms, in order
// to speed up the Serial.parseInt()
}
void loop() {
// Check if characters available in the buffer
if (Serial.available() > 0) {
operation = Serial.read();
delay(wait_for_transmission); // If not delayed, second character is not correctly read
mode = Serial.read();
pin_number = Serial.parseInt(); // Waits for an int to be transmitted
if (Serial.read()==':'){
value_to_write = Serial.parseInt(); // Collects the value to be written
}
switch (operation){
case 'R': // Read operation, e.g. RD12, RA4
if (mode == 'D'){ // Digital read
digital_read(pin_number);
} else if (mode == 'A'){ // Analog read
analog_read(pin_number);
} else {
break; // Unexpected mode
}
break;
case 'W': // Write operation, e.g. WD3:1, WA8:255
if (mode == 'D'){ // Digital write
digital_write(pin_number, value_to_write);
} else if (mode == 'A'){ // Analog write
analog_write(pin_number, value_to_write);
} else {
break; // Unexpected mode
}
break;
case 'M': // Pin mode, e.g. MI3, MO3, MP3
set_pin_mode(pin_number, mode); // Mode contains I, O or P (INPUT, OUTPUT or PULLUP_INPUT)
break;
default: // Unexpected char
break;
}
}
}