floppy
Posts: 7
Joined: Fri Dec 01, 2017 6:48 am

Bar billiard project.

Fri Jan 05, 2018 7:51 am

I apologise in advance for the long post, but a thought a detailed description of my proposed project was necessary.
I have a bar billiard table at home. The table has 9 holes and 9 independent slots down which the balls run after the ball is potted. Each hole has a different value. All the balls are white except 1 ball that is red. The red ball scores double points.
I would like to use my Pi3 to automate the scoring.
My knowledge of programming and electronics is very basic, so I looking for a solution that is relatively easy to understand and implement, rather than necessarily being the most elegant. Elegance can be introduced later as my knowledge increases.
My current concerns only lie with the ball detection. I have to detect a ball passing along the 9 slots and detect whether the ball is red. This needs to be done quick enough to allow for rolling balls and the fact that 2 (or even more) balls could be rolling down other slots at the same time.
To detect whether the ball is red I am thinking of using a TCS3200 RGB sensor. My understanding is that this sensor can set to detect separately white and red by setting 2 pins high or low, and therefore in theory a single sensor per slot could be used to detect both white and red balls. However, I am thinking that doing this on 9 sensors will tie up resources and cause delays due to reaction time of the sensor so that I may not reliably detect the colour of a ball passing by. So I am thinking that I could use a simple long arm micro switch to detect a passing ball (red or white) and then have the TCS3200 colour sensor permanently set to detect only red balls.
But how to interface the nine TCS3200 sensors to my Pi? Forum post 113235 discusses connecting a single sensor direct to a Pi and using the PIGPIO library. This seems quite complex and I do not understand how to extend this to cover 9 sensors. Elsewhere I have read about making the connection via a CD4040 binary counter. But then, how do I connect nine CD4040 counters to the Pi? Could I use a multiplexer?
Any advice to help point me in the right direction would be much appreciated.

scotty101
Posts: 2987
Joined: Fri Jun 08, 2012 6:03 pm

Re: Bar billiard project.

Fri Jan 05, 2018 12:48 pm

Given that white and red are such different colours, you should be able to use a simple light level detector to determine the difference between the balls. Get an analogue to digital converter (like MCP3008) and some light dependent resistors (LDRs) and a light source like a white LED.
With the correct layout of these components you should be able to both detect the presence of a ball and what colour it is.
I used a system like this to sort lego bricks using lego mindstorms years ago.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

floppy
Posts: 7
Joined: Fri Dec 01, 2017 6:48 am

Re: Bar billiard project.

Fri Jan 05, 2018 9:20 pm

Scotty, thanks for the fast response and the suggestion. I did not appreciate the LDR was sensitive enough to detect different colours. Your suggestion would certainly make the project simpler. For the last hour I have been reading up on LDR's as I have never used one before. I have noted 2 things. Firstly, these LDR seem to be generally used with a RGB LED, although I tend to agree with your thought that a white LED should be sufficient for my project. Secondly, and more importantly, DoI have some concern that they will not be fast enough. They seem to have a rise/fall time of about 30mSec. The ball is traveling at approximately 1mm/1mSec, so the ball is likely to pass the LDR before the LDR has the time to fully react. Of course I can minimise the potential problem by putting the LDR near to the top of the slot where the balls velocity will be at its lowest.
During my read-up of LDRs, I came across what seemed to me to be a similar device, a Light to Voltage Converter, such as a TSL260R-LF. I believe these have a much faster response time of less than 1mSec, which would easily be fast enough for my needs, but I appreciate that they are significantly more expense than the GL5516/5528/5537/5539 type of LDR.
Do you have any experience of the TSL260R-LF or similar product please?
You suggested use of a MCP3008 A/D. I need 9 channels. Should I use 2 off MCP3008, or are there 10 or 12 channel devices available? I could not find any.

User avatar
rpdom
Posts: 12669
Joined: Sun May 06, 2012 5:17 am
Location: Ankh-Morpork

Re: Bar billiard project.

Fri Jan 05, 2018 9:25 pm

If the slots are dark enough, just shining a certain colour light on the ball and using the LDR should be enough. A red or white ball illuminated with red light will both show as red. A red ball illuminated with blue or green light will show as black, but the white ball will show as blue or green.

floppy
Posts: 7
Joined: Fri Dec 01, 2017 6:48 am

Re: Bar billiard project.

Sat Jan 06, 2018 7:49 am

I have just realised that my plan was to use a MAX7219 dot matrix display to show the scores and an ILI9341 touch screen to act as the user interface. So they will be using the Pi's 2 SPI hardware interfaces.
The MCP3008 A/D that has been suggested, of which I need 2, are also SPI devices. Will I be able to use software SPI for these 2 A/D's, or should I change to I2C A/D's?
I would appreciate any advice please.

User avatar
DougieLawson
Posts: 33388
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Bar billiard project.

