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)