Arduino nano source code for 6 button remote with manual gain and level controls
Switches go to gnd and respective pins labeled in program d4-d9 on nano PCB
Potentiometers 1 & 2 are 10K outer pins connected to gnd and 5v labeled on nano PCB then
center pin or wiper connected to A3 (gain potentiometer #1) and A5(level potentiometer #2) labeled on nano PCB
gnd------A3-------5v (pot#1) gnd-------A5---------5v (pot#2) labeled on nano PCB
Note: to reprogram the arduino nano that's connected to a RS232 to TTL PCB board you have to disconnect the TX and RX pins from the nano PCB to the rs232 PCB. The wires going to the camera can stay connected, as well as the buttons.
Only 2 wires need to be disconnected the rx and tx from the nano to the RS232 PCB
after reprogramming reconnect the TX and RX back, if you don't disconnect you can't communicate with the nano
Tested and working
Cut and paste this code in arduino IDE program window
Start below
#include <SoftwareSerial.h>
#define DEBOUNCE 100 // ms for Debounce, 5+
//Buttons.
byte buttons[] = {4, 5, 6, 7, 8, 9};
//Figure out Array Size
#define NUMBUTTONS sizeof(buttons)
//Track Buttons Pressed, Released, Currently Pressed
byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];
byte previous_keystate[NUMBUTTONS], current_keystate[NUMBUTTONS];
//define variables
int bhot=1;
int narrow=1;
int manual=0;
int mgain=120;
int mlevel=125;
int mgain2=125;
int mlevel2=200;
void setup() {
byte i;
Serial.begin(19200); //Setup Serial Port
// Input and Enable Pull-Up Resistors for Switch Pins
for (i=0; i< NUMBUTTONS; i++) {
pinMode(buttons[i], INPUT);
digitalWrite(buttons[i], HIGH);
}
}
void loop() {
analogRead(A3); //Discard
int level=analogRead(A3);
analogRead(A5); //Discard
int gain=analogRead(A5);
mgain=map(gain,0,1023,190,220);
mlevel=map(level,0,1023,00,255);
byte ccsum=256-1-50-2-15-mgain;
byte bcsum=256-1-51-2-7-mlevel;
byte thisSwitch=thisSwitch_justPressed();
switch(thisSwitch)
{
case 0:
// button 1 NUC with shutter pin D4
Serial.write(0x01);
Serial.write(0x27);
Serial.write(0x02);
Serial.write(0x00);
Serial.write(0x03);
Serial.write(0xd3); break;
case 1:
// button 2 TOGGLE POLARITY pin D5
if(bhot==1){
Serial.write(0x01);
Serial.write(0x28);
Serial.write(0x00);
Serial.write(0xd7);
bhot=bhot-1; break;
}else
Serial.write(0x01);
Serial.write(0x29);
Serial.write(0x00);
Serial.write(0xd6);
bhot=bhot+1; break;
case 2:
// button 3 MANUAL AGC MODE pin D6
Serial.write(0x01);
Serial.write(0x2a);
Serial.write(0x02);
Serial.write(0x00);
Serial.write(0x02);
Serial.write(0xd1);
delay(100);
manual=1;
Serial.write(0x01);
Serial.write(0x32);
Serial.write(0x02);
Serial.write(0x0f);
Serial.write(mgain);
Serial.write(ccsum);
delay(100);
Serial.write(0x01);
Serial.write(0x33);
Serial.write(0x02);
Serial.write(0x07);
Serial.write(mlevel);
Serial.write(bcsum);
delay(100);
break;
case 3:
// button 4 STANDARD AGC pin D7
Serial.write(0x01);
Serial.write(0x2a);
Serial.write(0x02);
Serial.write(0x00);
Serial.write(0x01);
Serial.write(0xd2);
manual=0;
break;
case 4:
// button 5 TOGGLE FOV pin D8
if(narrow==1){
Serial.write(0x01);
Serial.write(0xa4);
Serial.write(0x00);
Serial.write(0x5b);
narrow=narrow-1;
break;
}else {
Serial.write(0x01);
Serial.write(0xa5);
Serial.write(0x02);
Serial.write(0x00);
Serial.write(0x00);
Serial.write(0x58);
narrow=narrow+1;
break; }
case 5:
// button 6 MANUAL AGC OFF pin D9
Serial.write(0x01);
Serial.write(0x2a);
Serial.write(0x02);
Serial.write(0x00);
Serial.write(0x00);
Serial.write(0xd3);
manual=0;
break;
}
}
void check_switches()
{
static byte previousstate[NUMBUTTONS];
static byte currentstate[NUMBUTTONS];
static long lasttime;
byte index;
if (millis() < lasttime) {
// We Looped, One More Time
lasttime = millis();
}
if ((lasttime + DEBOUNCE) > millis()) {
// Not Enough Time Passed for Debounce
return;
}
// DEBOUNCE Milliseconds, Reset the Timer
lasttime = millis();
for (index = 0; index < NUMBUTTONS; index++) {
justpressed[index] = 0; //Clear Out Indicators
justreleased[index] = 0;
currentstate[index] = digitalRead(buttons[index]); //Read Button
if (currentstate[index] == previousstate[index]) {
if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
// Just Pressed
justpressed[index] = 1;
}
else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
justreleased[index] = 1; // Just Released
}
pressed[index] = !currentstate[index]; //HIGH means NOT pressed
}
previousstate[index] = currentstate[index]; //Running Count of the Buttons
}
}
byte thisSwitch_justPressed() {
byte thisSwitch = 255;
check_switches(); //Check the Switches ; Get Current State
for (byte i = 0; i < NUMBUTTONS; i++) {
current_keystate[i]=justpressed[i];
if (current_keystate[i] != previous_keystate[i]) {
if (current_keystate[i]) thisSwitch=i;
}
previous_keystate[i]=current_keystate[i];
}
analogRead(A3); //Discard
int level=analogRead(A3); // center pin of potentiometer #1
analogRead(A5); //Discard
int gain=analogRead(A5); // center pin of potentiometer #2
mgain=map(gain,0,1023,190,220);
mlevel=map(level,0,1023,00,255);
byte ccsum=256-1-50-2-15-mgain;
byte bcsum=256-1-51-2-7-mlevel;
if ((manual==1)&&(mgain2!=mgain)) {
Serial.write(0x01);
Serial.write(0x32);
Serial.write(0x02);
Serial.write(0x0f);
Serial.write(mgain);
Serial.write(ccsum);
delay(100);
mgain2=mgain;
}
if ((manual==1)&&(mlevel2!=mlevel)) {
Serial.write(0x01);
Serial.write(0x33);
Serial.write(0x02);
Serial.write(0x07);
Serial.write(mlevel);
Serial.write(bcsum);
delay(100);
mlevel2=mlevel;
}
return thisSwitch;
}
Ended above this line