//Frank F. 2016 //project: bike speedometer //version: 2.3 //Licence: Attribution-NonCommercial-NoDerivatives 4.0 International const int digits = 3; //How many digits on the ssd const int dot = 7; //Dot pin # const int commons [digits] = {4, 5, 6}; //Common anode pins const int BCD [4] = {0, 1, 2, 3}; //BCD pins of the 7447 const int D2B [10] [4] = { {0,0,0,0}, {1,0,0,0}, {0,1,0,0}, {1,1,0,0}, {0,0,1,0}, {1,0,1,0}, {0,1,1,0}, {1,1,1,0}, {0,0,0,1}, {1,0,0,1} }; //Binary numbers 0-9 const int freq = 1000; //Display refresh frequency const int sensPin = 8; //Sensor pin # const double pi = 3.14159265359; //Declaration of pi as a constant number const int mags = 2; //Number of magnets on the bike ***CHANGE*** const double R = 0.34; //Radius of the bike whell ***CHANGE*** double u = 0; //Velocity double T = 0; //Interval between two reads long double prevTime = 0; //Previous time //double A = 0; //Debug 1 var //double j = 0; //Debug 2 car void setup() { for(int i = 0; i < 4; i++){ pinMode(BCD [i], OUTPUT); pinMode(commons [i], OUTPUT); } pinMode(dot, OUTPUT); pinMode(sensPin, INPUT); //Serial.begin(9600); } void loop() { //Debug 1 /*if(Serial.available() > 0){ off(); A = Serial.parseFloat(); } //Serial.println(A); disp(A);*/ //Main Software if(digitalRead(sensPin)){ //You might want to add a ! before digitalRead() if the polarity of your sensor is reversed. This depends on how you connected the sensor to your board. Check if the display goes to 0 after a while. If it doesnt change it T = mags * (millis() - prevTime); //Physics time prevTime = millis(); u = ((2 * pi * R) / T) * 3600; //Those physics again while(digitalRead(sensPin)) disp(u); //Wait until the magnet is past the sensor (The ! before the digitalRead() goes here too) } if(millis() - prevTime > 4000/mags) u = 0; //Set velocity to 0 if no disp(u); //display the velocity //Debug 2 /*disp(j); if(millis() - prevTime >= 100){ j++; if(j == 999) j = 0; prevTime = millis(); }*/ } //Turns off the ssd void off () { for(int i = 0; i < 4; i++){ digitalWrite(commons[i], LOW); digitalWrite(BCD[i], HIGH); } digitalWrite(dot, HIGH); } //Breakes float to digits (12.3 -> 1 2 3) int shatter (double x, int y) { x = x * 10; switch (y){ case 0: //Serial.print(((int)x / 100) % 10); return ((int)x / 100) % 10; case 1: //Serial.print(((int)x / 10) % 10); return ((int)x / 10) % 10; case 2: //Serial.println((int)x % 10); return (int)x % 10; } } //Writes a digit on a specific position on the ssd void bcdWrite (int x, int y) { off(); if (x == 0 && y == 0) return; for (int i = 0; i < digits; i++){ if(i == y){ digitalWrite(commons[i], HIGH); } else { digitalWrite(commons[i], LOW); } } if (y == 1) digitalWrite(dot, LOW); for (int i = 0; i < 4; i++){ digitalWrite(BCD[i], D2B[x][i]); } } //displays a float on the ssd void disp (double x) { for(int i = 0; i < digits; i++){ bcdWrite(shatter(x, i), i); delay (1000 / freq); } }