please help add freq counter to program

OK, I spent some time messing with the Arduino. I’m just not getting my head wrapped around some of the code.

Attached is the code I’m using now. It’s pretty simple, It has one output, a servo signal. There’s 2 inputs, a switch and an analog input.

Here’s how it operates right now-

When the Arduino is started, It will output a 1000 ms pulse.

When the switch is closed, the output will be anywhere from 1200 to 2000 ms pulse depending on the voltage on the analog input 0 to 5 volts which comes from a twist grip throttle off an electric scooter.

If at any time the switch is opened, the output must drop to 1000 ms.

That’s all, pretty simple? The code was originally from the arduino website it was a servo tester. Someone on the forum helped me get the switch working.

This is what I need- I have a sensor that outputs a beautiful square wave 0 to about 4.5 volts. I need to hook the sensor to one of the inputs. When the input sees 950 hz,or more, I need the servo pulse to go to 1400ms for about 5 seconds.

I’ll use D3 for the sensor input. What would be the simplest way to modify the existing code? I know I have to define the input but where I have no clue is how to make the arduino see this as a frequency and then respond when the frequency hits a certain level.

#include <Servo.h> 


const int buttonPin = 2;        // the number of the pushbutton pin
const int potPin =  0; // the number of the potentiometer pin (Analog 0)
const int servoPin = 12; // Servo control line

const int ServoIdle = 1000;      // Servo position when idling in ms.
const int ServoMin = 1200;      // Minimum pulse width for servos in ms.
const int ServoMax = 2000;      // Maximum pulse width for servos in ms.
const int DebounceDelay = 100; 

Servo myservo; 

void setup() {
        // initialize the pot and button pin as an input:
        pinMode(potPin, INPUT);
        pinMode(buttonPin, INPUT);
        // Activate internal pull up resistor for button
        digitalWrite(buttonPin, HIGH);

        // Initialise Servos. First set it to idle position, then attach to prevent movement on startup
        myservo.writeMicroseconds (ServoIdle);
        myservo.attach (servoPin);
}

void loop() {
    static unsigned long debounce = 0;         // variable for reading the pushbutton status
    static int buttonstate = HIGH;         // variable for reading the pushbutton status

    // Read button and debounce.
    int buttonnow = digitalRead(buttonPin);
    if (millis() - debounce >= DebounceDelay
        && buttonnow != buttonstate) {
        buttonstate = buttonnow; 
        debounce = millis();

        if (buttonstate == HIGH) {
             // The button has just been released - move back to idle
             myservo.writeMicroseconds (ServoIdle);
        }
    }

    // If button is pressed, track potentiometer
    if (buttonstate == LOW) {
        int potnow = analogRead (potPin);      // Read potentiometer
        myservo.writeMicroseconds (map (potnow, 0, 1023, ServoMin, ServoMax));    // Set Servo
        delay (50);    // Wait a little to give servo time to move
    }
}

it's for this-

throttle.pde (1.78 KB)

Google "FreqCounter.h arduino"

It's lastched to D5 so you'll need to move your sensor input there.

here's some on-board video of the bike-

Greetings,

I am also using Nawrath's "FreqCounter.h" I am using the following demo code:

// Frequency Counter Lib example

/*
  Martin Nawrath KHM LAB3
  Kunsthochschule f¸r Medien Kˆln
  Academy of Media Arts
  http://www.khm.de
  http://interface.khm.de/index.php/labor/experimente/	
 */
#include <FreqCounter.h>


unsigned long frq;
int cnt;
int pinLed=13;

void setup() {
  pinMode(pinLed, OUTPUT);

  Serial.begin(115200);        // connect to the serial port

  Serial.println("Frequency Counter");

}



void loop() {

  // wait if any serial is going on
  FreqCounter::f_comp=10;   // Cal Value / Calibrate with professional Freq Counter
  FreqCounter::start(100);  // 100 ms Gate Time

  while (FreqCounter::f_ready == 0) 

  frq=FreqCounter::f_freq;
  Serial.print(cnt++);
  Serial.print("  Freq: ");
  Serial.println(frq);
  delay(100);  //original delay delay(20);
  digitalWrite(pinLed,!digitalRead(pinLed));  // blink Led

}

Which is the example.

When I put 280 Hz in I get a reading of 28.

When I put 28,000 Hz in I get a reading of 2,800.

How should I correct for this factor of 10 error?

Thanks and...

Kindest Regards,

Paul Flint