Monthly Archives: November 2016

Use one PWM signal channel to switch onboard FPV cameras and switch on and off recording.

This Arduino sketch will allow two buttons on an er9x radio control radio transmitter to be multiplexed into turning on and off any predefined state of pins on the Arduino. I used it to control recording, on and off, of the raspberry pi. This was allocated to one of the switches and the other to toggle around 4 cameras. This could be easily extended to more cameras though. The pins that are used can be wired up to a solid state analogue switch. such as,

MC74LVXT4066DTRG ANALOGUE SWITCH, QUAD, SPST, TSSOP-14

The code basically moves a walking bit from one pin to another enabling the CVBS signal from one camera signal at a time, to switch on and send the analogue CVBS signal to the onboard TV signal transmitter. This will then send the signal back to your receiver on the ground, where you can see a TV signal.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
unsigned long duration;
 
int pin=A0;//This pin receives the signal from the receiver which is a 5v PWM signal
int ledPin = 13;//led to indicate recording.
int rec=0;//recored pin enable / disable
 
/*
Zones
The three ranges below create four zones. That is given the PWM signal can exceed the LOW_RANGE and HIGH_RANGE. These have been defined as constants as all radios and calibrations are different and you will need to tune yours. I have found the pulse width is stable enough to stay within these reliably with space for more in future. I have two switches on the radio transmitter. One is a toggle switch and one is a binary 2 state switch. I use the toggle switch to switch from camera to camera and the other to switch record on and off. When recording is enabled the width of the signal stays within zone 2 and 3 and when it is disabled zone 1 and 4. Then if there is a zone transition between 1 and 2 in record mode this will toggle the camera to the next and so on.
*/
//zone 1
const int LOW_RANGE   =1350;
//zone 2
const int MIDDLE_RANGE=1850;
//zone 3
const int HIGH_RANGE  =2350;
//zone 4
 
int chanTog=1;
void togChan() {//permits 4 states 0,1,2,3 then loops back to 0
if (chanTog > 1){
chanTog = 0;
} else {
chanTog=chanTog+1;
}
 
}
 
void setup()
{
Serial.begin(9600);
pinMode(ledPin,OUTPUT);//recording light only
pinMode(12,OUTPUT);//recording actual
//digital D7 needs to be low for ground.
//digitalWrite(7,LOW);
 
//digitalWrite(5,LOW);
//digitalWrite(6,LOW);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
 
}//close setup
 
void loop()
{
duration = pulseIn(pin, HIGH);
 
//Debugging serial
Serial.print(rec,DEC);
Serial.print("\t");
Serial.print(chanTog,DEC);
Serial.println("");
 
//Camera switcher
 if ( chanTog == 3) {//These high and low states can be set arbitrarily.
 digitalWrite(2,LOW);
 digitalWrite(3,LOW);
 digitalWrite(4,LOW);
 digitalWrite(5,LOW);
 digitalWrite(6,HIGH);
 digitalWrite(7,LOW);
 digitalWrite(8,LOW);
 digitalWrite(9,LOW);
 }
 if ( chanTog == 2) {
 digitalWrite(2,LOW);
 digitalWrite(3,LOW);
 digitalWrite(4,LOW);
 digitalWrite(5,HIGH);
 digitalWrite(6,LOW);
 digitalWrite(7,LOW);
 digitalWrite(8,LOW);
 digitalWrite(9,LOW);
 }
 if ( chanTog == 1) {
 digitalWrite(2,LOW);
 digitalWrite(3,HIGH);
 digitalWrite(4,LOW);
 digitalWrite(5,LOW);
 digitalWrite(6,LOW);
 digitalWrite(7,LOW);
 digitalWrite(8,LOW);
 digitalWrite(9,LOW);
 }
 if ( chanTog == 0) {
 digitalWrite(2,HIGH);
 digitalWrite(3,LOW);
 digitalWrite(4,LOW);
 digitalWrite(5,LOW);
 digitalWrite(6,LOW);
 digitalWrite(7,LOW);
 digitalWrite(8,LOW);
 digitalWrite(9,LOW);
 
 /*  digitalWrite(2,HIGH);
 digitalWrite(3,HIGH);
 digitalWrite(4,HIGH);
 digitalWrite(5,HIGH);
 digitalWrite(6,HIGH);
 digitalWrite(7,HIGH);
 digitalWrite(8,HIGH);
 digitalWrite(9,HIGH);*/
 
 }
 
 if (duration >  MIDDLE_RANGE) { //enable record
   if( duration > HIGH_RANGE ) { //channel change
   togChan();
   delay(300);//debouncing noise delay. ms
   }
  rec=0;
  digitalWrite(ledPin,LOW);
  digitalWrite(12,LOW);
 } else {
 
   if(duration > LOW_RANGE ) {
   togChan();
   delay(300);
   }
 rec=1;
 digitalWrite(ledPin,HIGH);
 digitalWrite(12,HIGH);
 }
 
} //close loop