Ok, so I'm working on this project and trying to build a chrono into the barrel of a nerf gun so it will read the FPS of my darts. Sounds simple enough.
Well as far as I can tell my code is working and the Arduino Nano seems to be acknowledging the IR triggers and reading out contact. Unfortunately, the fastest I can get it to read is 7 fps.
/*********************************************************************
Nerf Barrel Chrono
Mark French - 2017
*********************************************************************/
//Includes
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//for the OLED display
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
static const unsigned char PROGMEM logo16_glcd_bmp[] =
{ B00000000, B11000000, B00000001, B11000000, B00000001, B11000000, B00000011, B11100000,
B11110011, B11100000, B11111110, B11111000, B01111110, B11111111, B00110011, B10011111,
B00011111, B11111100, B00001101, B01110000, B00011011, B10100000, B00111111, B11100000,
B00111111, B11110000, B01111100, B11110000, B01110000, B01110000, B00000000, B00110000 };
//For the Chrono
unsigned long start=0, finish=0, interval=1, velocity=0.01, distance=0; //These variables might use large numbers or decimals
int startPin=0, finishPin=0, flagstart=0, flagstop=0;
//Pin position addresses. Trying analog...
#define startPin (A1)
#define finishPin (A2)
void setup() {
Serial.begin(9600);
// More display stuff
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
display.clearDisplay();
//pinMode for the triggers
pinMode(startPin, INPUT);
pinMode(finishPin, INPUT);
//Distance between sensors in feet x 1000,000 because microseconds
distance=250000; // .25 of a foot = 3 inches
}
void loop() {
//Starting Chrono pin reads for the IR triggers.
if (digitalRead(startPin) == 0){ //Reading first pin trigger
start = micros(); //Read Microseconds Timer
flagstart = 1; //Set starting flag
}
if (digitalRead(finishPin) == 0 && flagstart == 1){ //Reading second pin trigger.
finish = micros(); //Read Microseconds Timer
flagstop = 1; //Set stopping flag
}
if (flagstart == 1 && flagstop == 1){ //If both flags are set to 1, calculate FPS
interval = finish - start; //Subtract finish timer from start timer to find interval
unsigned long velocity = distance/interval; //Velocity equals distance over time ...unsigned long again for fun...
flagstart=0, flagstop=0; //reset flags
}
//Displaying information on OLED
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
//display.print("Start ");
//display.print(flagstart);
//display.print(", Stop ");
//display.println(flagstop);
display.print(interval);
display.print(", ");
display.println(finish);
display.setTextSize(2);
display.print(velocity);
display.println(" ft/sec");
display.setTextSize(1);
display.print("Start ");
display.print(digitalRead(startPin));
display.print(", Finish ");
display.print(digitalRead(finishPin));
display.display();
display.clearDisplay();
}
I've got it displaying on an OLED display and I used the extra lines to read out trigger states so I could see whether they were making contact. Everything reads out fine. I'm just wondering if I'm doing something wrong. My IR triggers also have LEDs to display when they've been tripped by a projectile. Anyone have any thoughts or suggestions?