Kevin Wells há 6 anos atrás
pai
commit
37dcfe0072
7 ficheiros alterados com 140 adições e 141 exclusões
  1. 1 1
      .vscode/arduino.json
  2. 81 0
      _main.ino
  3. 6 28
      lapCounter.ino
  4. 26 39
      loop.ino
  5. 0 47
      main.ino
  6. 5 0
      pushButton.ino
  7. 21 26
      setup.ino

+ 1 - 1
.vscode/arduino.json

@@ -3,5 +3,5 @@
     "configuration": "cpu=16MHzatmega328",
     "programmer": "AVRISP mkII",
     "port": "COM5",
-    "sketch": "main.ino"
+    "sketch": "_main.ino"
 }

+ 81 - 0
_main.ino

@@ -0,0 +1,81 @@
+/*
+
+ * This is the Arduino code for an: 
+ * Infrared Obstacle Sensor and displaying the time on 6 digital display and serial monitor.
+ * Uses preprogrammed distance and converting it into mph.
+
+***Still needs to count and store laps for average #of laps once button press
+***Still needs to use 2 digit separate display the lap count for the above laps once button press
+
+
+IR Sensor Wiring:
+Brown: 5V DC
+Blue:  GND
+Black: Signal, to PIN 3
+
+Arduino pins to the Large Digit Driver IN
+
+Arduino pin for SPEED DISPLAY
+ 6 -> CLK (GREEN)
+ 5 -> LAT (BLUE)
+ 7 -> SER on the IN side (YELLOW)
+ 5V -> 5V (ORANGE)
+ Power Arduino with 12V and connect to Vin -> 12V (RED)
+ GND -> GND (PURPLE)
+ 8 -> Push Button
+ START Input 
+
+Arduino pin for LAP DISPLAY
+ 11 -> CLK (GREEN)
+ 10 -> LAT (BLUE)
+ 12 -> SER on the IN side (YELLOW)
+ 5V -> 5V (ORANGE)
+ Power Arduino with 12V and connect to Vin -> 12V (RED)
+ GND -> GND (PURPLE)
+
+*/
+
+//GPIO declarations
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+#define segmentClock 6
+#define segmentLatch 5
+#define segmentData 7
+
+#define segmentClockLAP 11
+#define segmentLatchLAP 10
+#define segmentDataLAP 12
+#define button 2
+
+
+// whittier track circumference equals 205.08 feet or 0.0388409091 miles per lap USED 0.0384
+// Sabatatini test track circumference equals 100.531 feet or 0.01903996212 miles per lap USED 0.01903   
+// Iowa track = 0.02083333333 miles per lap
+#define distance 0.02083333333
+#define maxLaps 12
+
+
+#define MODE_SPINUP 0
+#define MODE_COUNTING 1
+#define MODE_RESTING 2
+
+#define SENSOR 3 // define pint 3 for sensor
+#define ACTION 9 // define pin 9 as for ACTION
+int mode = MODE_SPINUP;
+float startTime;
+float endTime;
+float duration;
+byte timerRunning;
+unsigned long time;
+
+float seconds;
+// float minutes;
+float hours;
+float mph;
+int laps = 0;
+int avglaps= 0; // AVERAGE LAP COUNTER
+
+
+//LAP COUNTER SETUP ///////////////////////////////////////////////////
+int recordedLaps = 0;     //number of samples averaged 
+double speeds[maxLaps] = {0};    // need a 0 for every sample to average
+

+ 6 - 28
lapCounter.ino

