|
2 | 2 | #define in1 6 |
3 | 3 | #define in2 7 |
4 | 4 | #define button 4 |
| 5 | + #define RELAY 12 |
5 | 6 | #define CH1 3 |
| 7 | + #define CH2 2 |
6 | 8 | #define RANGE 1000 |
7 | 9 |
|
8 | 10 | 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); |
12 | 15 | pinMode(button, INPUT); |
13 | | - pinMode(CH1, INPUT); |
| 16 | + pinMode(CH1, INPUT); |
| 17 | + pinMode(CH2, INPUT); |
14 | 18 | TCCR1B = TCCR1B & B11111000 | B00000001; |
15 | 19 | // Set initial rotation direction |
16 | 20 | digitalWrite(in1, LOW); |
17 | 21 | digitalWrite(in2, HIGH); |
18 | 22 | attachInterrupt(digitalPinToInterrupt(CH1), ch1_interrupt, CHANGE); |
| 23 | + attachInterrupt(digitalPinToInterrupt(CH2), ch2_interrupt, CHANGE); |
19 | 24 |
|
20 | 25 | Serial.begin(9600); |
21 | 26 | } |
|
25 | 30 | int oldCh1; |
26 | 31 |
|
27 | 32 |
|
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}; |
31 | 36 | volatile bool isPPMOk = false; |
32 | 37 | volatile unsigned int valid_frames = 0; |
33 | 38 |
|
34 | 39 | void ch1_interrupt() { |
35 | 40 | volatile int ch = digitalRead(CH1); |
36 | | - if (ch==1 && oldCh==0) |
| 41 | + if (ch==1 && oldCh[0]==0) |
37 | 42 | { |
38 | | - microsStart = micros(); |
| 43 | + microsStart[0] = micros(); |
39 | 44 | } |
40 | | - if (ch==0 && oldCh==1) |
| 45 | + if (ch==0 && oldCh[0]==1) |
41 | 46 | { |
42 | 47 | //Faling |
43 | | - readout = micros()- microsStart; |
| 48 | + readout[0] = micros()- microsStart[0]; |
44 | 49 | valid_frames ++; |
45 | 50 |
|
46 | 51 | } |
47 | | - if (readout > 2500) |
| 52 | + if (readout[0] > 2500) |
48 | 53 | { |
49 | 54 | valid_frames = 0; |
50 | 55 | isPPMOk = false; |
|
56 | 61 | isPPMOk = true; |
57 | 62 | } |
58 | 63 | } |
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 | + |
61 | 81 | } |
62 | 82 |
|
63 | 83 | int lastSetpoint = 0; |
64 | 84 |
|
65 | 85 | char buffer [50]; |
66 | 86 | void loop() { |
67 | 87 | // input PPM smoothing |
68 | | - int setPoint = map(readout, 1000, 2000, -RANGE, RANGE); |
| 88 | + int setPoint = map(readout[0], 1000, 2000, -RANGE, RANGE); |
69 | 89 | if (setPoint > RANGE) setPoint = RANGE; |
70 | 90 | if (setPoint < -RANGE) setPoint = -RANGE; |
71 | 91 |
|
|
101 | 121 | //Serial.println(pwmOutput); |
102 | 122 | analogWrite(enA, pwmOutput); // Send PWM signal to L298N Enable pin |
103 | 123 | // Read button - Debounce |
104 | | - |
105 | | - |
106 | | - |
| 124 | + |
107 | 125 | //int i=sprintf (buffer, "%d %d \n", setPoint,input ); |
108 | 126 | //for(int l= 0; l<=i; l++) |
109 | 127 | // Serial.print(buffer[l]); |
|
124 | 142 | digitalWrite(in2, LOW); |
125 | 143 | digitalWrite(in1, LOW); |
126 | 144 | } |
| 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 | + } |
127 | 154 |
|
128 | 155 | // logging |
129 | 156 |
|
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); |
131 | 158 | for(int l= 0; l<=i; l++) |
132 | 159 | Serial.print(buffer[l]); |
133 | 160 |
|
|
0 commit comments