-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparkcore.rgbled.cpp
More file actions
62 lines (53 loc) · 1.25 KB
/
sparkcore.rgbled.cpp
File metadata and controls
62 lines (53 loc) · 1.25 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
// name the pins
int led1 = A4;
int led2 = A5;
int led3 = A6;
void setup()
{
//Register our Spark function here
Spark.function("led", ledControl);
// Configure the pins to be outputs
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
// Initialize LEDs to be OFF
analogWrite(led1, 255);
analogWrite(led2, 255);
analogWrite(led3, 255);
}
void loop()
{
// nothing here?
}
int ledControl(String command)
{
//find out the pin number and convert the ascii to integer
int pinNumber = command.charAt(0) - '0';
//Sanity check to see if the pin numbers are within limits
if (pinNumber < 0 || pinNumber > 4) return -1;
// convert the string to an int
// the command to send to API would be
// curl https://api.spark.io/v1/devices/0123456789abcdef01234567/led \
// -d access_token=1234123412341234123412341234123412341234 \
// -d params=1,100
// params are (123),(0-255)
String x = command.substring(2);
int state = x.toInt();
if (pinNumber == 1)
{
analogWrite(led1, state);
}
else if (pinNumber == 2)
{
analogWrite(led2, state);
}
else if (pinNumber == 3)
{
analogWrite(led3, state);
}
else
{
return -1;
}
return 1;
}