Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

initial commit

  • Loading branch information...
commit e3ae58904f631c3b8137149786cd48f1c3a68138 0 parents
authored January 27, 2010
1  .gitignore
... ...
@@ -0,0 +1 @@
  1
+applet/*
99  bike_mountain.pde
... ...
@@ -0,0 +1,99 @@
  1
+#include <Stepper.h>
  2
+
  3
+#define STEPS 200
  4
+
  5
+int reedSwitch = 5;
  6
+long startTime = 0;
  7
+int revsPer = 0;
  8
+float runningAve = 0;
  9
+long lastSeen = 0;
  10
+int basePin = 3;
  11
+int peakPin = 4;
  12
+float rpm = 0;
  13
+
  14
+float lastFour[] = {
  15
+  0,0,0,0};
  16
+
  17
+Stepper stepper(STEPS, 12, 11, 8, 7);
  18
+
  19
+float ave(float nums[]){
  20
+  float sum = 0;
  21
+  for(int i = 0; i < 4; i++){
  22
+    sum = sum + nums[i];
  23
+  }
  24
+  return sum / 4.0; 
  25
+}
  26
+
  27
+void pushOnArray(float incoming, float arr[]){
  28
+  for(int i = 3; i > 0; i--){
  29
+    arr[i] = arr[i -1];
  30
+  }
  31
+  arr[0] = incoming;
  32
+}
  33
+
  34
+
  35
+void setup(){
  36
+  pinMode(reedSwitch, INPUT);
  37
+  pinMode(basePin, INPUT);
  38
+  pinMode(peakPin, INPUT);
  39
+  Serial.begin(9600);
  40
+  startTime = millis(); 
  41
+
  42
+  digitalWrite(10, HIGH); // enable
  43
+  digitalWrite(6, HIGH); // enable
  44
+
  45
+  stepper.setSpeed(50);
  46
+}
  47
+
  48
+void loop(){
  49
+  int atBase = digitalRead(basePin); 
  50
+  int atPeak = digitalRead(peakPin);
  51
+  // ====BEGIN RPM CODE=======
  52
+  digitalWrite(13, LOW);
  53
+  if(revsPer < 4){
  54
+    if(digitalRead(reedSwitch) && (millis() > lastSeen + 300)){
  55
+      digitalWrite(13, HIGH);
  56
+      revsPer++;
  57
+
  58
+      long thisInterval = millis() - lastSeen;
  59
+
  60
+      pushOnArray(thisInterval, lastFour);
  61
+      lastSeen = millis();
  62
+    } 
  63
+  } 
  64
+  else {
  65
+    runningAve = ave(lastFour); // this is now average time for 1/2 wheel rev
  66
+    float wheelTime = runningAve * 2;
  67
+    rpm = 60000 / wheelTime;
  68
+
  69
+    revsPer = 0;
  70
+    startTime = millis();
  71
+  }
  72
+
  73
+  //====COMMUNICATE WITH PROCESSING
  74
+  Serial.print(rpm);
  75
+  Serial.print(",");
  76
+  Serial.print(atBase);
  77
+  Serial.print(",");
  78
+  Serial.print(atPeak);
  79
+  Serial.println(",");
  80
+
  81
+  //=====DECIDE WHERE TO MOVE
  82
+  if(atBase){
  83
+   // Serial.println("up atBase");
  84
+    stepper.step(1);
  85
+  } 
  86
+  else {
  87
+    if(rpm < 50){
  88
+    //Serial.println("down under 50");
  89
+      stepper.step(-1);
  90
+    } 
  91
+    else {
  92
+      if(atPeak != 1){
  93
+      //  Serial.println("up not at top");
  94
+        stepper.step(1);
  95
+      }
  96
+    }
  97
+  }
  98
+}
  99
+

0 notes on commit e3ae589

Please sign in to comment.
Something went wrong with that request. Please try again.