-
Notifications
You must be signed in to change notification settings - Fork 0
/
IoTCloud_Sketch
109 lines (92 loc) · 2.63 KB
/
IoTCloud_Sketch
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
#include "thingProperties.h"
#include <Arduino_LSM6DSOX.h>
#include <Servo.h>
//Preset variables
int buttonCount = 0;
Servo myservo;
void setup() {
//Establish serial connection for debugging
Serial.begin(9600);
delay(1500);
//Define RGB LED outputs
pinMode(LEDR, OUTPUT);
pinMode(LEDG, OUTPUT);
pinMode(LEDB, OUTPUT);
//Prime servo in start position
myservo.attach(9);
myservo.write(10);
initProperties();
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
//init IMU library
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}
}
void loop() {
ArduinoCloud.update();
}
/*
the onRgbLightChange() function is triggered
when the rgb_light variable changes
*/
void onRgbLightChange() {
//create r,g,b variables
uint8_t r, g, b;
//retrieve values from the cloud
rgb_light.getValue().getRGB(r, g, b);
//values on Nano RP2040 Connect are inverted
//so let's remap them
int red = map(r, 0, 255, 255, 0);
int green = map(g, 0, 255, 255, 0);
int blue = map(b, 0, 255, 255, 0);
if (rgb_light.getSwitch()) {
analogWrite(LEDR, red);
analogWrite(LEDG, green);
analogWrite(LEDB, blue);
}
else {
analogWrite(LEDR, 255);
analogWrite(LEDG, 255);
analogWrite(LEDB, 255);
}
}
/*
Since Position is READ_WRITE variable, onPositionChange() is
executed every time a new value is received from IoT Cloud.
*/
void onPositionChange() {
// Add your code here to act upon Position change
myservo.write(position);
}
/*
Since DropTreat is READ_WRITE variable, onDropTreatChange() is
executed every time a new value is received from IoT Cloud.
*/
void onDropTreatChange() {
// Add your code here to act upon DropTreat change
if (drop_treat == HIGH){
if (treats_left == 0){
int pos = 180;
for (pos = 180; pos >= 10; pos -= 1) { // goes from 180 degrees to 10 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
buttonCount = 0;
treats_left = 8;
}
else {
int oldpos = 10 + buttonCount * 20;
int newpos = oldpos + 20;
int pos = oldpos;
for (pos = oldpos; pos <= newpos; pos += 1) { // goes from oldpos to newpos in 1 deg steps
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
buttonCount = buttonCount + 1;
treats_left = 8 - buttonCount;
}
}
}