Skip to content

Commit 150da17

Browse files
committed
Relay switch
1 parent 9c282cb commit 150da17

File tree

1 file changed

+46
-19
lines changed

1 file changed

+46
-19
lines changed

servo/servo.ino

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,25 @@
22
#define in1 6
33
#define in2 7
44
#define button 4
5+
#define RELAY 12
56
#define CH1 3
7+
#define CH2 2
68
#define RANGE 1000
79

810
void setup() {
9-
pinMode(enA, OUTPUT);
10-
pinMode(in1, OUTPUT);
11-
pinMode(in2, OUTPUT);
11+
pinMode(enA, OUTPUT);
12+
pinMode(in1, OUTPUT);
13+
pinMode(in2, OUTPUT);
14+
pinMode(RELAY, OUTPUT);
1215
pinMode(button, INPUT);
13-
pinMode(CH1, INPUT);
16+
pinMode(CH1, INPUT);
17+
pinMode(CH2, INPUT);
1418
TCCR1B = TCCR1B & B11111000 | B00000001;
1519
// Set initial rotation direction
1620
digitalWrite(in1, LOW);
1721
digitalWrite(in2, HIGH);
1822
attachInterrupt(digitalPinToInterrupt(CH1), ch1_interrupt, CHANGE);
23+
attachInterrupt(digitalPinToInterrupt(CH2), ch2_interrupt, CHANGE);
1924

2025
Serial.begin(9600);
2126
}
@@ -25,26 +30,26 @@
2530
int oldCh1;
2631

2732

28-
volatile unsigned int oldCh;
29-
volatile unsigned int microsStart=0;
30-
volatile unsigned int readout =0;
33+
volatile unsigned int oldCh[2];
34+
volatile unsigned int microsStart[2]={0,0};
35+
volatile unsigned int readout[2]={0,0};
3136
volatile bool isPPMOk = false;
3237
volatile unsigned int valid_frames = 0;
3338

3439
void ch1_interrupt() {
3540
volatile int ch = digitalRead(CH1);
36-
if (ch==1 && oldCh==0)
41+
if (ch==1 && oldCh[0]==0)
3742
{
38-
microsStart = micros();
43+
microsStart[0] = micros();
3944
}
40-
if (ch==0 && oldCh==1)
45+
if (ch==0 && oldCh[0]==1)
4146
{
4247
//Faling
43-
readout = micros()- microsStart;
48+
readout[0] = micros()- microsStart[0];
4449
valid_frames ++;
4550

4651
}
47-
if (readout > 2500)
52+
if (readout[0] > 2500)
4853
{
4954
valid_frames = 0;
5055
isPPMOk = false;
@@ -56,16 +61,31 @@
5661
isPPMOk = true;
5762
}
5863
}
59-
60-
oldCh = ch;
64+
oldCh[0] = ch;
65+
}
66+
void ch2_interrupt()
67+
{
68+
volatile int ch = digitalRead(CH2);
69+
if (ch==1 && oldCh[1]==0)
70+
{
71+
microsStart[1] = micros();
72+
}
73+
if (ch==0 && oldCh[1]==1)
74+
{
75+
//Faling
76+
readout[1] = micros()- microsStart[1];
77+
}
78+
79+
oldCh[1] = ch;
80+
6181
}
6282

6383
int lastSetpoint = 0;
6484

6585
char buffer [50];
6686
void loop() {
6787
// input PPM smoothing
68-
int setPoint = map(readout, 1000, 2000, -RANGE, RANGE);
88+
int setPoint = map(readout[0], 1000, 2000, -RANGE, RANGE);
6989
if (setPoint > RANGE) setPoint = RANGE;
7090
if (setPoint < -RANGE) setPoint = -RANGE;
7191

@@ -101,9 +121,7 @@
101121
//Serial.println(pwmOutput);
102122
analogWrite(enA, pwmOutput); // Send PWM signal to L298N Enable pin
103123
// Read button - Debounce
104-
105-
106-
124+
107125
//int i=sprintf (buffer, "%d %d \n", setPoint,input );
108126
//for(int l= 0; l<=i; l++)
109127
// Serial.print(buffer[l]);
@@ -124,10 +142,19 @@
124142
digitalWrite(in2, LOW);
125143
digitalWrite(in1, LOW);
126144
}
145+
146+
// Relay
147+
if(readout[1]>1850 and readout[1] < 3000)
148+
{
149+
digitalWrite(RELAY, LOW);
150+
}else
151+
{
152+
digitalWrite(RELAY, HIGH);
153+
}
127154

128155
// logging
129156

130-
int i=sprintf (buffer, "%d %d %d %d %d, %d \n",readout, setPoint,setPointSmooth, output, input, isPPMOk);
157+
int i=sprintf (buffer, "%d %d %d %d %d %d, %d \n",readout[0],readout[1], setPoint,setPointSmooth, output, input, isPPMOk);
131158
for(int l= 0; l<=i; l++)
132159
Serial.print(buffer[l]);
133160

0 commit comments

Comments
 (0)