Adri67
Junior Member level 2
- Joined
- Aug 11, 2014
- Messages
- 24
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 267
20180120
I am designing a remote/auto start for 5kVA generator (gen) already fitted with 12V electric start. The gen is fitted with a choke lever which must be "played" progressively from fully closed while cranking, to fully open when full speed (3,000 RPM = 50Hz) is achieved. I have fitted a small RC-type servo (6Vdc / 50Hz / 180 degree = 1...2mS, etc). My sketch contains the "Knob"(Servo) part which works fine with a pot input.
I have added a frequency counter to translate the motor speed which also works well displaying frequency.
My problem is to USE the frequency output (Freq or FREQ) in order to control the servo satisfactority instead of the pot. I cannot get the frequency signal "out". D5 output with present sketch is 0 for 0Hz and 1 for any output above 0Hz.
What am I doing wrong to not achieve an output of 0... 1023 instead of 0...1?
I am designing a remote/auto start for 5kVA generator (gen) already fitted with 12V electric start. The gen is fitted with a choke lever which must be "played" progressively from fully closed while cranking, to fully open when full speed (3,000 RPM = 50Hz) is achieved. I have fitted a small RC-type servo (6Vdc / 50Hz / 180 degree = 1...2mS, etc). My sketch contains the "Knob"(Servo) part which works fine with a pot input.
I have added a frequency counter to translate the motor speed which also works well displaying frequency.
My problem is to USE the frequency output (Freq or FREQ) in order to control the servo satisfactority instead of the pot. I cannot get the frequency signal "out". D5 output with present sketch is 0 for 0Hz and 1 for any output above 0Hz.
What am I doing wrong to not achieve an output of 0... 1023 instead of 0...1?
Code:
// 20180120 - no FREQ output D5
#include <LiquidCrystal595.h>
#include <Servo.h>
Servo myservo;
int potpin = 0; // analog pin A0 for pot
int val; // variable to read the value from the analog pin
LiquidCrystal595 lcd (7, 8, 6);
const int ledPin = 13 ;
int ledState = LOW ;
long previousMillisA = 0 ;
long wait = 400 ;
float FREQ = 0 ;
// float Freq = 0 ;
#define MainPeriod 400 // was 1000
long previousMillis = 0 ;
volatile unsigned long duration = 0 ;
volatile unsigned int pulsecount = 0 ;
volatile unsigned long previousMicros = 0 ;
void setup() {
pinMode (ledPin, OUTPUT) ;
pinMode (FREQ, OUTPUT) ;
myservo.attach (10); // connects the servo to pin 10
lcd.begin (20, 4);
attachInterrupt (0, myinthandler, RISING) ; // input D3
}
void loop() {
val = analogRead (potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map (val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write (val); // sets the servo position according to the scaled value
delay (15); // waits for the servo to get there
unsigned long currentMillisA = millis();
if (currentMillisA - previousMillisA > wait) {
previousMillisA = currentMillisA;
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite (ledPin, ledState);
}
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= MainPeriod)
{
previousMillis = currentMillis;
unsigned long Aduration = duration;
unsigned long Apulsecount = pulsecount;
duration = 0;
pulsecount = 0;
float Freq = 1e6 / float(Aduration);
Freq *= Apulsecount;
FREQ = Freq ;
if (isnan (FREQ)) FREQ = 0 ; // to remove "nan" when near 0
digitalWrite (5, FREQ) ;
lcd.setCursor (2, 0) ;
lcd.print (Freq) ; // for testing only - shows "nan"
lcd.setCursor (2, 1) ;
lcd.print (FREQ) ;
}
}
void myinthandler()
{
unsigned long currentMicros = micros();
duration += currentMicros - previousMicros;
previousMicros = currentMicros;
pulsecount++;
}