IOT Button LED
I used Arduino’s IOT and developed a sketch in order to pair together three online buttons to the Neopixel LED lights. I had some troubles at first understanding how to implement the code into the IOT. I learned that that you have to use the on change code.
/*
Sketch generated by the Arduino IoT Cloud Thing "LeanneLED"
https://create.arduino.cc/cloud/things/f75b1218-ad43-453d-972d-ed1abb80271d
Arduino IoT Cloud Properties description
The following variables are automatically generated and updated when changes are made to the Thing properties
String lEDbutton;
bool button1;
bool button2;
bool button3;
Properties which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
#include <Adafruit_NeoPixel.h>
#define PIN 2 // input pin Neopixel is attached to
#define NUMPIXELS 30 // number of neopixels in Ring
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int delayval = 50; // timing delay
int redColor = 0;
int greenColor = 0;
int blueColor = 0;
long lastMessageTime = 0;
long messageDelay = 1000;
String lastMessageTimeStr = "";
void setup() {
// Defined in thingProperties.h
initProperties();
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
pixels.begin(); // Initializes the NeoPixel library.
// Serial.begin(9600);
}
void loop() {
ArduinoCloud.update();
// Your code here
//Serial.println("looping");
if ((millis() - lastMessageTime) > messageDelay) {
Serial.println("Update Message");
lastMessageTimeStr = String(lastMessageTime);
lEDbutton = lastMessageTimeStr;
lastMessageTime = millis();
}
for(int i=0;i<NUMPIXELS;i++){
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
pixels.setPixelColor(i, pixels.Color(redColor, greenColor, blueColor)); // Moderately bright green color.
pixels.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
// Serial.println(i);
}
} // END LOOP
void onLEDbuttonChange() {
// Do something
}
void onButton1Change() {
// Do something
Serial.println("virtual button click");
lEDbutton = "Button Color 1 Pushed";
noColor();
} // End Here
void onButton2Change() {
// Do something
lEDbutton = "Button Color 2 Pushed";
setColor();
} // End Here
void onButton3Change() {
// Do something
lEDbutton = "Button Color 3 Pushed";
setColor2();
}
void noColor() {
redColor = 0;
greenColor = 0;
blueColor = 0;
};
void setColor(){
redColor = 0;
greenColor = 255;
blueColor = 255;
Serial.print("red: ");
Serial.print("green: ");
Serial.print("blue: ");
};
void setColor2() {
redColor = 255;
greenColor = 0;
blueColor = 255;
};