Variable ESC speed with only 1 button...

So I have been here for help about 2 weeks back, where I got some really nice help from a guy (UKHeliBob- really patient guy) and finalized the code for me, however the code doesn't want to work for some reason. The idea is to have a code that (when the Arduino is connected to power) would arm the ESC with a 975 us to 1000 us pulse, then with each press of the one button, the Arduino would send out a pulse that would increase(higher speed) every time it would be pressed. There would be five speeds, 1000- which is the automatic arming pulse, 1250 us which is one-quarter speed, 1500 us which is half the speed, 1750 us which is three-quarters speed, 2000 us which is max speed and then after the 2000 us pulse an additional press would cause the whole thing to go to the start.PLEASE, JUST FOR YOUR INFO I AM THE MOST ABSOLUTE BEGINNER YOU CAN IMAGINE.

#include <Servo.h>
Servo m1;
const byte buttonPin = A2;
const int stratPin = A3;
const int ledPin = 9;
int writeValues[5] = {1000, 1250, 1500, 1750, 2000};
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;

void setup()
{
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT_PULLUP);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{
  if (stratPin == HIGH); // A3 is connected to 5 volt, so t should spit the 1000 us right away...
  m1.writeMicroseconds(1000);

  delay(1000);

  {
    buttonState = digitalRead(buttonPin);
    if (buttonState != lastButtonState)
    {
      // if the state has changed, increment the counter
      if (buttonState == LOW)
      {
        // if the current state is HIGH then the button went from off to on:
        buttonPushCounter++;
        Serial.println(buttonPushCounter);
        Serial.print("buttonPushCounter % 5 = ");
        Serial.println(buttonPushCounter % 5);
        Serial.print("writing ");
        Serial.println(writeValues[buttonPushCounter % 5]);
      }
      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);
    }
    lastButtonState = buttonState;
    m1.writeMicroseconds(writeValues[buttonPushCounter % 5]);
  }
}

Now we know what the program is supposed to do. If we knew what it actually did, instead, we could help you fix your expectations to match what the code actually does, or suggest how to change the code to match your expectations.

  if (stratPin == HIGH); // A3 is connected to 5 volt, so t should spit the 1000 us right away...

It is extremely rare to see a properly written if statement end with a semicolon.

That is equivalent to

  if (stratPin == HIGH)
  {
     ; // This is the do nothing symbol
  }
  {
    buttonState = digitalRead(buttonPin);

Why is that useless curly brace there?

PaulS the code does absolutely nothing, and the useless curly brace is a part of the example code provided by Arduino (StageChangeDetection), I and the other people just edited the code. Again it does nothing.
The only thing that it does right is posting the values in Serial.print... :stuck_out_tongue:

Your last thread contained UKHeliBob's version of the code. That made a lot more sense than your current code does and didn't contain the errors that yours does.

So post that version here and tell us exactly what that does and what changes you need to make to it and why. Then we might be able to help.

Steve

This is the UKHeliBob code, the problem here is that it does not do anything except post things in Serial. print. And also it doesn't automatically give the arming pulse the 975 us, that is probably the main problem is that is what I tried to solve with the thing you were pointing out...

#include <Servo.h>
Servo m1;
const byte buttonPin = A2;
const int ledPin = 9;
int writeValues = {1000, 1250, 1500, 1750, 2000};
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;

void setup()
{
 // initialize the button pin as a input:
 pinMode(buttonPin, INPUT_PULLUP);
 // initialize the LED as an output:
 pinMode(ledPin, OUTPUT);
 // initialize serial communication:
 Serial.begin(9600);
}

void loop()
{
 buttonState = digitalRead(buttonPin);
 if (buttonState != lastButtonState)
 {
   // if the state has changed, increment the counter
   if (buttonState == LOW)
   {
     // if the current state is HIGH then the button went from off to on:
     buttonPushCounter++;
     Serial.println(buttonPushCounter);
     Serial.print("buttonPushCounter % 5 = ");
     Serial.println(buttonPushCounter % 5);
     Serial.print("writing ");
     Serial.println(writeValues);
   }
   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);
 }
 lastButtonState = buttonState;
 m1.writeMicroseconds(writeValues);
}

No that isn't UKHeliBob's code. That's your mangled version of it. The original contained 'int writeValues[5] = {1000, 1250, 1500, 1750, 2000};' which is correct. Also ' Serial.println(writeValues[buttonPushCounter % 5]);' and ' m1.writeMicroseconds(writeValues[buttonPushCounter % 5]);' which are both needed.

If you test THAT version checking to see if it prints to the monitor the right values at the right times and perhaps initially using a servo in place of the ESC I think you'll find it works pretty well.

So try again to find the correct code, give it a try and report back what you see on Serial Monitor (and what a servo does if you have one to test with).

So then the other thing you may need is the ESC arming code. And since that only needs to run once it should be in setup() which automatically only runs once not in loop(). That way you don't need any flags to tell it about startup which what I guess you were trying to do with stratPin.

Steve

unfortunately, with the servo it didn't work ...

Matyk:
unfortunately, with the servo it didn't work ...

Quite possibly because al the code you have listed lacks a statement attaching the Servo object to any pin on the arduino.

Add the following line to your setup function:-

    m1.attach(servoPin);

and add the following definition at the start of your file:-

int servoPin = X;    // N.B. Replace X with the pin number you have connected to your servo!!!!!

These changes are in addition to the ones noted by slipstick in reply #6.

Ian

Guys I would like to really thank you because the thing is now working. seriously a huge appreciation to all of yall. have a great day!! :slight_smile: