Created
March 13, 2020 00:51
-
-
Save jfrux/1d7e6fe268deb37d6024d3b03f473233 to your computer and use it in GitHub Desktop.
Colins Wind Tunnel Fan Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Adafruit_CircuitPlayground.h> | |
#include <Adafruit_Circuit_Playground.h> | |
#include <Servo.h> | |
Servo throttle; | |
bool testMode = false; | |
int escPin = 10; | |
int buttonPin = 4; | |
int forwardPos = 102; | |
int neutralPos = 90; | |
int currentPos = neutralPos; | |
int buttonState = LOW; | |
int timeToRun = 10000; | |
int sweepDelay = 20; | |
String under25perc = "green"; | |
String under50perc = "yellow"; | |
String under75perc = "orange"; | |
String under100perc = "red"; | |
float currentProgressPerc = 0; | |
float currentProgressPercFriendly = 0; | |
float currentProgress = 0; | |
float maxProgress = ((float)forwardPos-(float)neutralPos); | |
float ledAmt = ((float)forwardPos-(float)neutralPos)/10.0; | |
float currentLedAmt = 0.0; | |
int currentLed = 0; | |
int sweepTime = (forwardPos-neutralPos)*sweepDelay; | |
int motorState = LOW; | |
unsigned long currentTime; | |
unsigned long timerTime; | |
void setup(){ | |
CircuitPlayground.begin(); | |
pinMode(buttonPin, INPUT); | |
if (!testMode) { | |
throttle.attach(escPin); | |
throttle.write(neutralPos); | |
} | |
} | |
void loop() { | |
// BUTTON | |
buttonState = digitalRead(buttonPin); | |
// IF BUTTON IS PRESSED | |
if (buttonState == HIGH) { | |
currentLed = 0; | |
currentProgress = 0; | |
currentProgressPerc = 0; | |
motorState == HIGH; | |
CircuitPlayground.clearPixels(); | |
for (currentPos = neutralPos; currentPos < forwardPos; currentPos += 1) { // goes from 0 degrees to 180 degrees | |
// STEP THROUGH SPEED POSITION BY 1 DEGREE EACH TIME | |
if (!testMode) { | |
throttle.write(currentPos); | |
} | |
// DELAY BETWEEN EACH SPEED CHANGE | |
delay(sweepTime); | |
currentProgress++; | |
currentProgressPerc = (currentProgress/maxProgress); | |
currentProgressPercFriendly = currentProgressPerc * 100; | |
currentLed = (floor)(currentProgressPerc*10); | |
// CHANGE COLOR OF LED'S BASED ON SPEED | |
if (currentProgressPercFriendly <= 25) { | |
CircuitPlayground.setPixelColor(currentLed, 0x00FF00); | |
} else if (currentProgressPercFriendly > 25 && currentProgressPercFriendly <= 50) { | |
CircuitPlayground.setPixelColor(currentLed, 0xFFFF00); | |
} else if (currentProgressPercFriendly > 50 && currentProgressPercFriendly <= 75) { | |
CircuitPlayground.setPixelColor(currentLed, 0xFF5500); | |
} else if (currentProgressPercFriendly > 75 && currentProgressPercFriendly <= 100) { | |
CircuitPlayground.setPixelColor(currentLed, 0xFF0000); | |
} | |
} | |
delay(timeToRun-sweepTime); | |
CircuitPlayground.clearPixels(); | |
throttle.write(neutralPos); | |
} else { | |
throttle.write(neutralPos); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment