Boom, finished! Time to run the thing, right? Well, almost... all of the cool effects had to be coded and tested. I got a little impatient and left off some of the cooler ideas I wanted to implement, but I just wanted to run the darn thing. Below is the arduino code running the special effects on the locomotive:
Quote:
#include < Adafruit_DotStar.h>
#include < SPI.h>
#define NUMPIXELS 12 // Number of LEDs in strip
#define INPUT1 2
#define INPUT2 3
#define INPUT3 4
#define INPUT4 5
#define CLOCKPIN 6
#define DATAPIN 7
#define MOTOR1 8
#define MOTOR2 9
#define MOTORPWM 10
#define HEADLIGHT 13
Adafruit_DotStar strip = Adafruit_DotStar(NUMPIXELS, DATAPIN, CLOCKPIN);
boolean motorState = false; //is the motor spinning
boolean locoForward = true; //is the direction forward
boolean fadeUp = false; //is the red fade growing
int brightness = 0;
int brightnessLimit = 20; //limits how bright the fading leds go
int index = 1; //header leds
int index2 = 6; //cylinder leds
int motorSpeed = 180; //sets top motor speed
int pauseAmount = 80; //sets speed of fading red lights
unsigned long time = millis(); //keeps track of time passed
void setup() {
pinMode(INPUT1, INPUT);
pinMode(INPUT2, INPUT);
pinMode(INPUT3, INPUT);
pinMode(INPUT4, INPUT);
pinMode(CLOCKPIN, OUTPUT);
pinMode(DATAPIN, OUTPUT);
pinMode(MOTOR1, OUTPUT);
pinMode(MOTOR2, OUTPUT);
pinMode(MOTORPWM, OUTPUT);
pinMode(HEADLIGHT, OUTPUT);
strip.begin(); // Initialize pins for output
strip.show(); // Turn all LEDs off ASAP
Serial.begin(9600);
}
void loop() {
//inputDebug();
//function 5 controls INPUT1
//function 6 controls INPUT4
if (motorState == false && digitalRead(INPUT1) == HIGH) { //if the motor is off and needs to be turned on
motorStart();
}
if (motorState == true && digitalRead(INPUT1) == LOW) { //if the motor is on and needs to be turned off
motorStop();
}
if (digitalRead(INPUT3) == HIGH && locoForward == false) { //if forward direction
digitalWrite(HEADLIGHT, HIGH);
strip.setPixelColor(0, 8, 1, 5); // taillight off
locoForward = true;
}
if (digitalRead(INPUT3) == LOW && locoForward == true) { //if reversed direction
digitalWrite(HEADLIGHT, LOW);
strip.setPixelColor(0, 30, 5, 20); // taillight on
locoForward = false;
}
if (motorState == true) { //whenever the turbine spins, the leds cycle a yellow flash
cycleRedLights();
}
if (motorState == false && (millis() - time)> pauseAmount) { //governs the speed of the red fade
if (brightness < = 0 && fadeUp == false)
fadeUp = true;
if (brightness> = brightnessLimit && fadeUp == true)
fadeUp = false;
if (fadeUp == true)
brightness++;
if (fadeUp == false)
brightness--;
fadeRedLights(brightness);
time = millis();
}
strip.show(); //hehe, programming humor
}
void motorStart() { //slow ramp up for the auxiliary motor
digitalWrite(MOTOR1, LOW);
digitalWrite(MOTOR2, HIGH);
for (int x = 0; x < motorSpeed; x++) {
analogWrite(MOTORPWM, x);
delay(50);
}
motorState = true; //indicates that the motor has started
}
void motorStop() { //slow ramp down for the auxiliary motor
digitalWrite(MOTOR1, LOW);
digitalWrite(MOTOR2, HIGH);
for (int x = motorSpeed; x> 0; x--) {
analogWrite(MOTORPWM, x);
delay(50);
}
motorState = false; //indicates that the motor has stopped
}
void fadeRedLights(int x) { //resting mode where lights pulse as if breathing
for (int y = 1; y < NUMPIXELS; y++)
strip.setPixelColor(y, x, 0, 0); // 'On' pixel at head
strip.show(); // Refresh strip
}
void cycleRedLights () {
if (index> = 6)
index = 1;
if (index2 > = 12)
index2 = 6;
for (int x = 255; x> 10; x = x - 4) {
strip.setPixelColor(index, (x / 20), 0, (x / 45)); // 'On' pixel at head
strip.setPixelColor(index2, x, 0, (x / 2)); // 'On' pixel at head
strip.show(); // Refresh strip
}
for (int x = 10; x < 255; x = x + 4) {
strip.setPixelColor(index, x / 30, 0, 0); // 'On' pixel at head
strip.setPixelColor(index2, x / 2, 0, 0); // 'On' pixel at head
strip.show(); // Refresh strip
}
index++;
index2++;
}
void inputDebug() { //when you don't know if your arduino is getting anything from the decoder
Serial.print("input 1 ");
Serial.println(digitalRead(INPUT1));
Serial.print("input 2 ");
Serial.println(digitalRead(INPUT2));
Serial.print("input 3 ");
Serial.println(digitalRead(INPUT3));
Serial.print("input 4 ");
Serial.println(digitalRead(INPUT4));
delay(500);
}