Hello, I am new to code writing. I have made a few nano and trinket projects. Here is current project. I am trying to decode 6 channels of PWM(PPM) input, and then depending on freq of PWM( on off or in between) I plan to trigger an output pin high or low.
I am able to see the PWM input on NANO D2 perfectly. I can trigger some LEDs at "IDLE" and "FULL THROTTLE" (Full throttle in Forward and reverse, hence placed the ends of PWM freq)
Considerations :
- identify each input /output channel , only one input is linked to one output
- assign an independent function to each output channel (will only be 'on or off' during a specific PWM freq)
- monitor each channel in its own column of necessary width to hold "text", min 6 max 10 columns.
- Each input may have a spectrum of Freq to look at(1000-2000PWM), or just be at freq limits for basic on-off.
Thoughts:
Do I do this in IF-Else statements or should I change to 'switch case' ?
How to space columns for each channel, in order?
It seems pretty clear that the current code will only allow one input channel 'duration' as this has not been declared appropriately for any other channel than PWM0, adn I do not know how to define it for the other channels.
My current basic code does this perfectly for one channel. It inserts the "txt" followed by the 'duration' just like I want. But I feel that I am going down the wrong path overall.
I provided a jpg of the serial monitor in action as I throttle up the PWM freq.
Thank you in advance for any tips
[code]
// Sketch reads RC PWM (PPM) input pin 2,3,4,5,6,7 and
// can check pulse width millis on serial monitor, maybe output
// LED to denote certain PWM freq
// pwm input
int PWM0 = 2;
int PWM1 = 3;
int PWM2 = 4;
int PWM3 = 5;
int PWM4 = 6;
int PWM5 = 7;
int outhigh = 17; // LED
int outlow = 15; // LED
unsigned long duration;
// setup serial and input, output pins
void setup()
{
Serial.begin(19200);
pinMode(PWM0, INPUT); // PWM input pin
pinMode(outhigh, OUTPUT); //LED pin, lights LED on pin 17
pinMode(outlow, OUTPUT);// LED pin, lights LED on pin 15
}
void loop(){
{
duration = pulseIn(PWM0, HIGH);
digitalWrite(outhigh, LOW);
digitalWrite(outlow, LOW);
}
if (duration > 1050 && duration < 1150)
{
digitalWrite(outlow, HIGH);
Serial.println("FULL THROTTLE");
}
else if (duration > 1151 && duration < 1250)
{
digitalWrite(outlow, LOW);
Serial.println("ACELL 3");
}
else if (duration > 1251 && duration < 1350)
{
digitalWrite(outlow, LOW);
Serial.println("ACCEL 2");
}
else if (duration > 1351 && duration < 1450)
{
digitalWrite(outlow, LOW);
Serial.println("ACCEL 1");
}
else if (duration > 1451 && duration < 1550)
{
digitalWrite(outlow, HIGH);
digitalWrite(outhigh, HIGH);
Serial.println("IDLE");
}
else if (duration > 1551 && duration < 1650)
{
digitalWrite(outhigh, LOW);
Serial.println("ACCEL 1");
}
else if (duration > 1651 && duration < 1750)
{
digitalWrite(outhigh, LOW);
Serial.println("ACCEL 2");
}
else if (duration > 1751 && duration < 1850)
{
digitalWrite(outhigh, LOW);
Serial.println("ACCEL 3");
}
else if (duration > 1851 && duration < 1950)
{
digitalWrite(outhigh, HIGH);
Serial.println("FULL THROTTLE");
}
Serial.println(duration);
delay(100); //delay so you can read the scrolling output
}
[/code]