i saw that example.
here is a code i modified slightly. it is actually a speedometer for bicycles.
i replaced reed switch with a hall sensor on pin 2. i am using NANO and it must give out when hall sensor is pulsing and it should stop when it is low for a duration. hall sensor has a built in led so i can see its activity.
there is serial print out to see hall sensor activity.
problem is, this gives the desired voltage to output pin but does not care if INPUT voltage is really present or not.
i need to supply the arduino input with 2 3 4 volts, these volts will determine the output voltage on PWM pin consequently. ( now it gives 1.76 volts for 2 volt input even there i no input connected) because condition for panel input voltage is <= 90 (8 bit) volts. now it is 0 volts because of test bench has no input from panel.
if no hall sensor movement, it gives 0 volts as expected.
int valu = 0;
int panel = 11; //panel voltage input pin 2 3 4 volts preset.
int thr = 6; //voltage output
int THR1 = 90; // output preset voltage. approx 1.7 volts 8 bit
int THR2 = 130; // 2.7 volts
int THR3 = 185; // 4.3 volts
int PNLLO = 420; // constant signal from control panel 2 volt. 10 bit
int PNLMD = 630; // 3 volt. 10 bit
int PNLHI = 800; // 4 volt. 10 bit
float rotationTime;
float distanceTravelledPerSecond;
float wheelCircumference = 2; // Circumference of the wheel
float RPM;
float speeds;
float maximumSpeed;
unsigned int distanceTravelled;
//unsigned int averageSpeed; // not needed for now
unsigned long revoloutions = 0;
unsigned long startRotation = 0;
unsigned long endRotation = 0;
unsigned long timeInSeconds;
unsigned long timeInMilliseconds;
unsigned long lastMillis;
boolean reedSwitchState;
boolean lastReedSwitchState;
const int reedSwitchPin = 2;
int buttonState;
int val;
int val2;
void DoTimeAndDistanceCalcs()
{
revoloutions ++;
endRotation = millis();
rotationTime = endRotation - startRotation;
startRotation = endRotation;
RPM = (600 / rotationTime);
distanceTravelled = revoloutions * wheelCircumference / 100;
distanceTravelledPerSecond = RPM * wheelCircumference;
distanceTravelledPerSecond = distanceTravelledPerSecond / 1000 * 60;
speeds = distanceTravelledPerSecond;
if (speeds > maximumSpeed)
maximumSpeed = speeds;
timeInSeconds = millis();
timeInSeconds = timeInMilliseconds / 1000;
//averageSpeed = (distanceTravelled / timeInSeconds); //not needed for now
}
void setup() {
pinMode(reedSwitchPin, INPUT);
Serial.begin(9600);
buttonState = digitalRead(reedSwitchPin);
// lcd.begin(16,4);
}
void loop() {
valu = analogRead(panel);
val = digitalRead(reedSwitchPin);
delay(10);
val2 = digitalRead(reedSwitchPin);
if (val == val2) {
if (val != buttonState) {
lastReedSwitchState = reedSwitchState;
reedSwitchState = val;
if ((lastReedSwitchState == HIGH) && (reedSwitchState == LOW) && (valu <= PNLLO)) {
DoTimeAndDistanceCalcs();
analogWrite(thr, THR1); // this supplies THR1 preset voltage to output pin
// lcd.setCursor(8,3); from original code
}
else {
DoTimeAndDistanceCalcs();
delay(1500);
analogWrite(thr, 0); // if no activity on hall sensor gives 0 volts to output.
}
}
}
buttonState = val ;
Serial.println(digitalRead(reedSwitchPin)); //halls sensor activity
}
i need to add two other conditions for my panel voltages. 2 is already written in sketch.
but i cant add 3 volts and 4 volts. which are THR2 and THR3.
i need to tell it to
A)output THR2 if there is hall sensor activity and if panel supplies 3 volts
B)output THR3 if there is hall sensor activity and if panel supplies 4 volts to arduino input pin.
what ever i do, it acts weird.it tries to give 1.76 volts and other preset voltages in a loop. or i am missing somehting? could you please help to add these to my sketch?
if we can manage to do this, whenever i supply appropriate voltage to arduino, arduino will give correct preset output votlages as long as hall sensor is pulsing.