Page MenuHomec4science

controller.ino
No OneTemporary

File Metadata

Created
Thu, Oct 3, 01:20

controller.ino

#include <SoftwareSerial.h>
#include <Adafruit_NeoPixel.h>
#define NUMPIXELS 32
#define NEO_PIN 2
#define FAN_PIN 4
#define PANEL_PIN 3
//#define DEBUG
#define DEMO
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, NEO_PIN, NEO_GRB + NEO_KHZ800);
SoftwareSerial Serial(0, 1, false); // RX, TX
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
#if defined(DEBUG)
Serial.println("Booting...");
#endif
pixels.begin();
pixels.clear();
pixels.show();
pinMode(PANEL_PIN, INPUT);
pinMode(FAN_PIN, OUTPUT);
}
bool door_open = false;
unsigned long last_time = 0;
unsigned long last_command = 0;
byte fan_speed = 0;
void loop() {
byte door_open = digitalRead(PANEL_PIN);
if(door_open) analogWrite(FAN_PIN, 100);
else analogWrite(FAN_PIN, fan_speed);
if(Serial.available() > 0) {
byte command = Serial.read();
#if defined(DEBUG)
Serial.print("Command received: ");
#endif
switch(command){
case 'f': {
fan_speed = Serial.parseInt();
if (!door_open) analogWrite(FAN_PIN, fan_speed);
#if defined(DEBUG)
Serial.print("Fan");
Serial.println(fan_speed, DEC);
#endif
break;
}
case 'l': {
last_command = millis();
byte row = Serial.parseInt();
#if defined(DEBUG)
Serial.print("Neo row ");
Serial.println(row, DEC);
#endif
for(uint8_t i = 0; i < 8; ++i) {
byte r = Serial.parseInt();
byte g = Serial.parseInt();
byte b = Serial.parseInt();
pixels.setPixelColor(row * 8 + i, r, g, b);
#if defined(DEBUG)
Serial.print("Color ");
Serial.print(r, DEC);
Serial.print(g, DEC);
Serial.print(b, DEC);
#endif
}
pixels.show();
break;
}
#if defined(DEBUG)
default:
Serial.println("Unknown");
#endif
}
}
#if defined(DEMO)
unsigned long now = millis();
if(now - last_command > 10000) {
if (now - last_time >= 500 && !door_open) {
byte row = random(4);
pixels.clear();
for(uint8_t i = 0; i < 8; ++i) {
byte r = random(100);
byte g = random(50);
byte b = random(25);
pixels.setPixelColor(row * 8 + i, r, g, b);
}
pixels.show();
last_time = now;
}
}
#endif
}

Event Timeline