Created
March 23, 2016 19:39
-
-
Save dwilkins/e47c1ae00b5ef32dab45 to your computer and use it in GitHub Desktop.
Arduino program to test nuvinci hub
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 <Wire.h> | |
#include <Adafruit_MCP4725.h> | |
Adafruit_MCP4725 dac; | |
volatile int counter = 0; | |
void setup(void) { | |
Serial.begin(57600); | |
Serial1.begin(57600); | |
pinMode(2, INPUT); // tach on pin 2 | |
attachInterrupt (digitalPinToInterrupt(2), MyISR, FALLING); | |
dac.begin(0x60); // address for BangGood device was 0x60 | |
delay(1000); | |
Serial1.println("start"); | |
} | |
void loop(void) { | |
for (int i = 0; i < 42; ++i) { | |
int gear = 100 * i; | |
if (gear > 4096) gear = 4096; | |
dac.setVoltage(gear,false); | |
delay(2000); // time to run up; | |
noInterrupts(); | |
counter = 0; | |
interrupts(); | |
delay(500); | |
noInterrupts(); | |
int rpm = counter * 60; // 500ms 2 magnets 2 pulses per revolution | |
counter = 0; | |
interrupts(); | |
float mph = ((rpm * 63.0 / 12.0) * 60.0) / 5280.0; | |
Serial.print(gear); | |
Serial.print("\t"); | |
Serial.println(mph,2); | |
Serial1.print(gear); | |
Serial1.print("\t"); | |
Serial1.println(mph,2); | |
} | |
Serial1.println("end"); | |
} | |
void MyISR(){ | |
++counter; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment