Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Air Hockey Table software development

Status
Not open for further replies.

smeagol109

Newbie level 4
Newbie level 4
Joined
May 4, 2014
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
55
Hey guys, im developing an air hockey table and i am kinda confused with the code, i want to make a program using IR leds that counts the amount of goals in 1 goal. So i will have a pair of IR leds in the goal, and when the ball interrupts the beam it will count has 1 goal, this will be updated in a display installed on the table. Here is what i got so far.


Code dot - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
 * 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.
 *
 */
 
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);
  }




I dont know if the code is complete, nor 100% correct, if you could help me i would apreciate it alot, it would mean the world to me!!
Thanks in advance.
 
Last edited by a moderator:

I will have a pair of IR leds in the goal, and when the ball interrupts the beam it will count has 1 goal

I presume you mean 1 LED (transmitter) and one receiver device, is that correct?

Why do your code comments describe a tachometer? If these comments are incorrect, please update or remove them. It is a waste of everyone's time to post poorly commented code on the forum.

I dont know if the code is complete, nor 100% correct, if you could help me i would apreciate it alot, it would mean the world to me!!
Thanks in advance.

So, what trouble are you having specifically? What should the program do that it is not doing? Most of your functional code is interfaced via the included LiquidCrystal.h header file. This is specific to your hardware, so you need to describe your intentions much more clearly.

I can't even see an obvious entry point for your program (e.g. a main() function), so you really need to provide more information.
 
Last edited:

Oh yeah I'm sorry the comments are wrong don't mind them I adapted this from another code and forgot to update them

Yes I meant 1 LED (transmitter) and one receiver

So I only need to know if there is something missing here in order to a goal being counted each time the ball passes through the led

Don't mind the LCD part, I'm pretty sure that's correct, but I'm only in doubt as to the counting being made I only need to do

Code:
 goalcount++

I'm a super newbie when it comes to programming, can't you just tell me if the code makes sense to do what I want?

I adapted this from an rpm counter that indeed worked

it looked like 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 we used this setup Arduino-RPM-Counter.gif

Tell me more information you need so you can potentially help me
This is hard without guidance :-(
 

I presume you mean 1 LED (transmitter) and one receiver device, is that correct?

Yes that was what I meant

Why do your code comments describe a tachometer? If these comments are incorrect, please update or remove them. It is a waste of everyone's time to post poorly commented code on the forum.

Yes I'm so sorry, I forgot to remove/update the comments


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

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 Arduino-RPM-Counter.gif


Now I was trying to adapt it to a simple counter for goals and I am using the code you can see above, can you say If that is logically correct? I'm a noob when it comes to programming, so I'm sorry for any confusion, but I would apreciate a little guidance :-(
 

So, do you actually have a problem with the code? I mean, have you tried running it to see what happens? Does it behave as you expect?

I don't see any blatant problems, but you haven't really given any clues as to what I should be looking for.
 

Is adapted RPM code what you really want?

The RPM code is originally to count the number of times the IR beam is broken within a fixed time period so it can calculate the speed of the fan. In your application it seems all you need to do is count the number of times the beam is broken and keep incrementing the number until you reset it. Am I right?

Brian.
 
  • Like
Reactions: Magico

    Magico

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top