Buy an Axon, Axon II, or Axon Mote and build a great robot, while helping to support SoR.
0 Members and 1 Guest are viewing this topic.
Right now, I have my program increment the shotcount("ShotCount++;")
#include <LiquidCrystal.h> //Liquid Crystal Library#include <TimerOne.h> //Timer 1 Library#define pin6 (1 << PORTD6)#define pin7 (1 << PORTD7) int Flag; unsigned long StartTime; unsigned long StopTime; unsigned long Duration; unsigned long FPS; volatile unsigned long ShotCount; volatile unsigned long BPS; LiquidCrystal lcd(8, 9, 10, 11, 12, 13); void setup() { DDRD &= ~ ((1 << PORTD6) | (1 << PORTD7)); // Sets Pins 6 and 7 as INPUTS Timer1.initialize(1000000UL); // 1000000 microseconds (unsigned long) is 1 second Timer1.attachInterrupt(OneSecInterrupt); lcd.begin(16, 2); Duration=0; FPS=0; BPS=0; Flag=0; } void loop() { if (PIND & (1 <<PORTD6) && Flag==0) { StartTime=micros(); Flag=1; } if (PIND & (1 <<PORTD7) && Flag==1) { StopTime=micros(); ++ShotCount; Flag=0; Duration=StopTime-StartTime; FPS=(0.5*(1000000))/Duration; } if(BPS<=3) { PrintVelocity(); } if(BPS>3) { PrintROF(); } } void OneSecInterrupt() { BPS = ShotCount; ShotCount = 0; } void PrintVelocity() { lcd.clear(); lcd.print(" Ball Velocity"); lcd.setCursor(0, 1); lcd.print(FPS, DEC); lcd.print(" Feet/Second"); } void PrintROF() { lcd.clear(); lcd.print(" Fire Rate"); lcd.setCursor(0, 1); lcd.print(BPS, DEC); lcd.print(" Balls/Second"); }
#include <LiquidCrystal.h>const uint8_t FIRST_INTERRUPT = 0;const uint8_t FIRST_INTERRUPT_PIN = 2;const uint8_t SECOND_INTERRUPT = 1;const uint8_t SECOND_INTERRUPT_PIN = 3;volatile uint32_t shot_count;volatile uint32_t first_interrupt_time;volatile uint32_t second_interrupt_time;volatile uint8_t flag;float rof;float fps;uint32_t last_process;LiquidCrystal lcd(8, 9, 10, 11, 12, 13);void first_isr() { first_interrupt_time = micros(); flag |= (1 << FIRST_INTERRUPT);}void second_isr() { second_interrupt_time = micros(); ++shot_count; flag |= (1 << SECOND_INTERRUPT);}void process_rof() { uint32_t process_time = millis(); rof = shot_count / ((process_time - last_process) * 1.0e-3); shot_count = 0; last_process = process_time; }void process_fps() { uint32_t duration = second_interrupt_time - first_interrupt_time; fps = 0.5 / (duration * 1.0e-6);}void print_fps() { lcd.clear(); lcd.print(" Ball Velocity"); lcd.setCursor(0, 1); lcd.print(fps, DEC); lcd.print(" Feet/Second"); }void print_rof() { lcd.clear(); lcd.print(" Fire Rate"); lcd.setCursor(0, 1); lcd.print(rof, DEC); lcd.print(" Balls/Second"); }void setup() { // set pins to input pinMode(FIRST_INTERRUPT_PIN, INPUT); pinMode(SECOND_INTERRUPT_PIN, INPUT); // interrupt when pin2 goes from LOW to HIGH attachInterrupt(FIRST_INTERRUPT, first_isr, RISING); // interrupt when pin3 goes from LOW to HIGH attachInterrupt(SECOND_INTERRUPT, second_isr, RISING); // initialize to zero shot_count = first_interrupt_time = second_interrupt_time = flag = last_process = 0;}void loop() { // if more than a second has elapsed if (millis() - last_process > 1e3) { process_rof(); if (rof > 3) { print_rof(); } else { print_fps(); } } // if both interrupts have occurred if (flag & (1 << FIRST_INTERRUPT) && flag & (1 << SECOND_INTERRUPT)) { process_fps; flag = 0; }}
volatile unsigned long bps;
volatile float bps;
f you want ROF as a decimal, you need to use a more mathematical approach, forex:1) I will count the time between each ball that passes2) I will invert that time to find the number of balls that should pass each second3) I will print this calculated ROF each second
Yea, I avoid floating point if at all possible, except when I'm too lazy to do an alternative. Its better to multiply everything by 10^x. For example:slow:5.5/5.5 = 1faster:55/55 = 1Also maybe of interest to you is modulus:5/2 = 25%2 = 0.5Or to avoid floats for a 5/2 calculation:5/2 = 25%2*10 = 5(the solution is 2.5)
Quotef you want ROF as a decimal, you need to use a more mathematical approach, forex:1) I will count the time between each ball that passes2) I will invert that time to find the number of balls that should pass each second3) I will print this calculated ROF each secondDoesn't that assume your ROF is constant?
I found this in another thread, it seems to be relevant as he states floating point is too slow and ads 2-3k with the required libraries which would considerably slow down my program. Couldn't I use division as he states to get the value to the left and then the modulus to give me the remainder? QuoteYea, I avoid floating point if at all possible, except when I'm too lazy to do an alternative. Its better to multiply everything by 10^x. For example:slow:5.5/5.5 = 1faster:55/55 = 1Also maybe of interest to you is modulus:5/2 = 25%2 = 0.5Or to avoid floats for a 5/2 calculation:5/2 = 25%2*10 = 5(the solution is 2.5)
Code: [Select]#include <LiquidCrystal.h>const uint8_t FIRST_INTERRUPT = 0;const uint8_t FIRST_INTERRUPT_PIN = 2;const uint8_t SECOND_INTERRUPT = 1;const uint8_t SECOND_INTERRUPT_PIN = 3;volatile uint32_t shot_count;volatile uint32_t first_interrupt_time;volatile uint32_t second_interrupt_time;volatile uint8_t flag;float rof;float fps;uint32_t last_process;
#include <LiquidCrystal.h>const uint8_t FIRST_INTERRUPT = 0;const uint8_t FIRST_INTERRUPT_PIN = 2;const uint8_t SECOND_INTERRUPT = 1;const uint8_t SECOND_INTERRUPT_PIN = 3;volatile uint32_t shot_count;volatile uint32_t first_interrupt_time;volatile uint32_t second_interrupt_time;volatile uint8_t flag;float rof;float fps;uint32_t last_process;
Code: [Select]void loop() { // if more than a second has elapsed if (millis() - last_process > 1e3) { process_rof(); if (rof > 3) { print_rof(); } else { print_fps(); } }
void loop() { // if more than a second has elapsed if (millis() - last_process > 1e3) { process_rof(); if (rof > 3) { print_rof(); } else { print_fps(); } }
Code: [Select]void process_rof() { uint32_t process_time = millis(); rof = shot_count / ((process_time - last_process) * 1.0e-3); shot_count = 0; last_process = process_time; }
void process_rof() { uint32_t process_time = millis(); rof = shot_count / ((process_time - last_process) * 1.0e-3); shot_count = 0; last_process = process_time; }
BCode: [Select]void second_isr() { second_interrupt_time = micros(); ++shot_count; flag |= (1 << SECOND_INTERRUPT);}
void second_isr() { second_interrupt_time = micros(); ++shot_count; flag |= (1 << SECOND_INTERRUPT);}
Code: [Select]void loop() { // if more than a second has elapsed if (millis() - last_process > 1e3) { [b]++shotcount;[/b] process_rof(); if (rof > 3) { print_rof(); } }
void loop() { // if more than a second has elapsed if (millis() - last_process > 1e3) { [b]++shotcount;[/b] process_rof(); if (rof > 3) { print_rof(); } }
Code: [Select]void loop() { // if both interrupts have occurred if (flag & (1 << FIRST_INTERRUPT) && flag & (1 << SECOND_INTERRUPT)) { process_fps; ++shotcount; flag = 0; }}
void loop() { // if both interrupts have occurred if (flag & (1 << FIRST_INTERRUPT) && flag & (1 << SECOND_INTERRUPT)) { process_fps; ++shotcount; flag = 0; }}
lcd.print(rof/10); lcd.print("."); lcd.print(rof%10);
int a = 2; // initial valueint leftofdecimal = 2 * 10 / 10;int rightofdecimal = 2 * 10 % 10;