So I'm working on an Arduino project, specifically a project that would control an ESC. What I want to do is basically to use pulls time or millis to generate a PWM signal, that is firstly neutral for the ESC (1500) and when a button would be pushed it would add 200, so 1700, 1900. I have already made a code that makes a signal depending on a button, however, it is quite inconvenient to have 4 buttons under one hand, if this code would work I would basically just click the one button until the desired speed.
I have come this far, however, I am a complete amateur so I am having a lot of issues...
Copy and paste your code. A picture of code will not compile. Use the </> button (in the control bar above the posting window) to mark your code as code.
Servo m1;
int pushButton1 = A2;
int pushButton2 = A3;
int pullstime = millis;
int pushpulls = 10;
void setup() {
// put your setup code here, to run once:
šq+< ě m1.attach(9);
delay(1);
m1.write(40);
}
void loop() {
if (digitalRead(pushButton1) == HIGH > LOW) { //Opens the door when the 'open' button is pressed
m1.attach(9); //Gives the servo power
m1.writeMicroseconds(1500); //Tells servo to move to 0 degree mark
delay(1); // waits 1000ms for the servo to reach the position
} else {
m1.writeMicroseconds(1000);
delay(1);
{
if (digitalRead(pushButton2) == HIGH > LOW) { //Opens the door when the 'open' button is pressed
m1.attach(9); //Gives the servo power
m1.writeMicroseconds(2000); //Tells servo to move to 0 degree mark
delay(1); // waits 1000ms for the servo to reach the position
} else {
m1.writeMicroseconds(1000);
delay(1);
{
if (digitalRead(pushpulls) == HIGH > LOW) {
m1.attach(9);
m1.write( pullstime + 0.2)
or millis == 1500 + 0.2;
if (digitalRead(pushpulls) == HIGH > LOW) {
m1.attach(9);
m1.write( pullstime - 0.2);
or millis == 1500 - 0.2;
If you mean the </> icon, you need to be in Reply not Quick Reply, although you can enable the icons in Quick Reply by editin the settings in your Profile.
But if all you want to do is have one button increment something each time it's pressed, you could do worse than look at the State Change Detect example (also in the IDE at File > Examples...).
(Although the example shows the button wired to 5V with a pull down resistor to ground. Much easier to use the internal pullup, and a button press is then a low.)
# include <Servo.h>
Servo m1;
int pushButton1 = A2;
int pushButton2 = A3;
int pullstime = millis;
int pushpulls = 10;
void setup() {
// put your setup code here, to run once:
m1.attach(9);
delay(1);
m1.write(40);
}
void loop() {
if (digitalRead(pushButton1) == HIGH) { //Opens the door when the 'open' button is pressed
m1.attach(9); //Gives the servo power
m1.writeMicroseconds(1500); //Tells servo to move to 0 degree mark
delay(1); // waits 1000ms for the servo to reach the position
} else {
m1.writeMicroseconds(1000);
delay(1);
{
if (digitalRead(pushButton2) == HIGH) { //Opens the door when the 'open' button is pressed
m1.attach(9); //Gives the servo power
m1.writeMicroseconds(2000); //Tells servo to move to 0 degree mark
delay(1); // waits 1000ms for the servo to reach the position
} else {
m1.writeMicroseconds(1000);
delay(1);
{
if (digitalRead(pushpulls) == HIGH) {
m1.attach(9);
m1.write( pullstime + 0.2);
{
if (digitalRead(pushpulls) == HIGH) {
m1.attach(9);
m1.write( pullstime - 0.2);
}
}
}
}
}
}
}
}[code]
I have looked at the stage change detect and it looks good, but I don't know how I would tell it to change the pulse time (1500) with each click so that the ESC understands it
Matyk:
but I don't know how I would tell it to change the pulse time (1500) with each click
In the example, the counter is incremented (just as an example of what you can do) each time the button is clicked. So in there, you do the same with your value, I forget what it's called, but something like:
//The circuit:
// - pushbutton attached to pin 2 from +5V
//- 10 kilohm resistor attached to pin 2 from ground
// - LED attached from pin 13 to ground (or use the built-in LED on most
// this constant won't change:
#include <Servo.h>
Servo m1;
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
// turns on the LED every four button pushes by checking the modulo of the
// button push counter. the modulo function gives you the remainder of the
// division of two numbers:
if (buttonPushCounter % 1 == 0) { //Opens the door when the 'open' button is pressed
m1.attach(9); //Gives the se
m1.writeMicroseconds(1500); //Tells servo to move to 0 degree mark
delay(1); // waits 1000ms for the servo to reach the position
} else {
m1.writeMicroseconds(1000);
delay(1);
if (buttonPushCounter % 2 == 0) { //Opens the door when the 'open' button is pressed
m1.attach(9); //Gives the se
m1.writeMicroseconds(1700); //Tells servo to move to 0 degree mark
delay(1); // waits 1000ms for the servo to reach the position
} else {
m1.writeMicroseconds(1000);
delay(1);
if (buttonPushCounter % 3 == 0) { //Opens the door when the 'open' button is pressed
m1.attach(9); //Gives the se
m1.writeMicroseconds(2000); //Tells servo to move to 0 degree mark
delay(1); // waits 1000ms for the servo to reach the position
} else {
m1.writeMicroseconds(1000);
delay(1);
}
}
}