I have posted previously on the forum for the same project when I had a different problem(Pwm controlled led low frequency flicker]
The project is an Arduino controlled lights system for an rc plane which includes - navigation lights, strobe lights, beacon lights, afterburner effect and (yet to work at the same time as the previous) gun firing effect.
The new (and unrelated to previous issue) is the combination of the base sketch with the next sketch I'm adding onto the end in order to add the next functional element - which is a gun flashing effect that is called for after a switch on an external remote is triggered.
The idea of the code for the gun is to have the momentary switch start a sequence of code (by setting the bool firing to true ) then to have the sequence - zero delay flash led to start for 60 status changes then to set the bool firing back to false.
the switch input is a pwm input - so bool firing is set to true when the duty cycle is 80% (1800)
So I have been able to get this pwm gun firing sketch to work independently.
I have also got the base sketch with a pwm afterburner to work independently.
However when combined the resulting sketch does not work properly only one of the pwm controlled functions work, which one works appears to be randomly selected after the Arduino is powered up(plugged in)
Gun
input pin - 3
output pin -4
afterburner
input pin - 2
output pin - 5
gun sketch-
const char ledpingun = 4;
bool blinkinggun = true;
byte blinksgun = 0;
int ch5; //create value to keep value of switch channel
bool firing = false;
//delay stuff
unsigned long previoustimegun = 0;
int eventintervalgun = 5;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // what does this do?
pinMode(3, INPUT); //gunchannel
pinMode(4, OUTPUT); //gun
}
void loop() {
//gun
ch5 = pulseIn(3, HIGH , 25000); //read the value of the channel on pin 3
ch5 = constrain(ch5, 1000, 2000);
unsigned long lasteventgun;
unsigned long toploopgun = millis();
unsigned long currenttimegun = millis();
if(currenttimegun - previoustimegun >= eventintervalgun){//this introduces delay to function to prevent errors
if (ch5 >= 1800) {
firing = true;} // this sets firing to true as soon as pulse of 1800ms is achieved and does not set to false until all following blinks have been achieved
if (firing) { //if firing is true do following code
if (blinkinggun) {//if blinksgun is true do following code
if (toploopgun - lasteventgun >= 20) { //period between status change of ledpingun
lasteventgun = toploopgun;
digitalWrite(ledpingun, !digitalRead(ledpingun));//short for...
if (++blinksgun >= 60) {//dtermines how many time the ledpingun will change staus x/2 gives number of light flashes
blinkinggun = false; //tells the loop that the gun has done all flashes
blinksgun = 0; //set number of blinks back to 0 in preperation for the next use of the gun
firing = !firing; // resets the orional loop to false to say that the code has run its course and is ready for next activation
//reset millis()
}
}
} else if (toploopgun - lasteventgun >= 800) {
blinkinggun = true;
}
previoustimegun = currenttimegun;
}
}
}
base sketch with afterburner and other simple led functions(strobe, nav light, beacon)
this code has the integrated gun sketch and DOES NOT WORK
//this is strobe
const char ledPinstrobe = 7;
bool blinkingstrobe = true;
byte blinksstrobe = 0;
//fade beacon
int value = 2;
int ledpin = 6;
long time=0;
int periode = 2000;
//afterburner
int ch1; // create a value to keep the rc values
int valueab = 0;
unsigned long previoustime = 0;
int eventinterval = 50;
//this is gun
const char ledpingun = 4;
bool blinkinggun = true;
byte blinksgun = 0;
int ch5; //create value to keep value of switch channel
bool firing = false;
unsigned long previoustimegun = 0;
int eventintervalgun = 5;
void setup() {
pinMode(2,INPUT);//afterburner channel
pinMode(3,INPUT);//gunchannel
pinMode(4,OUTPUT);//gun
pinMode(5,OUTPUT);//afterburner
pinMode(6,OUTPUT);//beacon
pinMode(7,OUTPUT);//strobe
pinMode(8,OUTPUT);//nav light
digitalWrite(8,HIGH);//nav lights on
//afterburner
Serial.begin(9600); // what does this do?
}
void loop() {
//strobe
static unsigned long lastEventstrobe;
unsigned long topLoopstrobe = millis();
if (blinkingstrobe) {
if (topLoopstrobe - lastEventstrobe >= 50) {
lastEventstrobe = topLoopstrobe;
digitalWrite(ledPinstrobe, !digitalRead(ledPinstrobe));//short for "if (digitalRead(ledPin) == HIGH) {
// digitalWrite(ledPin, LOW);
//} else {
// digitalWrite(ledPin, HIGH);
// }
if (++blinksstrobe >= 4) {//if the last event was the top loop-850ms (off) it will blink on(50)-off(50)-on(50)-off(50)
blinkingstrobe = false;
blinksstrobe = 0;
//reset millis()
}
}
} else if (topLoopstrobe - lastEventstrobe >= 800) {//this 800 +4x50 =1000
blinkingstrobe = true;
}
//beacon fade
time = millis();
value = 128+127*cos(2*PI/periode*time);
analogWrite(ledpin, value); // sets the value (range from 0 to 255)
//afterburner
unsigned long currenttime = millis();
ch1 = pulseIn(2, HIGH ,25000); //read the width of each channel
if(currenttime - previoustime >= eventinterval){
if (ch1>=1600){
valueab = ch1;
valueab = constrain(valueab,1600,2000);
valueab = map(valueab, 1600,2000,0,255);
analogWrite(5,valueab); //sets pwm value from 0 to 255 //led afterburner
previoustime = currenttime;
}else{
analogWrite(5,0);
previoustime = currenttime;}
}
//gun
ch5 = pulseIn(3, HIGH ,25000);//read the value of the channel on pin 3
ch5 = constrain(ch5,1000,2000);
unsigned long lasteventgun;
unsigned long toploopgun = millis();
unsigned long currenttimegun = millis();
if(currenttimegun - previoustimegun >= eventintervalgun){//this introduces delay to function to prevent errors
if(ch5 >= 1800){
firing = true;} // this sets firing to true as soon as pulse of 1800ms is achieved and does not set to false until all following blinks have been achieved
if(firing){
if (blinkinggun) {
if (toploopgun - lasteventgun >= 20) { //period between status change of ledpingun
lasteventgun = toploopgun;
digitalWrite(ledpingun, !digitalRead(ledpingun));//short for...
if (++blinksgun >= 30) {//dtermines how many time the ledpingun will change staus x/2 gives number of light flashes
blinkinggun = false; //tells the loop that the gun has done all flashes
blinksgun = 0; //set number of blinks back to 0 in preperation for the next use of the gun
firing = false; // resets the orional loop to false to say that the code has run its course and is ready for next activation
//reset millis()
}
}
} else if (toploopgun - lasteventgun >= 800) {//this 800 +4x50 =1000
blinkinggun = true;
}
previoustimegun = currenttimegun;
}
}
}