1 | #include <LiquidCrystal_I2C.h>
|
2 | #include <Wire.h>
|
3 |
|
4 | LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display. ACHTUNG: Adresse kann auch 0x3F sein !!!
|
5 |
|
6 | // Anschlüsse:
|
7 | // GND - GND
|
8 | // VCC - 5V
|
9 | // SDA - ANALOG Pin 4
|
10 | // SCL - ANALOG pin 5
|
11 |
|
12 | unsigned long time_start;
|
13 | unsigned long time_end;
|
14 | unsigned long time;
|
15 |
|
16 | int PD_start; // value of the start-PD
|
17 | int PD_end; // value of the end-PD
|
18 | int Werte[100];
|
19 | long start;
|
20 | int i;
|
21 |
|
22 | float velocity; // velocity of the projectile
|
23 | float multiplier; // multiplier of the PD-value to start or finish the measurement
|
24 | float distance; // distance between the two PDs
|
25 |
|
26 | // settings for faster analogread:
|
27 |
|
28 | // ADPS2 ADPS1 ADPS0 Division factor
|
29 | // 0 0 0 1 nicht schneller als 2
|
30 | // 0 0 1 2
|
31 | // 0 1 0 4
|
32 | // 0 1 1 8
|
33 | // 1 0 0 16
|
34 | // 1 0 1 32
|
35 | // 1 1 0 64
|
36 | // 1 1 1 128 (Standard)
|
37 |
|
38 |
|
39 |
|
40 | #define FASTADC 1
|
41 |
|
42 | // defines for setting and clearing register bits
|
43 | #ifndef cbi
|
44 | #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
|
45 | #endif
|
46 | #ifndef sbi
|
47 | #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
|
48 | #endif
|
49 |
|
50 |
|
51 |
|
52 | // ===========================
|
53 | // ======= SETUP =========
|
54 | // ===========================
|
55 |
|
56 | void setup()
|
57 | {
|
58 | Serial.begin(9600);
|
59 |
|
60 | #if FASTADC
|
61 |
|
62 | /*
|
63 | // set prescale to 128
|
64 |
|
65 | sbi(ADCSRA,ADPS2) ; // ADPS2 auf 1 gesetzt
|
66 | sbi(ADCSRA,ADPS1) ; // ADPS1 auf 1 gesetzt
|
67 | sbi(ADCSRA,ADPS0) ; // ADPS0 auf 1 gesetzt
|
68 | */
|
69 |
|
70 | // set prescale to 2
|
71 |
|
72 | cbi(ADCSRA,ADPS2) ; // ADPS2 auf 0 gesetzt
|
73 | cbi(ADCSRA,ADPS1) ; // ADPS1 auf 0 gesetzt
|
74 | sbi(ADCSRA,ADPS0) ; // ADPS0 auf 1 gesetzt
|
75 |
|
76 |
|
77 | #endif
|
78 |
|
79 | lcd.begin(); // initialize the lcd
|
80 | lcd.backlight();
|
81 | lcd.setCursor(0,0);
|
82 | lcd.print("Geschwindigkeits");
|
83 | lcd.setCursor(0,1);
|
84 | lcd.print("messer");
|
85 |
|
86 | delay(3000);
|
87 |
|
88 | lcd.setCursor(0,0);
|
89 | lcd.print(" ");
|
90 | lcd.setCursor(0,1);
|
91 | lcd.print(" ");
|
92 |
|
93 |
|
94 | multiplier = 0.95; // the LDR-values have to be 5% lower than the values without projectile to start and finish the measurement
|
95 |
|
96 | distance = 0.20; // distance in meters between the two LDRs
|
97 |
|
98 | PD_start = analogRead(A0); // measuring the brightness of the starting-PD without projectile
|
99 | delay(20);
|
100 | PD_end = analogRead(A1); // measuring the brightness of the ending-PD without projectile
|
101 | delay(20);
|
102 |
|
103 | Serial.print("PD-start: ");
|
104 | Serial.print(PD_start);
|
105 | Serial.print(" , PD-end: ");
|
106 | Serial.println(PD_end);
|
107 |
|
108 | lcd.setCursor(0,0);
|
109 | lcd.print("PD1: ");
|
110 | lcd.print(PD_start);
|
111 | lcd.setCursor(0,1);
|
112 | lcd.print("PD2: ");
|
113 | lcd.print(PD_end);
|
114 |
|
115 | delay(6000);
|
116 |
|
117 | lcd.setCursor(0,0);
|
118 | lcd.print("ready for shoot");
|
119 | lcd.setCursor(0,1);
|
120 | lcd.print("... ");
|
121 |
|
122 | delay(4000);
|
123 |
|
124 | lcd.setCursor(0,0);
|
125 | lcd.print(" ");
|
126 | lcd.setCursor(0,1);
|
127 | lcd.print(" ");
|
128 | }
|
129 |
|
130 |
|
131 |
|
132 | // ===========================
|
133 | // ======= LOOP =========
|
134 | // ===========================
|
135 |
|
136 | void loop()
|
137 | {
|
138 |
|
139 | while(analogRead(A0) > PD_start * multiplier)
|
140 | {
|
141 | // waiting for the projectile
|
142 | }
|
143 |
|
144 | time_start = micros(); // start-time in µs
|
145 |
|
146 | while(analogRead(A1) > PD_end * multiplier)
|
147 | {
|
148 | // waiting for the projectile
|
149 | }
|
150 |
|
151 | time_end = micros(); // end-time in µs
|
152 |
|
153 | time = time_end - time_start; // flight-time in µs
|
154 |
|
155 |
|
156 | //time = 123456;
|
157 |
|
158 | Serial.print("Time: ");
|
159 | Serial.println(time);
|
160 |
|
161 | velocity = distance * 1000000 / time; // velocity of the projectile in m/s
|
162 |
|
163 | lcd.setCursor(0,0);
|
164 | lcd.print(" ");
|
165 | lcd.setCursor(0,1);
|
166 | lcd.print(" ");
|
167 |
|
168 | lcd.setCursor(0,0);
|
169 | lcd.print("t = ");
|
170 | lcd.setCursor(4,0);
|
171 | lcd.print(time);
|
172 | lcd.print(" us");
|
173 |
|
174 | lcd.setCursor(0,1);
|
175 | lcd.print("v = ");
|
176 | lcd.setCursor(4,1);
|
177 | lcd.print(velocity, 2);
|
178 | lcd.print(" m/s");
|
179 |
|
180 | //delay(1000);
|
181 |
|
182 | /*
|
183 | // test for fast-analogread
|
184 |
|
185 | Serial.print("ADCTEST: ") ;
|
186 |
|
187 | start = micros();
|
188 |
|
189 | for (i = 0 ; i < 100 ; i++)
|
190 | Werte[i] = analogRead(A0);
|
191 |
|
192 | //delay(16);
|
193 | Serial.print(micros() - start);
|
194 | Serial.println(" microsec (100 calls)") ;
|
195 | */
|
196 |
|
197 | }
|