Permalink
Browse files

Merge pull request #3 from nicole-allard/master

servo and leds both working
  • Loading branch information...
2 parents 8d6e91c + 2148926 commit 3b614ab86346ef473c4e0a69b74464b4b482dd25 @bryanduxbury committed Apr 25, 2012
Showing with 52 additions and 37 deletions.
  1. +52 −37 firmware/firmware.ino
View
@@ -1,78 +1,93 @@
#include <Servo.h>
#define FIRST_LED_PIN 14
+#define LAST_LED_PIN 53
#define NUM_LEDS 40
-#define MAX_DEGREES 180.0
+#define MIN_DEGREES 5
+#define MAX_DEGREES 167
Servo servo1;
int servoDegrees = 0;
+float percent = 0;
int numLedsLit = 0;
+int first = -1;
+
void setup() {
servo1.attach(9);
Serial.begin(9600);
startupSequence();
- servoDegrees = 90;
+ servoDegrees = 0;
}
-void loop() {
-// if (Serial.available()) {
-// servoDegrees = min(180, max(0, Serial.read()));
-// numLedsLit = float(servoDegrees)/MAX_DEGREES * NUM_LEDS;
-// }
-
- updateExternals();
-
+// Temporary input parsing for debugging
+void parseInput() {
+ int input = Serial.read();
+ // line feed
+ if (input == 10) {
+ return;
+ }
+ input = input - 48;
+ if (first == -1) {
+ first = input;
+ }
+ else {
+ percent = (first * 10) + input;
+ first = -1;
+ }
}
void startupSequence() {
for (int i = 0; i < NUM_LEDS; i++) {
pinMode(FIRST_LED_PIN + i, OUTPUT);
- digitalWrite(FIRST_LED_PIN + i, HIGH);
- delay(200);
- digitalWrite(FIRST_LED_PIN + i, LOW);
}
-// for (int i = 0; i <= 180; i++) {
-// servo1.write(i);
-// delay(150);
-// }
-
- for (int i = 0; i <= 180; i++) {
- servoDegrees = i;
- numLedsLit = float(servoDegrees)/MAX_DEGREES * NUM_LEDS;
- long endTime = millis() + 200;
+ calculateExternals(float(0));
+ delay(1000);
+ for (int i = 0; i <= 100; i++) {
+ calculateExternals(float(i));
+ unsigned long endTime = millis() + 15;
while (millis() < endTime) {
- updateExternals();
+ updateLEDs();
}
}
for (int i = 180; i >= 0; i--) {
- servoDegrees = i;
- numLedsLit = float(servoDegrees)/MAX_DEGREES * NUM_LEDS;
- long endTime = millis() + 200;
+ calculateExternals(float(i));
+ unsigned long endTime = millis() + 15;
while (millis() < endTime) {
- updateExternals();
+ updateLEDs();
}
}
}
-void delayMicros(long usec) {
- unsigned long start = micros();
- while (micros() - usec < start) {}
-}
+void loop() {
+ if (Serial.available()) {
+ parseInput();
+ calculateExternals(percent);
+ }
-void updateExternals() {
+ updateLEDs();
+}
+
+void calculateExternals(float percent) {
+ percent = percent/float(100);
+ percent = min(1, max(0, percent));
+ servoDegrees = MAX_DEGREES - (percent * (MAX_DEGREES - MIN_DEGREES));
+
+ numLedsLit = round(percent * float(NUM_LEDS));
servo1.write(servoDegrees);
+}
- for (int i = FIRST_LED_PIN; i < FIRST_LED_PIN+numLedsLit; i+=5) {
- for (int j = 0; j < 5 && i + j < FIRST_LED_PIN+numLedsLit; j++) {
- digitalWrite(i + j, HIGH);
+void updateLEDs() {
+ for (int i = LAST_LED_PIN; i > LAST_LED_PIN-numLedsLit; i-=5) {
+ for(int j = 0; j < 5 && i-j > LAST_LED_PIN-numLedsLit; j++) {
+ digitalWrite(i - j, HIGH);
}
delay(1);
- for (int j = 0; j < 5 && i + j < FIRST_LED_PIN+numLedsLit; j++) {
- digitalWrite(i + j, LOW);
+ for(int j = 0; j < 5 && i-j > LAST_LED_PIN-numLedsLit; j++) {
+ digitalWrite(i - j, LOW);
}
}
}

0 comments on commit 3b614ab

Please sign in to comment.