Hi guys,
All weekend I`m trying to get my head around how to make this work the way I like to, so I can move next part of the project, but no luck what so ever. I will try get some tips or guidelines here.
In this code that I copied in, Im trying to achieve a function that, when button pressed SW1, SW1_Led comes on and brushless motor start spinning at set rpm. then SW1 pressed again SW1_Led goes off and brushless motor goes off too. and 3rd function I need then SW1 button held for more then second SW1_Led will blink and then enables function to increase or decrease speed of brushless motor.
So issue what I have with current code - When press and hold SW1 Im able to change motor speed but as soon button is released status is changed to on or off and SW1_led not blinking simple because I can get my head around where and how to change the code to make it work.
This code will be used on different buttons and led to adjust setting and values of different outputs on same code.
int SW1 = 8;
int SW1_LED = 13;
int SW1_LED_state = LOW;
int SW1_LED_reading;
int SW1_LED_previous = HIGH;
long time = 0;
long debounce = 500;
float RPM = 1500; //Motor starting RPM
int SW_POS_1 =2; //Button UP
int SW_POS_2 =3; // Button DOWN
int STATE=1; //Part off ESC
int Arming_Time=0; //Part off ESC
int ESC = 9; //Part off ESC digital pin
int Pulse=1000; //Pulse in ms Part off ESC
int maxRot = 1850; //Motor starting RPM
int minRot = 1300; //Motor starting RPM
void setup()
{
pinMode(SW1, INPUT_PULLUP);
pinMode(SW1_LED, OUTPUT);
pinMode(ESC,OUTPUT);
for (Arming_Time = 0; Arming_Time < 500; Arming_Time += 1)
{
digitalWrite(ESC,HIGH);
delayMicroseconds(1100);
digitalWrite(ESC,LOW);
delay(20-(Pulse/1000));
}
Serial.begin(9600);
pinMode(SW_POS_1, INPUT_PULLUP); //SW_POS_1 VALUE UP
pinMode(SW_POS_2, INPUT_PULLUP); //SW_POS_2 VALUE DOWN
}
void loop()
{
SW1_LED_reading = digitalRead(SW1); //SW1= HIGH (OFF)
if (SW1_LED_reading == HIGH && SW1_LED_previous == LOW && millis() - time > debounce ) //if Button Pressed & LED is off & pressed londer then debounce value
{
if (SW1_LED_state == HIGH)
SW1_LED_state = LOW;
else
SW1_LED_state = HIGH;
time = millis();
}
digitalWrite(SW1_LED, SW1_LED_state);
SW1_LED_previous = SW1_LED_reading;
int SW_POS_1 = digitalRead(2);
int SW_POS_2 = digitalRead(3);
if (SW_POS_1 == LOW && SW1_LED_reading == LOW)
{
delay(100);
RPM = constrain (RPM + 10, minRot, maxRot);
}
if (SW_POS_2 == LOW && SW1_LED_reading == LOW)
{
delay(100);
RPM = constrain (RPM - 10, minRot, maxRot);
}
{
digitalWrite(ESC,SW1_LED_state);
delayMicroseconds(RPM);
digitalWrite(ESC,LOW);
}
Serial.println("RPM");
Serial.println(RPM);
}