Can't go on in the program

Hi,

I’m new here and I have a question about my program.

I have a while loop in void setup() to check the buttonstate to execute/send a value. I need that to stay in the loop, until the button is pressed. But it goes not on and so the LED doesn’t lit up. :confused: And my question is, how I could fix this.

#include <ESC.h>

// Pin for the LED
#define SPEED_MIN (1000)                                          // Set the Minimum Speed in microseconds
#define SPEED_MAX (2000)                                          // Set the Minimum Speed in microseconds
#define ARM_ESC (0)                                               //Arm Pin for arming ESC
ESC myESC (2, SPEED_MIN, SPEED_MAX, 500);                         // ESC_Name (ESC PIN, Minimum Value, Maximum Value, Default Speed, Arm Value)
int oESC;                                                         // Variable for the speed sent to the ESC
int buttonstate = digitalRead(ARM_ESC);
void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);                                   // LED Visual Output
  pinMode (ARM_ESC, INPUT);
  while (true) {
    if (buttonstate == LOW) {                                       //Checks, if the arming button is pressed
      myESC.arm();                                                // Send the Arm value (500) so the ESC will be ready to take commands
      digitalWrite(LED_BUILTIN, HIGH);                            // LED High Once Armed
      delay (5000);                                               // Wait for a while
      break;
    } 
  }
}
void loop() {
  

}

Greetings
PS: Forgot code tags
Mainsedora

ESC_Ramp.ino (1.57 KB)

while (true)By definition this while loop can never end because true can never be false

Change the while condition to check buttonState instead of using if, but you must read the state of the button in the while loop or it will never change

Let’s say a switch press gives a HIGH.

Add to while():
if(digitalRead(buttonstate) == HIGH)
{
break;
}

What if I declare the buttonstate as a Integer and give him the value 0 and then I write in the while statement digitalRead(buttonstate) == HIGH. Can it go wrong?

Greeting

Mainsedora

Can it go wrong?

Why don't you try it and see ?

Mainsedora:
What if I declare the buttonstate as a Integer and give him the value 0 and then I write in the while statement digitalRead(buttonstate) == HIGH. Can it go wrong?

Greeting

Mainsedora

I'd call it "buttonPin".
buttonState implies, to me, the instantaneous value read back.
Pins zero and one are best reserved for serial comms.

But I have already defined ARM_ESC on Pin 1. So can I delete the buttonPin?

#include <ESC.h>

// Pin for the LED
#define SPEED_MIN (1000)                                          // Set the Minimum Speed in microseconds
#define SPEED_MAX (2000)                                          // Set the Minimum Speed in microseconds
static int ARM_ESC = 1;                                               //Arm Pin for arming ESC
ESC myESC (2, SPEED_MIN, SPEED_MAX, 500);                         // ESC_Name (ESC PIN, Minimum Value, Maximum Value, Default Speed, Arm Value)
int oESC;                                                         // Variable for the speed sent to the ESC
int buttonPin = 0;
void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);                                   // LED Visual Output
  pinMode (ARM_ESC, INPUT);
  while (digitalRead(buttonPin) == LOW) {                       //Checks, if the arming button is pressed
      myESC.arm();                                                // Send the Arm value so the ESC will be ready to take commands
      digitalWrite(LED_BUILTIN, HIGH);                            // LED High Once Armed
      delay (5000);                                               // Wait for a while
      break;
  }
}
void loop() {
  
Serial.println(buttonPin);
}

Pin one is used for what other purpose?

AWOL:
Pin one is used for what other purpose?

Well, I should delete buttonPin and instead using the ARM_ESC Pin, because one of the pins are unused and the ARM_ESC Pin has the purpose to be used for arming an ESC.