#include <Adafruit_ILI9341.h>
#include <Adafruit_NeoPixel.h>
#include "Fonts/FreeSerif9pt7b.h"
Adafruit_ILI9341 tft(10,9);
Adafruit_NeoPixel np(8,8);
int bu = 7, bm = 6, bd = 5;
void Text(int x, int y, uint16_t color, int size, String text) {
tft.setFont(&FreeSerif9pt7b);
tft.setCursor(x,y);
tft.setTextColor(color);
tft.setTextSize(size);
tft.print(text);
}
bool click() {
return (digitalRead(bu) == 0 || digitalRead(bm) == 0 || digitalRead(bd) == 0)? true : false;
}
bool press(int btn) {
return (!digitalRead(btn)) ? true : false;
}
uint32_t color_np[7] {0xff0000, 0xff9900, 0xffff00, 0x00ff00, 0x00ffff, 0x0000ff, 0xff00ff};
int brightness = 205;
int state = 1;
int opt1 = 1;
int run_mode = 1;
void led_running_mode(int mode) {
switch(mode) {
case 1:
for(int i=0; i<7; i++) {
for(int j=0; j<8; j++) {
np.setPixelColor(j, color_np[i]);
np.show();
if(click())
break;
delay(50);
}
if(click())
break;
delay(100);
for(int j=0; j<8; j++) {
np.setPixelColor(j, 0x000000);
np.show();
if(click())
break;
delay(50);
}
} break;
case 2:
for(int i=0; i<7; i++) {
for(int j=0; j<4; j++) {
np.setPixelColor(j, color_np[i]);
np.setPixelColor(7-j, color_np[i]);
np.show();
if(click())
break;
delay(50);
}
if(click())
break;
delay(100);
for(int j=0; j<4; j++) {
np.setPixelColor(j, 0x000000);
np.setPixelColor(7-j, 0x000000);
np.show();
if(click())
break;
delay(50);
}
} break;
case 3:
for(int i=0; i<7; i++) {
for(int j=0; j<8; j++) {
for(int k=0; k<=(7-j); k++) {
np.setPixelColor(k, color_np[i]);
np.show();
if(click())
break;
delay(50);
if(k<(7-j)) {
np.setPixelColor(k, 0x000000);
np.show();
}
}
if(click())
break;
delay(50);
}
if(click())
break;
delay(100);
}
}
}
void setup() {
tft.begin();
np.begin();
tft.fillScreen(0xffff);
np.setBrightness(brightness);
pinMode(bu, INPUT_PULLUP);
pinMode(bm, INPUT_PULLUP);
pinMode(bd, INPUT_PULLUP);
}
void loop() {
if(state == 1) {
Text(100, 15, 0x0, 1, "Torch");
Text(10, 40, 0x0, 1, "Off");
Text(10, 40+30, 0x0, 1, "On");
Text(10, 40+2*30, 0x0, 1, "Red");
Text(10, 40+3*30, 0x0, 1, "Green");
Text(10, 40+4*30, 0x0, 1, "Blue");
Text(10, 40+5*30, 0x0, 1, "Yellow");
Text(10, 40+6*30, 0x0, 1, "Running LED");
tft.drawRoundRect(5,(opt1-1)*30+22, 200, 24, 3, 0x03e7);
switch(opt1) {
case 1:
for(int i=0; i<8; i++) {
np.setPixelColor(i, 0x000000);
np.show();
} break;
case 2:
for(int i=0; i<8; i++) {
np.setPixelColor(i, 0xffffff);
np.show();
} break;
case 3:
for(int i=0; i<8; i++) {
np.setPixelColor(i, 0xff0000);
np.show();
} break;
case 4:
for(int i=0; i<8; i++) {
np.setPixelColor(i, 0x00ff00);
np.show();
} break;
case 5:
for(int i=0; i<8; i++) {
np.setPixelColor(i, 0x0000ff);
np.show();
} break;
case 6:
for(int i=0; i<8; i++) {
np.setPixelColor(i, 0xffff00);
np.show();
} break;
case 7:
led_running_mode(run_mode);
break;
}
if(click()) {
tft.drawRoundRect(5,(opt1-1)*30+22, 200, 24, 3, 0xffff);
if(press(bu)) {
opt1--;
(opt1 < 1) ? opt1 = 7 : opt1 = opt1;
delay(150);
}
else if(press(bd)) {
opt1++;
(opt1 > 7) ? opt1 = 1 : opt1 = opt1;
delay(150);
}
else if(press(bm) && opt1 != 1 && opt1 != 7) {
tft.fillRoundRect(5,(opt1-1)*30+22, 200, 24, 3, 0x03e7);
Text(10, 40+(opt1-1)*30, 0xffff, 1, "Brightness");
delay(100);
state = 2;
}
else if(press(bm) && opt1==7) {
tft.fillRoundRect(5,(opt1-1)*30+22, 200, 24, 3, 0x03e7);
Text(10, 40+(opt1-1)*30, 0xffff, 1, "Mode: ");
delay(100);
state = 3;
}
}
}
else if(state == 2) {
tft.fillRoundRect(5,310, brightness,3, 3, 0x19ff);
if(click()) {
tft.fillRoundRect(5,310, brightness,3, 3, 0xffff);
if(press(bu)) {
brightness = (brightness+5)%255;
np.setBrightness(brightness); np.show();
delay(150);
}
else if(press(bd)) {
brightness -= 5;
(brightness < 0) ? brightness = 0: brightness -= 0;
np.setBrightness(brightness); np.show();
delay(150);
}
else if(press(bm)) {
tft.fillRoundRect(5,(opt1-1)*30+22, 200, 24, 3, 0xffff);
tft.drawRoundRect(5,(opt1-1)*30+22, 200, 24, 3, 0x03e7);
//Text(10, 40+5*30, 0x0, 1, "Brightness");
delay(100);
state = 1;
}
}
}
else if(state == 3) {
tft.setCursor(65, 40+6*30);
tft.print(run_mode);
led_running_mode(run_mode);
if(click()) {
tft.fillRect(65,(opt1-1)*30+24, 20,20, 0x03e7);
if(press(bu)) {
run_mode++;
(run_mode>3) ? run_mode = 1: run_mode+=0;
delay(150);
}
else if(press(bd)) {
run_mode--;
(run_mode<1) ? run_mode = 3: run_mode+=0;
delay(150);
}
else if(press(bm)) {
tft.fillRoundRect(5,(opt1-1)*30+22, 200, 24, 3, 0xffff);
tft.drawRoundRect(5,(opt1-1)*30+22, 200, 24, 3, 0x03e7);
delay(100);
state = 1;
}
}
}
}