Fade
This commit is contained in:
parent
d386dc6699
commit
bf41a23cf0
5 changed files with 100 additions and 72 deletions
4
include/fade.h
Normal file
4
include/fade.h
Normal file
|
@ -0,0 +1,4 @@
|
|||
#include <LEDStripe.h>
|
||||
|
||||
|
||||
void fade(LEDStripe* led);
|
0
lib/LEDStripe/LEDStripe.c
Normal file
0
lib/LEDStripe/LEDStripe.c
Normal file
16
lib/LEDStripe/LEDStripe.h
Normal file
16
lib/LEDStripe/LEDStripe.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
#ifndef PUBLIC_H_INCLUDED
|
||||
#define PUBLIC_H_INCLUDED
|
||||
struct LEDStripe {
|
||||
int MOD; //0 = off, 1 = on, 2 = fade ...
|
||||
int OLD_MOD;
|
||||
int SPEED;
|
||||
int RED;
|
||||
int GREEN;
|
||||
int BLUE;
|
||||
int LOOP;
|
||||
int LOOPCOUNT;
|
||||
int MOD_STEP;
|
||||
}
|
||||
typedef LEDStripe;
|
||||
|
||||
#endif /* PUBLIC_H_INCLUDED */
|
53
src/fade.cpp
Normal file
53
src/fade.cpp
Normal file
|
@ -0,0 +1,53 @@
|
|||
#include<LEDStripe.h>
|
||||
|
||||
void fade(LEDStripe* led)
|
||||
{
|
||||
if(led->MOD_STEP == 0) {
|
||||
led->RED ++;
|
||||
led->GREEN = 0;
|
||||
led->BLUE = 255;
|
||||
if(led->RED >= 255) {
|
||||
led->MOD_STEP++;
|
||||
}
|
||||
}
|
||||
if(led->MOD_STEP == 1) {
|
||||
led->RED = 255;
|
||||
led->GREEN = 0;
|
||||
led->BLUE --;
|
||||
if(led->BLUE <= 0) {
|
||||
led->MOD_STEP++;
|
||||
}
|
||||
}
|
||||
if(led->MOD_STEP == 2) {
|
||||
led->RED = 255;
|
||||
led->GREEN ++;
|
||||
led->BLUE = 0;
|
||||
if(led->GREEN >= 255) {
|
||||
led->MOD_STEP++;
|
||||
}
|
||||
}
|
||||
if(led->MOD_STEP == 3) {
|
||||
led->RED --;
|
||||
led->GREEN = 255;
|
||||
led->BLUE = 0;
|
||||
if(led->RED <= 0) {
|
||||
led->MOD_STEP++;
|
||||
}
|
||||
}
|
||||
if(led->MOD_STEP == 4) {
|
||||
led->RED = 0;
|
||||
led->GREEN = 255;
|
||||
led->BLUE ++;
|
||||
if(led->BLUE >= 255) {
|
||||
led->MOD_STEP++;
|
||||
}
|
||||
}
|
||||
if(led->MOD_STEP == 5) {
|
||||
led->RED = 0;
|
||||
led->GREEN --;
|
||||
led->BLUE = 255;
|
||||
if(led->GREEN <= 0) {
|
||||
led->MOD_STEP = 0;
|
||||
}
|
||||
}
|
||||
}
|
99
src/main.cpp
99
src/main.cpp
|
@ -1,6 +1,8 @@
|
|||
#include <Arduino.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <LEDStripe.h>
|
||||
#include <fade.h>
|
||||
|
||||
#define REDPIN1 5
|
||||
#define GREENPIN1 4
|
||||
|
@ -24,29 +26,19 @@ PubSubClient client(espClient);
|
|||
|
||||
|
||||
|
||||
struct LEDStripe {
|
||||
String MOD = "off"; //Which mode is used "off", "fade", "on"
|
||||
String OLD_MOD = "off";
|
||||
int SPEED = 3000;
|
||||
int RED = 0;
|
||||
int GREEN = 0;
|
||||
int BLUE = 0;
|
||||
int LOOP = 0;
|
||||
int LOOPCOUNT = 0;
|
||||
int MOD_STEP = 0;
|
||||
};
|
||||
|
||||
|
||||
int Sync = 0; //Sync LED2 on the value of LED1
|
||||
|
||||
LEDStripe led1;
|
||||
LEDStripe led2;
|
||||
LEDStripe led1 = {0, 0, 5000, 0, 0, 0, 0, 0, 0};
|
||||
LEDStripe led2 = {0, 0, 5000, 0, 0, 0, 0, 0, 0};
|
||||
|
||||
int debugLoop = 0;
|
||||
|
||||
//Blink
|
||||
int fadeLevel = 0;
|
||||
int fadeMode = 0;
|
||||
void fade() {
|
||||
void fadeled() {
|
||||
if(fadeMode == 0) {
|
||||
fadeLevel = fadeLevel + 10;
|
||||
if(fadeLevel > 1023) {
|
||||
|
@ -103,7 +95,7 @@ void connectWiFi() {
|
|||
WiFi.begin(SSID, PSK);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(5);
|
||||
fade();
|
||||
fadeled();
|
||||
Serial.print(".");
|
||||
wifiConnectionCount++;
|
||||
if(wifiConnectionCount > 1000) {
|
||||
|
@ -165,58 +157,6 @@ void pulse_white_three_times() {
|
|||
LEDStripe tmpLed();
|
||||
}
|
||||
|
||||
//Modes
|
||||
//LED Fade
|
||||
void fade(LEDStripe *led) {
|
||||
if(led->MOD_STEP == 0) {
|
||||
led->RED ++;
|
||||
led->GREEN = 0;
|
||||
led->BLUE = 255;
|
||||
if(led->RED >= 255) {
|
||||
led->MOD_STEP++;
|
||||
}
|
||||
}
|
||||
if(led->MOD_STEP == 1) {
|
||||
led->RED = 255;
|
||||
led->GREEN = 0;
|
||||
led->BLUE --;
|
||||
if(led->BLUE <= 0) {
|
||||
led->MOD_STEP++;
|
||||
}
|
||||
}
|
||||
if(led->MOD_STEP == 2) {
|
||||
led->RED = 255;
|
||||
led->GREEN ++;
|
||||
led->BLUE = 0;
|
||||
if(led->GREEN >= 255) {
|
||||
led->MOD_STEP++;
|
||||
}
|
||||
}
|
||||
if(led->MOD_STEP == 3) {
|
||||
led->RED --;
|
||||
led->GREEN = 255;
|
||||
led->BLUE = 0;
|
||||
if(led->RED <= 0) {
|
||||
led->MOD_STEP++;
|
||||
}
|
||||
}
|
||||
if(led->MOD_STEP == 4) {
|
||||
led->RED = 0;
|
||||
led->GREEN = 255;
|
||||
led->BLUE ++;
|
||||
if(led->BLUE >= 255) {
|
||||
led->MOD_STEP++;
|
||||
}
|
||||
}
|
||||
if(led->MOD_STEP == 5) {
|
||||
led->RED = 0;
|
||||
led->GREEN --;
|
||||
led->BLUE = 255;
|
||||
if(led->GREEN <= 0) {
|
||||
led->MOD_STEP = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void writeToLedStrips(LEDStripe *l1, LEDStripe *l2) {
|
||||
analogWrite(REDPIN1, l1->RED);
|
||||
|
@ -247,11 +187,26 @@ void callback(char* topic, byte* payload, unsigned int length) {
|
|||
Serial.println();
|
||||
Serial.println(getTopicNameString("/1/mode"));
|
||||
if(String(topic) == getTopicNameString("/1/mode")) {
|
||||
led1.MOD = message;
|
||||
Serial.println("Set Mode");
|
||||
if(message == "off") {
|
||||
led1.MOD = 0;
|
||||
}
|
||||
if(message == "fade") {
|
||||
led1.MOD = 2;
|
||||
}
|
||||
if(message == "on") {
|
||||
led1.MOD = 1;
|
||||
}
|
||||
}
|
||||
if(String(topic) == getTopicNameString("/2/mode")) {
|
||||
led2.MOD = message;
|
||||
if(message == "off") {
|
||||
led1.MOD = 0;
|
||||
}
|
||||
if(message == "fade") {
|
||||
led1.MOD = 2;
|
||||
}
|
||||
if(message == "on") {
|
||||
led1.MOD = 1;
|
||||
}
|
||||
}
|
||||
if(String(topic) == getTopicNameString("/1/speed")) {
|
||||
led1.SPEED = message.toInt();
|
||||
|
@ -319,7 +274,7 @@ void loop() {
|
|||
}
|
||||
client.loop();
|
||||
|
||||
if(led1.MOD == "fade") {
|
||||
if(led1.MOD == 2) {
|
||||
fade(&led1);
|
||||
}
|
||||
|
||||
|
@ -328,7 +283,7 @@ void loop() {
|
|||
led2.GREEN = led1.GREEN;
|
||||
led2.BLUE = led1.BLUE;
|
||||
} else {
|
||||
if(led2.MOD == "fade") {
|
||||
if(led2.MOD == 2) {
|
||||
fade(&led2);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue