Here is my present code. I can control the switches manually, and I can use the Serial Monitor to set the switches in Remote.
Unfortunately, including all the explanatory comments above the code exceeds the allowable number of characters.
*/
// I/O pins and their pin names
int switch_A = 2; // TTL output to RF switch A, HIGH = Port 2 connected, LOW = Ports 2 or 3
int switch_B1 = 3; // TTL output to RF switch B, LOW (HIGH)= Port 3 connected;
//switchB_A and switchB_B are to tbe complementary
int switch_B2 = 4; // TTL output to RF switch B, (HIGH) LOW= Port 4 connected
//switchB_A and switchB_B are to tbe complementary
int ledPort2 = 5; //TTL output to LED for RF port 2
int ledPort3 = 6; //TTL output to LED for RF port 3
int ledPort4 = 7; //TTL output to LED for RF port 4
int ledLocRem = 8; //TTL output to LED for LOC/REM indication; LOW = remote
int rfAposnInd = 9; //TTL input from RF switch A indicator
int rfBposnInd = 10; //TTL input from RF switch B indicator
int locRemsw = 11; //TTL input from Local/Remote switch
int manSw0 = 12; //TTL input Bit 0 from front panel manual switch
int manSw1 = 13; //TTL input Bit 1 from front panel manual switch
// Defining required variables
byte locRemswVal = HIGH; // seting intial value of Loc/Rem switch
byte manSw0Val = LOW, manSw1Val = LOW; // seting intial values of manual switch, bits 0 & 1
int rfAposnIndVal =0, rfBposnIndVal = 0 ; // setting intial values of RF switch A & B indicators
int manualSwitchComposite = 0; //defining composite value of man switche0 value- 0, 1 or 2
int switchSettingPortValue = 22; //integer, from manual or USB, which determines which RF switches have been set. 22 = Port 2 etc
int rfSwCompVal = 0; //composite value of two switch position indicators, for ATMS to use only
int remoteSwSet = 0; // Value of remote input (0, 1, 2) frin USB port to set switches
int switchPortVal = 0; //new value of switch setting value to give more logical values
int currentlySetPort = 0; // value of this read of manual switch setting in Local mod, to check if switch has changed next loop
int currentPortVal = 55; //initial value of value for ATMS to read to know present port
//
void setup()
{
// defining TTL ouptut pins
pinMode(switch_A, OUTPUT); // TTL output to RF switch A
pinMode(switch_B1, OUTPUT); // TTL output to RF switch B1
pinMode(switch_B2, OUTPUT); // TTL output to RF switch B2
pinMode(ledPort2, OUTPUT); //TTL output to LED for RF port 2
pinMode(ledPort3, OUTPUT); //TTL output to LED for RF port 3
pinMode(ledPort4, OUTPUT); //TTL output to LED for RF port 4
pinMode(ledLocRem, OUTPUT); //TTL output to LED for LOC/REM indication
// setting internal pull-ups for TTL inputs
digitalWrite(rfAposnInd, HIGH); //TTL input from RF switch A indicator
digitalWrite(rfBposnInd, HIGH); //TTL input from RF switch B indicator
digitalWrite(locRemsw, HIGH); //TTL input from Local/Remote switch; HIGH = Local
digitalWrite(manSw0, HIGH); //TTL Bit 0 input from front panel manual 2 pole 3 posn switch
digitalWrite(manSw1, HIGH); //TTL Bit 1 input from front panel manual 2 pole 3 posn switch
//
// Set RF switches, LEDS to Port 2 to begin.
//
digitalWrite(switch_A, HIGH); // Set RF switches- switch A set HIGH
digitalWrite(switch_B1, LOW); // Set RF switches- switch B1 set LOW
digitalWrite(switch_B2, HIGH); // Set RF switches- switch B2 set HIGH
delay (200);
digitalWrite(ledPort2, HIGH);
digitalWrite(ledPort3, LOW);
digitalWrite(ledPort4, LOW);
Serial.begin(9600);
}
void loop()
{
locRemswVal = digitalRead(locRemsw);
if (locRemswVal == LOW) // read local/remote switch on front panel;
{
// in local mode, so read manual switch
digitalWrite(ledLocRem, LOW); // set ledLocRem low, turning off led, indicating local mode
manSw1Val = digitalRead(manSw1); //read value of second bit of front panel manual switch, D13
manSw0Val = digitalRead(manSw0); //read value of second bit of front panel manual switch
manSw1Val = manSw1Val << 1; //shift this read bit one place to the left, but retain name
manualSwitchComposite = manSw0Val + manSw1Val; //combine value of two switch inputs into one 2 bit byte
if (manualSwitchComposite == 2) // Port 4; D12 = 0; D13 = 1
{
switchPortVal = 444;
}
else if (manualSwitchComposite == 1) // Port 2; D12 = 1; D13 = 0
{
switchPortVal = 222;
}
else //switchSettingVal == 0, by default)
{
switchPortVal = 333; // Port 3; D12 = 0; D13 = 0
///
}
if(switchPortVal == currentlySetPort) //no change in manual switch setting since last loop
{
void loop();
}
else
{
void setSwitchesLeds();
}
}
// now in Remote mode
else
{
digitalWrite(ledLocRem, HIGH); // set ledLocRem high, turning oon led, indicating remote mode
//
delay(100);
if (Serial.available() == 0)
{
void loop();
}
else
{
remoteSwSet = Serial.read();
remoteSwSet = (remoteSwSet - 48); // converting ASCII to number
//Serial.print(remoteSwSet);
//Serial.println();
if (remoteSwSet == 2)
{
switchPortVal = 222;
}
else if (remoteSwSet == 3)
{
switchPortVal = 333;
}
else
{
switchPortVal = 444;
}
}
}
// setSwitches is a function to set the switches
void setSwitchesLeds(); //setting the RF switch positions; depends on value of switchSettingVal
if(currentlySetPort == switchSettingPortValue) //present port selected = currently requested port
{
void loop(); //no need to do anything, so go back the beginning of loop
}
if (switchPortVal == 222) // Switch setting = 1, 1-0, Port 2 is selected from front panel or USB- set switches
{
digitalWrite(switch_A, HIGH); // Set RF switches- switch A set HIGH
digitalWrite(switch_B1, LOW); // Set RF switches- switch B1 set LOW
digitalWrite(switch_B2, HIGH); // Set RF switches- switch B2 set HIGH
}
if (switchPortVal == 333) // Switch setting = 1, 0-1, Port 3 is selected from front panel or USB- set switches
{
digitalWrite(switch_A, LOW); // Set RF switches- switch A set LOW
digitalWrite(switch_B1, LOW); // Set RF switches- switch B1 set HIGH
digitalWrite(switch_B2, HIGH); // Set RF switches- switch B2 set LOW
}
if (switchPortVal == 444) //Switch setting = 0, 0-0, Port 4 is selected from front panel or USB- set switches
{
digitalWrite(switch_A, LOW); // Set RF switches- switch A set LOW
digitalWrite(switch_B1, HIGH); // Set RF switches- switch B1 set LOW
digitalWrite(switch_B2, LOW); // Set RF switches- switch B2 set HIGH
}
delay (50);
// Reading actual positions of RF switches to confirm setting
rfAposnIndVal = digitalRead(rfAposnInd); //read position indicator of switch A
rfBposnIndVal = digitalRead(rfBposnInd); //read position indicator of switch B
if (rfAposnIndVal == HIGH) // if switch A indicator is HIGH, this can only mean that Port 2 is selected, so set LEDs
{
digitalWrite(ledPort2, HIGH);
digitalWrite(ledPort3, LOW);
digitalWrite(ledPort4, LOW);
currentlySetPort = 222;
currentPortVal = 22;
}
if (rfAposnIndVal == LOW && rfBposnIndVal == LOW) //if switch A is LOW and Switch B is HIGH, this means Port 3 is selected- set LEDs
{
digitalWrite(ledPort2, LOW);
digitalWrite(ledPort3, HIGH);
digitalWrite(ledPort4, LOW);
currentlySetPort = 333;
currentPortVal = 33;
}
if (rfAposnIndVal == LOW && rfBposnIndVal == HIGH) //if switch A is LOW and Switch B is LOW, this means Port 4 is selected- set LEDs
{
digitalWrite(ledPort2, LOW);
digitalWrite(ledPort3, LOW);
digitalWrite(ledPort4, HIGH);
currentlySetPort = 444;
currentPortVal = 44;
}
}