Sat Jan 06, 2018 10:17 am

There's five SPI interfaces on a regular RPi.

https://pinout.xyz/pinout/spi
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

floppy
Posts: 7
Joined: Fri Dec 01, 2017 6:48 am

Re: Bar billiard project.

Tue Jan 16, 2018 9:10 am

I have obtained some LDR (type 5528) and have carried out an initial experiment. In combination with a 5.6kohm resistor as a voltage divider, and a white LED, I obtained the following with a 3.3V supply:
no ball 1.5V
red ball 2.4V
white ball 3.0V

So the use of a LDR looks good to detect a ball and determine whether red or white - thanks to scotty101 for the suggestion.

Therefore I need to ideally detect whether the signal reaches a peak of say, between 2 to 2.7V or whether the peak reaches say, between 2.8V and 3.3V (with suitable A to D conversion).
Determining whether the signal has reached the 'white' voltage of greater than2.8V should be straight forward. But I am unclear about the best approach to determine the passage of a red ball. Obviously I cannot just detect a voltage of 2 to 2.7V to mean a red ball as this could be the approach of a white ball.
The only method that I can think is that if a voltage of, say, 2.0V is detected (meaning that a ball is approaching), then if the voltage, say 10mSec later (a time to be determined), is greater than 2.7V, it must be a white ball, otherwise it is a red ball.
Can anyone suggest a better solution please?

floppy
Posts: 7
Joined: Fri Dec 01, 2017 6:48 am

Re: Bar billiard project.

Tue Feb 06, 2018 10:16 am

I had made reasonable progress during the last few weeks (considering my very limited knowledge), but I have now hit a problem I do not know how to resolve, so I am looking for help please.
I am using a LDR and a pair of MCP3008 ADC for the ball detection. I have software written to detect presence and colour of the ball. This is working correctly. The pair of MCP3008 are connected to spi1 (1.0 and 1.1) (prompted by Dougie Lawson's input about the number of spi) For software debugging I have the ball colour and score displaying on a HDMI screen.
I then set up a Matrix7219 LED display to show the score. I connect this display to spi0. The display correctly shows the score.
BUT, I need to use spi0 for the touch screen (ILI9341 + XPT2046) that needs to use CE0 and CE1. I therefore tried to move the Matrix 7219 display to spi1 CE2. Now when I run the software the score counts up even with no ball passing the sensor. The MatrixLCD display does match the score display on the HDMI screen - it is just out of control.
If I disconnect the MOSI wire on the MatrixLED display, the software is back under control and the score is correctly displayed on the HDMI screen.I am thinking that my problem is due to 1 of 2 things (or maybe both, but either way, I do not know what to do about it):
1) SPI allows one device to talk at a time, so I am getting interaction between MCP3008 driver and the LUMA-LED driver
2) I have read that spi1 is not compatible with 3 wire spi which the MatrixLED is (no MISO pin)
I have read in other posts that 'bit-banging' is used to get around spi issues, but use of that frightens me.

Any thoughts or solutions would be most welcomed and appreciated.

PiGraham
Posts: 3321
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Bar billiard project.

Tue Feb 06, 2018 10:41 am

rpdom wrote:
Fri Jan 05, 2018 9:25 pm
If the slots are dark enough, just shining a certain colour light on the ball and using the LDR should be enough. A red or white ball illuminated with red light will both show as red. A red ball illuminated with blue or green light will show as black, but the white ball will show as blue or green.
This is a good method. Many RGB sensors are actually RG & B LEDs and a photo sensor.
You can illuminate with a red and a blue LED flashing alternately and a single photo sensor. You can probably arrange it so that you get two pulses from a white ball as the sensor is illuminated by red and blue flashes. On a red ball you get one pulse since the blue light is absorbed.
By timing the pulses you can tell that white or red balls are passing.
One GPIO input per sensing location and two outputs for all sensors to alternately flash the set of red and blue LEDS.

floppy
Posts: 7
Joined: Fri Dec 01, 2017 6:48 am

Re: Bar billiard project.

Tue Feb 06, 2018 1:39 pm

I should have added that the scripts for the MCP3008 and the Matrix display are in concurrent threads, so maybe I have a threading issue.

PiGraham
Posts: 3321
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Bar billiard project.

Tue Feb 06, 2018 1:57 pm

floppy wrote:
Tue Feb 06, 2018 1:39 pm
I should have added that the scripts for the MCP3008 and the Matrix display are in concurrent threads, so maybe I have a threading issue.
I think you will have problems if you try to use one SPI resource from two separate threads without synchronisation.

GPIOZero with pigpio has something called PiGPIOSoftwareSPI which might be worth a look.


Alternatively you could use opto sensor signals to comparators and read them as binary, dispensing with the ADC and freeing up the SPI for display use.

Return to “Automation, sensing and robotics”

Who is online

Users browsing this forum: No registered users and 24 guests