PDA

Επιστροφή στο Forum : εύκολο στροφόμετρο για arduino



apollonic
03-03-15, 09:03
// a simple and inexpensive rev-counter (strofometer)
// use one photoresistor to +5V, in series with 10KΩ that
// goes to earth and from the joind point one cable to ledPin Analog input
// No need to use interrupts or calibration
// you just put a white insulation tape to the drill and lighten it with a strong lamp
// or perhaps with a strong LED placed next to photoresistor
// By Prodromos Savaidis (apollonios)
// March, 1, 2015
// savaidis.makis@gmail.com


#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x38,16,2);
int V0, V1, V2, V3, rnd, rpm, K;
long currtime, prevtime, cnt, cnt_old, sum, sum_rpm, mil1, mil2;
int ledPin = 5; // STATUS LED
double seconds;
void display()
{
lcd.setCursor(0,0); lcd.print("V2="); lcd.print(V2);
lcd.print(" V3="); lcd.print(V3);lcd.print(" ");
}

void setup() {

rnd=0; cnt_old=0; sum=0;
currtime = millis(); // GET CURRENT TIME millis = the milliseconds from program starttime
pinMode(ledPin, INPUT);
lcd.init();
lcd.setCursor(0,1); lcd.print("round meter");
lcd.setCursor(0,0); lcd.print(" currtime ");
lcd.print( currtime); prevtime=currtime;
delay(3000);

V1= analogRead(ledPin);
V2=V1 * 0.9; V3=V1*1.1; // limits to triger
currtime = millis(); // GET CURRENT TIME
mil1 = currtime;
lcd.setCursor(0,1); lcd. print(" ");
lcd.setCursor(0,1); lcd.print("V="); lcd.print(V1);
V0=V1;
display();

}

void loop()

{

V1= analogRead(ledPin);
if (V1<V2 || V1>V3) // out of limits
{ if (V1<V2) // darker - for testing only must be >
{
lcd.setCursor(0,0);
rnd++;

cnt=0;
if (rnd==10)
{
mil2 = millis(); // cnt revolutions ind mil2-mil1 milliseconds
seconds=(mil2-mil1); seconds=seconds/1000; // needed to avoid truncion
lcd.setCursor(0,1); lcd.print("m2="); lcd.print( mil2);lcd.print(" sec"); lcd.print(seconds);lcd.print(" ");

// in rnd rounds in seconds
// in X? rnd in 60sec
rpm= rnd*60; rpm=rpm/seconds; mil1=mil2;

lcd.setCursor(0,0);

lcd.print(" rpm=");

lcd.print(rpm);lcd. print(" ");
sum_rpm=0; sum=0; rnd=0;

}
}
V1= analogRead(ledPin);
V2=V1*0.8; V3=V1*1.2; // new limits


}

}