Shouldn't this work? When i connect the motor to the arduino the motor runs at full speed, no matter if i have pressed the button or not? How to fix that? (If i check for butp==1(button not pressed), would it work?)
#include <Servo.h>
Servo ESC;
int potValue=90;
int but = 4;//button
int butp;
void setup() {
pinMode(but,INPUT);
digitalWrite(but,HIGH);
Serial.begin(9600);
ESC.attach(9,1000,2000);
}
void loop() {
butp=digitalRead(but); // set the butp to 0 when button is pressed
if (butp==0){
ESC.write(potValue);//start the motor
}
}
The button pin needs to be pinMode INPUT_PULLUP, have a external pullup resistor or an external pulldown resistor. Otherwise it is "floating" and its state will be indeterminate. If it works without a pullup or pulldown you are just lucky or I misunderstand the button wiring,
If the button reads 0 the motor turns on. What is there to turn the motor off? Once the motor starts there is nothing that stops it.
Read the forum guidelines to see how to properly post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.
Here is your code formatted and posted properly.
#include <Servo.h>
Servo ESC;
int potValue = 90;
int but = 4;//button
int butp;
void setup()
{
pinMode(but, INPUT);
digitalWrite(but, HIGH);
Serial.begin(9600);
ESC.attach(9, 1000, 2000);
}
void loop()
{
butp = digitalRead(but); // set the butp to 0 when button is pressed
if (butp == 0)
{
ESC.write(potValue);//start the motor
}
}
If it that ESC you write about recently and the motor also you are playing with perhaps the combination needs calibration.
This is a specific set of steps, performed once.
See
for dets.
Also, it is helpful to just write a small program to get values from the serial monitor and publish them to the servo, you can sit there and play and get a feel for things.
You shoukd know how to get numbers from the serial monitor anyway, perhaps you already do.