Hi,
I have written a sketch which allows the Arduino to output to 3 digital pins, delivering binary code 0 -7, which in turn changes the channel (Ch 1 - 8) on a small video receiver. The plan is to have a button increment the channels and display the current channel and its frequency on an LCD. Why replace a simple binary switch with electronics? - because the hardware project (FPV ground station) will have an Arduino and LCD built in. Also I intend to use the RSSI output of the module to set up a channel scanning feature.
The sketch compiles (Arduino 1.5.2 on Mac) but as you can see there is much repetition, its more of a hack than a programme. How do I use an array or some other device to pass parameters to the channel setting sub-routine in order to remove the duplication?
Any help gratefully received. (I have already used some code from this forum (counting button presses) and went through about another 400 pages before my head exploded.)
Regards
Alan
/* Sketch to change channel on Foxtech 5.8 Ghz Video receiver module.
And anything else I can think of!
The module requires 3 digital output pins connected to the module to select
one of 8 channels:
Channel 1 5705 Mhz = 000
Channel 2 5685 Mhz = 001
Channel 3 5665 Mhz = 010
Channel 4 5645 Mhz = 011
Channel 5 5885 Mhz = 100
Channel 6 5905 Mhz = 101
Channel 7 5925 Mhz = 110
Channel 8 5945 Mhz = 111
This is the simplest if not the most elegant approach
*/
#include <LiquidCrystal.h>;
LiquidCrystal lcd(12, 11, 10, 9, 8, 7); //Pins used for LCD,
boolean ChBut = 3; //Declare channel change button pin - we will count button presses to step through the channels
//Declare digital outputs to RX Module
boolean ChSet1 = 4; //Connect to Ch1 - module pin 9
boolean ChSet2 = 5; //Connect to Ch2 - module pin 8
boolean ChSet3 = 6; //Connect to Ch3 - module pin 7
int SigPin = 2; //Connect to RSSI - module pin 4
int RSSIVal; //Declare integer to hold RSSI value.
byte ChCount = 1; //Declare an integer to hold number of button counts and initialise at 1 press (Ch 1)
byte LastChCount = 1; //This is to compare channel counts to see if a change has occured
void setup() {
pinMode(ChBut, INPUT); //Set channel change button pin to an input
digitalWrite(ChBut, HIGH); //Set internal pull-up resistor
pinMode(ChSet1, OUTPUT); //Set channel change pins to outputs
pinMode(ChSet2, OUTPUT);
pinMode(ChSet3, OUTPUT);
lcd.setCursor(0, 0); //Home the cursor
lcd.print("Channel 1"); //Display Channel number
lcd.setCursor(0, 1); //Set cursor to line 2
lcd.print("5.705 Ghz"); //Display Channel frequency
digitalWrite(ChSet1, LOW); //Set channel change pins to low (0v).
digitalWrite(ChSet2, LOW); //This will set the default channel to 1,
digitalWrite(ChSet3, LOW); //which might prove unecessary when tested with hardware
RSSIVal = analogRead(SigPin); //Set the RSSI value to the analogue voltage on SigPin (pin 2) - use later for channel sweep
lcd.begin(16, 2); //Initialise LCD with 16 columns and 2 rows
lcd.clear(); //Clear the screen
}
void loop() {
ReadChBut(); //Read the condition of the channel change button and take action accordingly
}
// All other routines here
void ReadChBut() {
if (digitalRead(ChBut) == LOW) //See if channel change button has been pressed
{
ChCount++; //if the button is pressed increment channel count
delay(250); //debounce switch
}
if (ChCount == 9) ChCount = 1; //Only count up to 8 channels then reset counter to channel 1
if (LastChCount != ChCount); //Only do output if channel count has changed
{
if (ChCount == 1) SetCh1(); //Interogate the channel counter and set the channel
if (ChCount == 2) SetCh2();
if (ChCount == 3) SetCh3();
if (ChCount == 4) SetCh4();
if (ChCount == 5) SetCh5();
if (ChCount == 6) SetCh6();
if (ChCount == 7) SetCh7();
if (ChCount == 8) SetCh8();
}
LastChCount = ChCount;
}
void SetCh1()
{
lcd.clear(); //Clear the screen
lcd.setCursor(0, 0); //Home the cursor
lcd.print("Channel 1"); //Display Channel number
lcd.setCursor(0, 1); //Set cursor to line 2
lcd.print("5.705 Ghz"); //Display Channel frequency
digitalWrite(ChSet1, LOW); //Outputs set to Ch 1 = 000
digitalWrite(ChSet2, LOW);
digitalWrite(ChSet3, LOW);
}
void SetCh2()
{
lcd.clear(); //Clear the screen
lcd.setCursor(0, 0); //Home the cursor
lcd.print("Channel 2"); //Display Channel number
lcd.setCursor(0, 1); //Set cursor to line 2
lcd.print("5.685 Ghz"); //Display Channel frequency
digitalWrite(ChSet1, HIGH); //Outputs set to Ch 2 = 001
digitalWrite(ChSet2, LOW);
digitalWrite(ChSet3, LOW);
}
void SetCh3()
{
lcd.clear(); //Clear the screen
lcd.setCursor(0, 0); //Home the cursor
lcd.print("Channel 3"); //Display Channel number
lcd.setCursor(0, 1); //Set cursor to line 2
lcd.print("5.665 Ghz"); //Display Channel frequency
digitalWrite(ChSet1, LOW); //Outputs set to Ch 3 = 010
digitalWrite(ChSet2, HIGH);
digitalWrite(ChSet3, LOW);
}
void SetCh4()
{
lcd.clear(); //Clear the screen
lcd.setCursor(0, 0); //Home the cursor
lcd.print("Channel 4"); //Display Channel number
lcd.setCursor(0, 1); //Set cursor to line 2
lcd.print("5.645 Ghz"); //Display Channel frequency
digitalWrite(ChSet1, HIGH); //Outputs set to Ch 4 = 011
digitalWrite(ChSet2, HIGH);
digitalWrite(ChSet3, LOW);
}
void SetCh5()
{
lcd.clear(); //Clear the screen
lcd.setCursor(0, 0); //Home the cursor
lcd.print("Channel 5"); //Display Channel number
lcd.setCursor(0, 1); //Set cursor to line 2
lcd.print("5.885 Ghz"); //Display Channel frequency
digitalWrite(ChSet1, LOW); //Outputs set to Ch 5 = 100
digitalWrite(ChSet2, LOW);
digitalWrite(ChSet3, HIGH);
}
void SetCh6()
{
lcd.clear(); //Clear the screen
lcd.setCursor(0, 0); //Home the cursor
lcd.print("Channel 6"); //Display Channel number
lcd.setCursor(0, 1); //Set cursor to line 2
lcd.print("5.905 Ghz"); //Display Channel frequency
digitalWrite(ChSet1, HIGH); //Outputs set to Ch 6 = 101
digitalWrite(ChSet2, LOW);
digitalWrite(ChSet3, HIGH);
}
void SetCh7()
{
lcd.clear(); //Clear the screen
lcd.setCursor(0, 0); //Home the cursor
lcd.print("Channel 7"); //Display Channel number
lcd.setCursor(0, 1); //Set cursor to line 2
lcd.print("5.925 Ghz"); //Display Channel frequency
digitalWrite(ChSet1, LOW); //Outputs set to Ch 7 = 110
digitalWrite(ChSet2, HIGH);
digitalWrite(ChSet3, HIGH);
}
void SetCh8()
{
lcd.clear(); //Clear the screen
lcd.setCursor(0, 0); //Home the cursor
lcd.print("Channel 8"); //Display Channel number
lcd.setCursor(0, 1); //Set cursor to line 2
lcd.print("5.945 Ghz"); //Display Channel frequency
digitalWrite(ChSet1, HIGH); //Outputs set to Ch 8 = 111
digitalWrite(ChSet2, HIGH);
digitalWrite(ChSet3, HIGH);
}