-
Notifications
You must be signed in to change notification settings - Fork 4
/
bitcoinMachineServo.ino
61 lines (49 loc) · 1.45 KB
/
bitcoinMachineServo.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
#include <aJSON.h>
#include <Servo.h>
aJsonStream serial_stream(&Serial);
Servo myservo;
int pos = 0;
void setup(void) {
Serial.begin(9600);
myservo.attach(9);
}
void loop(void) {
//recieve commands
while (serial_stream.available())
{
aJsonObject *msg = aJson.parse(&serial_stream);
processMessage(msg); //see api.ino
aJson.deleteItem(msg);
}
delay(10); //just here to slow down the output so it is easier to read
}
/* ==================================================================
Expects something like this over serial:
{"buy": "1.0"}
*/
void processMessage(aJsonObject *msg)
{
aJsonObject *c = aJson.getObjectItem(msg, "buy");
if (c)
{
char* d = c->valuestring;
float b = atof(d);
int i = b;
if (i == 1) {
//stepper motor
moveServo();
}
}
}
void moveServo() {
for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=0; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}