@@ -1,34 +1,12 @@
-//LAP COUNTER SETUP ///////////////////////////////////////////////////
-const int dlen = 4;     //number of samples averaged 
-double d[dlen] = {0};    // need a 0 for every sample to average
-
-void pushd(double val) {
-  for (int i = 1; i < dlen; i++) {
-    d[i-1] = d[i]; 
-  }
-  d[dlen - 1] = val;
-//Serial.print ("val: "); /// same as ad
-//Serial.println (val,3);
-  
+void recordLap(double val) {
+  speeds[recordedLaps++] = val;
 }
 
-double avgd() {
+double averageSpeed() {
   double sum = 0;
-  for (int i = 0; i < dlen; i++) {
- 
-    sum = sum + d[i]; //  adds previous lap to sum
-    
-//Serial.print ("d[i]: ");
-//Serial.println (d[i],3);
-//Serial.print ("sum: ");
-//Serial.println (sum,3);
-
-    
+  for (int i = 0; i < recordedLaps; i++) {
+    sum = sum + speeds[i]; //  adds previous lap to sum
   }
-  double average = sum / (double)dlen;
+  double average = sum / (double)recordedLaps;
   return average;
-
-  
 }
-
-

+ 26 - 39
loop.ino

@@ -1,22 +1,3 @@
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-#define SENSOR 3 // define pint 3 for sensor
-#define ACTION 9 // define pin 9 as for ACTION
-
-float startTime;
-float endTime;
-float duration;
-byte timerRunning;
-unsigned long time;
-
-float distance;
-float seconds;
-// float minutes;
-float hours;
-float mph;
-int laps = 0;
-int avglaps= 0; // AVERAGE LAP COUNTER
-
-
 void loop() {
   // int v = digitalRead(8);
   // Serial.print(v);
@@ -45,28 +26,34 @@ void loop() {
     Serial.print ("avgLap:   ");
     Serial.println (avglaps);
     delay(5); // added this delay to stop reading rear wheels error before was up to .003 seconds
-    showNumber(mph);
-    showNumberLAP(laps);
+    if (mode == MODE_SPINUP) {
+      showNumber(mph);
+      showShapeLAP(2);
+    }
+    if (mode == MODE_COUNTING) {
+      showNumber(mph);
+      showNumberLAP(recordedLaps);
+      recordLap(mph);
+      if (recordedLaps == maxLaps) {
+        Serial.println('Switching mode to MODE_RESTING\n');
+        mode = MODE_RESTING;
+      }
+    }
+    if (mode == MODE_RESTING) {
+      showNumber(averageSpeed());
+      showNumberLAP(maxLaps);
+      delay(3000);
+      showNumber(0);
+      showShapeLAP(2);
+      Serial.println("Setting mode to MODE_SPINUP\n");
+      mode = MODE_SPINUP;
+      recordedLaps = 0;
+    } 
+    // Otherwise leave it
 
     /// LAP AVERAGE COUNTER LOOP CODE /////////////////////
-    double nd = mph;
-    //double ad= mph;
-    pushd(nd);
-    double ad = avgd();
-
-    //  pushd(ad); // this pushs previous average back into new average
-
-    //Serial.print ("nd: ");
-    //Serial.println (nd,4);
-    Serial.print ("ad: ");
-    Serial.println (ad,4);
-    //Serial.print ("dlen: ");
-    //Serial.println (dlen,4);
-
-
-    //Serial.print ("d[dlen]: ");
-    //Serial.println (d[dlen],4);
-    //Serial.print(average);
+    Serial.print("avgSpeed: ");
+    Serial.println (averageSpeed(),4);
   }
 
 }

+ 0 - 47
main.ino

@@ -1,47 +0,0 @@
-/*
-
- * This is the Arduino code for an: 
- * Infrared Obstacle Sensor and displaying the time on 6 digital display and serial monitor.
- * Uses preprogrammed distance and converting it into mph.
-
-***Still needs to count and store laps for average #of laps once button press
-***Still needs to use 2 digit separate display the lap count for the above laps once button press
-
-
-IR Sensor Wiring:
-Brown: 5V DC
-Blue:  GND
-Black: Signal, to PIN 3
-
-Arduino pins to the Large Digit Driver IN
-
-Arduino pin for SPEED DISPLAY
- 6 -> CLK (GREEN)
- 5 -> LAT (BLUE)
- 7 -> SER on the IN side (YELLOW)
- 5V -> 5V (ORANGE)
- Power Arduino with 12V and connect to Vin -> 12V (RED)
- GND -> GND (PURPLE)
- 8 -> Push Button
- START Input 
-
-Arduino pin for LAP DISPLAY
- 11 -> CLK (GREEN)
- 10 -> LAT (BLUE)
- 12 -> SER on the IN side (YELLOW)
- 5V -> 5V (ORANGE)
- Power Arduino with 12V and connect to Vin -> 12V (RED)
- GND -> GND (PURPLE)
-
-*/
-
-//GPIO declarations
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-const byte segmentClock = 6;
-const byte segmentLatch = 5;
-const byte segmentData = 7;
-
-const byte segmentClockLAP = 11;
-const byte segmentLatchLAP = 10;
-const byte segmentDataLAP = 12;
-const byte button = 2;

+ 5 - 0
pushButton.ino

@@ -6,6 +6,11 @@ void pushButton()
   if (interrupt_time - last_interrupt_time > 200) 
   {
     Serial.print("Button\n");
+    if (mode == MODE_SPINUP) {
+      Serial.print("Switching mode to MODE_COUNTING\n");
+      mode = MODE_COUNTING;
+      recordedLaps = 0;
+    }
   }
   last_interrupt_time = interrupt_time;
 }

+ 21 - 26
setup.ino

@@ -1,5 +1,4 @@
 
-
 void setup() {
   Serial.begin(9600);// setup Serial Monitor to display information
   pinMode(SENSOR, INPUT_PULLUP);// define pin as Input  sensor
@@ -26,31 +25,27 @@ void setup() {
   
   
 
-// whittier track circumference equals 205.08 feet or 0.0388409091 miles per lap USED 0.0384
-// Sabatatini test track circumference equals 100.531 feet or 0.01903996212 miles per lap USED 0.01903   
-// Iowa track = 0.02083333333 miles per lap
-   distance= 0.02083333333; 
-   Serial.println("Iowa Lap Timer");
-   Serial.print(distance, 4);
-   Serial.println (" = circumference in miles");
-   Serial.print(.250/distance, 4);
-   Serial.println(" = laps per quarter mile");
-   Serial.print(.310686/distance, 4);
-   Serial.println(" =laps per 500 meters");
-   Serial.println ("READY!");
-   showShape(0);
-   showShapeLAP(0);
-   delay(500);
-   showShape(4);
-   showShapeLAP(4);
-   delay(500);
-   showShape(0);
-   showShapeLAP(0);
-   delay(500);
-   showShape(4);
-   showShapeLAP(4);
-   delay(500);
-   showShapeLAP(2);
+  Serial.println("Iowa Lap Timer");
+  Serial.print(distance, 4);
+  Serial.println (" = circumference in miles");
+  Serial.print(.250/distance, 4);
+  Serial.println(" = laps per quarter mile");
+  Serial.print(.310686/distance, 4);
+  Serial.println(" =laps per 500 meters");
+  Serial.println ("READY!");
+  showShape(0);
+  showShapeLAP(0);
+  delay(500);
+  showShape(4);
+  showShapeLAP(4);
+  delay(500);
+  showShape(0);
+  showShapeLAP(0);
+  delay(500);
+  showShape(4);
+  showShapeLAP(4);
+  delay(500);
+  showShapeLAP(2);
    
 
 }