//tail lamp dev v3
#include <Adafruit_NeoPixel.h>
//# include "array.h"
#define PIN 3
const int sensorPin1 = A0; // select a input pin for control 1 RUN LAMP
const int sensorPin2 = A1; // select a input pin for control 2 TURN SIGNAL
const int sensorPin3 = A2; // select a input pin for control 3 BRAKE LIGHT
const int sensorPin4 = A3; // select a input pin for control 4 REVERSE
/// const int runPin = A5; // select a input pin for control 4 REVERSE
int sensorValue1 = A0; // variable to store the value coming from the sensor
int sensorValue2 = A1; // variable to store the value coming from the sensor
int sensorValue3 = A2; // variable to store the value coming from the sensor
int sensorValue4 = A3; // variable to store the value coming from the sensor
// pins for logic analyser
# define LTIME 5 // loop time
# define BTIME 4 // brake light setup
# define RTIME 6 // reverse light setup
# define STIME 7 // strip show
const int NUM_PIXELS = 128;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_PIXELS, PIN, NEO_GRB);
void setup()
{
strip.begin();
strip.setBrightness(255);
turnOffAll();
strip.show();
pinMode(sensorPin1, INPUT_PULLUP);
pinMode(sensorPin2, INPUT_PULLUP);
pinMode(sensorPin3, INPUT_PULLUP);
pinMode(sensorPin4, INPUT_PULLUP);
// for timing analysis:
pinMode(LTIME, OUTPUT);
pinMode(BTIME, OUTPUT);
pinMode(RTIME, OUTPUT);
pinMode(STIME, OUTPUT);
}
// palette
# define aRED 0xff0000
# define aWHITE 0xffffff
# define aBLACK 0x000002
# define OFF 0
# define ON 1
// bits from car sensors
# define RUN 0x1
# define TURN 0x2
# define BRAKE 0x4
# define REVERSE 0x8
void turnOffAll()
{
for (int ii = 0; ii <= 127; ii++)
strip.setPixelColor(ii, aBLACK);
}
void turnBrake(unsigned char onOff)
{
uint32_t color = onOff ? aRED : aBLACK;
for (int ii = 0; ii <= 23; ii++)
strip.setPixelColor(ii, color);
for (int yy = 2; yy <= 5; yy++)
for (int xx = 40; xx <= 120; xx += 8)
strip.setPixelColor(xx + yy, color);
}
void turnReverse(unsigned char onOff)
{
uint32_t color = onOff ? aWHITE : aBLACK;
for (int ii = 24; ii <= 39; ii++)
strip.setPixelColor(ii, color);
}
void turnRun(unsigned char onOff)
{
uint32_t color = onOff ? aRED : aBLACK;
for (int yy = 0; yy <= 1; yy++)
for (int xx = 40; xx <= 120; xx += 8) {
strip.setPixelColor(xx + yy, color);
strip.setPixelColor(xx + yy + 6, color);
}
}
static unsigned char currentDisplay = 0; // pattern 0 is all off
static bool turning = false;
void loop()
{
unsigned char theState = 0;
digitalWrite(5, HIGH);
if (digitalRead(sensorPin1) == LOW) theState |= 1; // RUN
if (digitalRead(sensorPin2) == LOW) theState |= 2; // TURN
if (digitalRead(sensorPin3) == LOW) theState |= 4; // BRAKE
if (digitalRead(sensorPin4) == LOW) theState |= 8; // REVERSE
if (theState & TURN) {
if (!turnMachineStep(1)) { // goose the machine
turnRun(ON);
turnMachineStep(0); // re-initialise the machine
}
}
else {
turnMachineStep(0);
if (theState & RUN)
turnRun(!(theState & BRAKE));
else turnRun(OFF);
}
digitalWrite(5, LOW);
// maintain static lights
digitalWrite(4, HIGH);
turnBrake(theState & BRAKE);
digitalWrite(4, LOW);
digitalWrite(6, HIGH);
turnReverse(theState & REVERSE);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
strip.show(); // often? unnecessary, figure it out
digitalWrite(7, LOW);
}
# define TWEEN 35
unsigned char turnMachineStep(unsigned char command)
{
static unsigned char step;
static unsigned long lastChange = 0;
static unsigned char busy = 0;
if (command == 0) {
turnRun(ON);
step = 0;
lastChange = millis();
busy = 1;
return (1);
}
if (!busy)
return (0);
if (millis() - lastChange < TWEEN)
return (1);
lastChange = millis();
turnRun(ON); // then turn off step # of columns...
for (int yy = 0; yy <= 1; yy++) {
int xx = 40; /* huh? */
for (int ns = 0; ns < step; ns++, xx += 8) {
strip.setPixelColor(xx + yy, aBLACK);
strip.setPixelColor(xx + yy + 6, aBLACK);
}
}
step++;
if (step == 12) {
busy = 0;
return (0); // machine is done
}
}