Last active
January 31, 2016 02:05
-
-
Save theterg/cca2195c540565f5ea51 to your computer and use it in GitHub Desktop.
Wicked motor shield -> light rope
This file contains hidden or 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 <Wicked_DCMotor.h> | |
int num_motors = 5; | |
Wicked_DCMotor motor1(M1); | |
Wicked_DCMotor motor2(M2); | |
Wicked_DCMotor motor3(M3); | |
Wicked_DCMotor motor4(M4); | |
Wicked_DCMotor motor5(M5); | |
Wicked_DCMotor *m[] = {&motor1, &motor2, &motor3, &motor4, &motor5}; | |
uint8_t fadelvlend[5]; | |
uint8_t fadelvlstart[5]; | |
uint8_t fading[5]; | |
uint8_t fadevals[5]; | |
uint32_t fadestart[5]; | |
uint32_t fadeduration[5]; | |
char buff[100]; | |
uint32_t pauseuntil; | |
void setup(void){ | |
Serial.begin(115200); | |
Serial.print(F("Wicked Motor Shield Library version ")); | |
Serial.print(WickedMotorShield::version()); | |
Serial.println(F("- DC Motors")); | |
pinMode(A0, INPUT); | |
pinMode(A1, INPUT); | |
pinMode(A2, INPUT); | |
pinMode(A3, INPUT); | |
pinMode(A4, INPUT); | |
digitalWrite(A0, HIGH); | |
digitalWrite(A1, HIGH); | |
digitalWrite(A2, HIGH); | |
digitalWrite(A3, HIGH); | |
digitalWrite(A4, HIGH); | |
for (int i=0;i<5;i++) { | |
fading[i] = 0; | |
} | |
pauseuntil = 0; | |
// note, library initialized all motors to a clockwise direction and brake condition | |
for (int i=0;i<num_motors;i++) { | |
m[i]->setDirection(DIR_CCW); | |
m[i]->setBrake(BRAKE_OFF); | |
m[i]->setSpeed(0); | |
} | |
} | |
void doFades(void) { | |
uint32_t elapsed; | |
float level; | |
uint8_t fading = 0; | |
for (int i=0;i<5;i++) { | |
if (!fading[i]) continue; | |
fading = 1; | |
// If this channel is beyond it's duration, turn it off | |
if (millis() > fadestart[i]+fadeduration[i]) { | |
fading[i] = 0; | |
m[i]->setSpeed(fadelvlend[i]); | |
continue; | |
} | |
elapsed = millis()-fadestart[i]; | |
// (elapsed/duration) is how far through the fade we are, 0-1 | |
// then scale and shift so that level will scale linearly from start -> end across the duration | |
if (fadelvlend[i] > fadelvlstart[i]) | |
level = ((float)fadelvlstart[i]) + (((float)elapsed)/((float)fadeduration[i])) * ((float)(fadelvlend[i] - fadelvlstart[i])); | |
else | |
level = ((float)fadelvlstart[i]) - (((float)elapsed)/((float)fadeduration[i])) * ((float)(fadelvlstart[i] - fadelvlend[i])); | |
// Clip to valid intensity values | |
if (level < 0.0) level = 0.0; | |
if (level > 255.0) level = 255.0; | |
m[i]->setSpeed( (uint8_t)level ); | |
fadevals[i] = (uint8_t)level; | |
// Debug print the fade status | |
sprintf(buff, "%d[%d->%d]%ld@%ld(%d) ", i, fadelvlstart[i], fadelvlend[i], fadestart[i], fadeduration[i], (uint8_t)level ); | |
Serial.print(buff); | |
} | |
if (fading) | |
Serial.println(" "); | |
} | |
void loop(void){ | |
uint8_t pressed; | |
doFades(); | |
// If any buttons are pressed, pause the random walk for 10 seconds | |
for (int i=0;i<5;i++) { | |
pressed = !digitalRead(A0+i); | |
if (pressed) { | |
pauseuntil = millis() + 10000; | |
} | |
} | |
// If we're not currently paused (in manual mode) | |
if (millis() > pauseuntil) { | |
// If any channels aren't currenly fading, trigger a new random fade | |
for (int i=0;i<5;i++) { | |
if (fading[i]) continue; | |
fading[i] = 1; | |
fadestart[i] = millis(); | |
fadeduration[i] = random(1000, 5000); | |
// toggle between high and low values | |
// Otherwise it doesn't always look like lights are changing. | |
if (fadelvlend[i] < 5) { | |
fadelvlstart[i] = fadelvlend[i]; | |
fadelvlend[i] = random(0, 80); | |
} else { | |
fadelvlstart[i] = fadelvlend[i]; | |
fadelvlend[i] = random(0, 4); | |
} | |
} | |
} else { | |
// If in manual mode, fade each channel according to it's button | |
for (int i=0;i<5;i++) { | |
pressed = !digitalRead(A0+i); | |
// Only start a new fade if we aren't currently fading | |
if (fading[i]) continue; | |
fading[i] = 1; | |
fadestart[i] = millis(); | |
fadeduration[i] = 100; | |
fadelvlstart[i] = fadevals[i]; | |
fadelvlend[i] = 200*pressed; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment