Select your language

Eesmärk oli luua mikroprotsessori baasil kronograaf kontrollimaks õhkrelva kuuli kiirust.
IR diood + reciver Paigutatud ritta: IR phototransistor + diode + phototransistor,
Keskel paiknev led > peegeldus tagasi kahele transistorile.
asend nii lähedal kui võimalik (praktiliselt tsentrid 10 mm)
dioodiel R220
Phototransistori ette 10K
AnalogRead() kiirendi:
http://yaab-arduino. … om-analog-input.html

/* Kronograaf, 31.3.19 */

int t0 = 100;           // analog read lasu registreerimise tase a0
int t1 = 100;           // analog read lasu registreerimise tase a1
int t2 = 100;           // analog read lasu registreerimise tase a2
int t3 = 100;           // analog read lasu registreerimise tase a3
int x = 10;             // sensitiivsuse koef
unsigned long aeg1 = 0; // lasu alghetk micros()
float aeg  = 0;         // lennuaeg micros
float v = 0;            // arvutatud kiirus
float fps = 0;          // arvutatud kiirus 1m/s = 3.28084 fps
unsigned long a0 = 0;   // 1. sensor a0 value
unsigned long a1 = 0;   // 1. sensor a1 value
unsigned long a2 = 0;   // 2. sensor a2 value
unsigned long a3 = 0;   // 2. sensor a3 value
String r1 = "";         // string LCD rida 1
unsigned int d = 0;     // delay in microsecond (min is 3)
float L = 0.305;        // sensorite vaheline kaugus
int w = 1000000;        // lasu ootamine = 1 sek.
int i = 0;              //
boolean s = false;      // lask true/false

#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))

//LCD
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>  // F Malpartida's NewLiquidCrystal library
#define I2C_ADDR    0x20        // Define I2C Address for the PCF8574A 
#define BACKLIGHT_PIN  7
#define En_pin  4
#define Rw_pin  5
#define Rs_pin  6
#define D4_pin  0
#define D5_pin  1
#define D6_pin  2
#define D7_pin  3
#define  LED_OFF  0
#define  LED_ON  1
LiquidCrystal_I2C  lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);

void setup() {
  // kiirus-set prescale to 16
  sbi(ADCSRA, ADPS2) ;
  cbi(ADCSRA, ADPS1) ;
  cbi(ADCSRA, ADPS0) ;

  Serial.begin(115200);

  // LCD
  lcd.begin (16, 2);
  lcd.setBacklightPin(BACKLIGHT_PIN, NEGATIVE);
  lcd.setBacklight(LED_ON);
  lcd.setCursor(0, 0);
  lcd.print("Setup() ...");
  delay(1000);
  out();
  lcd.setCursor(0, 0);
  lcd.print("Ootan lasku ....");
}

void out() {
  for (i = 0; i < 20; i ++) {
    a0 = a0 + analogRead(A0);
    delayMicroseconds(d);
    a1 = a1 + analogRead(A1);
    delayMicroseconds(d);
    a2 = a2 + analogRead(A2);
    delayMicroseconds(d);
    a3 = a3 + analogRead(A3);
    delayMicroseconds(d);
    delay(100);
  }
  a0 = a0 / i;
  a1 = a1 / i;
  a2 = a2 / i;
  a3 = a3 / i;
  t0 = a0 + x;
  t1 = a1 + x;
  t2 = a2 + x;
  t3 = a3 + x;
  ser();
}

void ser() {
  Serial.print("t0= ");
  Serial.println(t0);
  Serial.print("t1= ");
  Serial.println(t1);
  Serial.print("t2= ");
  Serial.println(t2);
  Serial.print("t3= ");
  Serial.println(t3);
  Serial.print("a0= ");
  Serial.println(a0);
  Serial.print("a1= ");
  Serial.println(a1);
  Serial.print("a2= ");
  Serial.println(a2);
  Serial.print("a3= ");
  Serial.println(a3);
  Serial.print("v= ");
  Serial.print(v, 1);
  Serial.println(" m/s");
  Serial.print("v= ");
  Serial.print(fps, 0);
  Serial.println(" fps");
  Serial.println("-------");
}

void kuva() {
  lcd.clear();
  lcd.home();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("***** LASK *****");
  lcd.setCursor(0, 1);
  r1 = String("V = " + String(v, 0) + " m/s " + String(fps, 0));
  lcd.print(r1);
  delay(10000);
  lcd.setCursor(0, 0);
  lcd.print("Ootan lasku ....");
}

void loop() {
  a0 = analogRead(A0);
  delayMicroseconds(d);
  a1 = analogRead(A1);
  delayMicroseconds(d);
  if (a0 > t0 || a1 > t1 ) {
    aeg1 = micros();
    while ((micros() - aeg1) < w && s == false) {
      a2 = analogRead(A2);
      delayMicroseconds(d);
      a3 = analogRead(A3);
      delayMicroseconds(d);
      if (a2 > t2 || a3 > t3 ) {
        s = true;
        aeg = (micros() - aeg1);
        v = L / (aeg / 1000000);
        fps = v * 3.28084;
        kuva();
        out();
        s = false;
      } else {
        t2 = a2 + x;
        t3 = a3 + x;
      }
    }
  } else {
    t0 = a0 + x;
    t1 = a1 + x;
  }
}