Hello, school project, simple goal counter

Hello guys! First off all I would like to say I’m a noob when it comes to programming, so I’m sorry for any confusion, but I would apreciate a little guidance :sweat_smile: :sweat_smile:

I’m going to try to explain what kind of help I need

So, I made a project that was an rpm counter and used this code

/*
 * Optical Tachometer
 *
 * Uses an IR LED and IR phototransistor to implement an optical tachometer.
 * The IR LED is connected to pin 13 and ran continually.
 * Pin 2 (interrupt 0) is connected across the IR detector.
 *
 * Code based on: www.instructables.com/id/Arduino-Based-Optical-Tachometer/
 * Coded by: arduinoprojects101.com
 */

int ledPin = 13;                // IR LED connected to digital pin 13
volatile byte rpmcount;
unsigned int rpm;
unsigned long timeold;

// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void rpm_fun()
 {
   //Each rotation, this interrupt function is run twice, so take that into consideration for 
   //calculating RPM
   //Update count
      rpmcount++;
 }

void setup()
 {
   lcd.begin(16, 2);  // intialise the LCD

   //Interrupt 0 is digital pin 2, so that is where the IR detector is connected
   //Triggers on FALLING (change from HIGH to LOW)
   attachInterrupt(0, rpm_fun, FALLING);

   //Turn on IR LED
   pinMode(ledPin, OUTPUT);
   digitalWrite(ledPin, HIGH);

   rpmcount = 0;
   rpm = 0;
   timeold = 0;
 }

 void loop()
 {
   //Update RPM every second
   delay(1000);
   //Don't process interrupts during calculations
   detachInterrupt(0);
   //Note that this would be 60*1000/(millis() - timeold)*rpmcount if the interrupt
   //happened once per revolution instead of twice. Other multiples could be used
   //for multi-bladed propellers or fans
   rpm = 30*1000/(millis() - timeold)*rpmcount;
   timeold = millis();
   rpmcount = 0;

   //Print out result to lcd
   lcd.clear();
   lcd.print("RPM=");
   lcd.print(rpm);

   //Restart the interrupt processing
   attachInterrupt(0, rpm_fun, FALLING);
  }

And used this setup

Now I was trying to adapt it to a simple counter for goals and I am using the same setup but with the code you can see below, can you say If that is logically correct?

int ledPin = 13;                // IR LED connected to digital pin 13
volatile byte goalcount;
 
 
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
 
void golos()
 {
   
   //Update count
       goalcount++;
      
   
 }
 
void setup()
 {
   lcd.begin(16, 2);  // intialise the LCD
 
   //Interrupt 0 is digital pin 2, so that is where the IR detector is connected
   //Triggers on FALLING (change from HIGH to LOW)
   attachInterrupt(0, golos, FALLING);
 
   //Turn on IR LED
   pinMode(ledPin, OUTPUT);
     digitalWrite(ledPin, HIGH);
 
  
   goalcount = 0;
 }
 
 void loop()
 {
   //Update goals every second
   delay(1000);
   detachInterrupt(0);
  
   
   
   lcd.clear();
   lcd.print("Golos");
   lcd.setCursor(0,1);
   lcd.print(goalcount);
   
   //Restart the interrupt processing
   attachInterrupt(0, golos, FALLING);
  }

Any help would be immensely apreciated!!! I’m eager to learn more about this kind of programming :slight_smile:

Why do you detach and attach interrupts? Why not just leave the interrupt attached?

As for updating the the screen, look at the blink without delay sketch here. (It is also in the IDE as the first digital example).

Forget interupts altogether. You only need those when things are happening at microsecond speeds. I'm sure you won't have two goals in less than 20 microseconds. The 'blink without delay', as suggested by ChilliTronics, principle is what you need. You should also look at http://forum.arduino.cc/index.php?topic=223286.0

Yep, get rid of those silly attach and detach statements in your loop and it'll likely work. I assume you're going to spin a fan between the LED and the phototransistor to get some results?

KenF: Yep, get rid of those silly attach and detach statements in your loop and it'll likely work. I assume you're going to spin a fan between the LED and the phototransistor to get some results?

Can you tell me wich part of the code I should remove, because i'm getting mixed answers :roll_eyes:

Oh and I did the fan test when the purpose of the program was to be a tachometer, now I was just thinking about scoring some goals to test it out :stuck_out_tongue:

do you have the same professor as http://forum.arduino.cc/index.php?action=profile;u=300884 ?

air hockey table post, uses the exact same 'tach' code and even has the same 'golos' function

http://forum.arduino.cc/index.php?topic=282146.0

dave-in-nj: do you have the same professor as http://forum.arduino.cc/index.php?action=profile;u=300884 ?

air hockey table post, uses the exact same 'tach' code and even has the same 'golos' function

http://forum.arduino.cc/index.php?topic=282146.0

Yes that's my friend, he's building the table with me, but he explained it kinda bad and I thought I should improve his post