HI All,
I am new to coding and so far successfully managed to code a nano to trigger a relay followed by a servo motor with delay.
However, i would like to this to trigger from a PWM input on pin 12 count to a predetermined number (approx 150 pulses) and then trigger the below code.
The “button” in the code below was only to try out pulses and count codes from various other projects.
I will still have this button in the project later just to trigger the servo without the relay.
any suggestions where to start or how to achieve this.
#include <Servo.h>
const int buttonPin = 2; //button input on pin 2
const int ledPin = 13; //led on pin 13
const int relayPin = 3; // relay on pin 3
const int pwm = 12; //pwm input
Servo servoA;
int pos = 0;
int OPEN = 2000;
int buttonState = 0;
void setup() {
// inititialize Output Pins:
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(relayPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(pwm, INPUT);
servoA.attach(9);
servoA.write(pos);
}
void loop() {
// READ BUTTON STATE
pulseIn(buttonPin, HIGH);
buttonState = digitalRead(buttonPin);
if (buttonState == LOW){
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite (relayPin, HIGH);
delay(5000);
servoA.write(OPEN);
} else {
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
digitalWrite (relayPin, LOW);
servoA.write(pos);
}
}