How to replace the press button to a RC PWM signal EX:
if(ch1>1000){digitalWrite(11, HIGH);}
if(ch1<1600){digitalWrite(11, LOW);}
if(ch1<1600){digitalWrite(13, HIGH);}
if(ch1>1000){digitalWrite(13, LOW);}
In the program below
I wanted to make an LED come on when you press a button and stay on, then once you press the button again it goes off and stays off until you press the button again.
//Radio Buttons 22
const int redledPin = 11; //LED Pin #
const int greenledPin = 13;
const int redbutton = 2; //button Pin #
const int greenbutton = 3;
char redbuttonstate = 0;
char greenbuttonstate = 0;
unsigned long redbuttoncount = 0; //button debounce timer
unsigned long greenbuttoncount = 0;
//"marker" chooses which counter to check
// by Paul___B of Arduino Forum
boolean timeout(unsigned long *marker, unsigned long interval) {
if (millis() - *marker >= interval) {
*marker += interval; //move on ready for next interval
return true;
}
else return false;
}
//Deal with a button read; true if button pressed and debounce is a new event
//Uses reading of button input, debounce store, state store and debounce interval.
//by Paul___B of Arduino Forum
boolean buttondown(char button, unsigned long *marker, char *buttonstate, unsigned long interval) {
switch (*buttonstate) { //odd states if was presses, >= 2 if debounce in progress
case 0: //button up so far,
if (button == HIGH) return false; //nothing happening!
else {
*buttonstate = 2; // record that is now pressed
*marker = millis(); // note when was pressed
return false; //and move on
}
case 1: //button down so far,
if (button == LOW) return false; // noting happening!
*buttonstate = 3; //record that is now released
*marker = millis(); //note when was released
return false; // and move on
case 2: //button was up, now down.
if (button == HIGH) {
*buttonstate = 0; // jackpot! update the state
return true; //because we have the desired event!
}
else
return false; //not done yet; just move on
case 3: // button was down mow up
if (button == LOW) {
*buttonstate = 1; //no, not debounced; revert the state
return false; // false alarm!
}
else {
if (millis() - *marker >= interval) {
*buttonstate = 0; //debounced; update state
return false; // but not the event we want
}
else
return false; //not done yet; just move on
}
default: //error; recover anyway
{
*buttonstate = 0;
return false; // definitly false!
}
}
}
void setup() {
pinMode(redledPin, OUTPUT);
pinMode(greenledPin, OUTPUT);
pinMode(redbutton, INPUT);
pinMode(greenbutton, INPUT);
digitalWrite (redledPin, LOW);
digitalWrite (greenledPin, LOW);
pinMode(redbutton, INPUT);
digitalWrite(redbutton, HIGH); //enable pullup resistor
pinMode(greenbutton, INPUT);
digitalWrite(greenbutton, HIGH); //enable pullup resistor
}
void loop() {
// Toggle LED if button is debounced
if (buttondown(digitalRead(redbutton), &redbuttoncount, &redbuttonstate, 10UL )) {
if (digitalRead(redledPin))
digitalWrite (redledPin, LOW); else
digitalWrite (redledPin, HIGH);
}
// Toggle LED if button is debounced
if (buttondown(digitalRead(greenbutton), &greenbuttoncount, &greenbuttonstate, 10UL )) {
if (digitalRead(greenledPin))
digitalWrite (greenledPin, LOW); else
digitalWrite (greenledPin, HIGH);
}
}