How do you code an arduino to count and display on LCD each number every time a relay triggers?

OK, I'm kinda new at the programming part for my projects. I was wondering if anyone can help me with (what I assume will be a simple code for a veteran) a code for arduino and maybe with a little explanation of the code. Project is simple. I have a toy laser gun shooting at a target with a photocell attached and connected to a relay. What I would like is that every time the relay switches, a counter adds the shot so I can keep score. It's for my son's 9th birthday party and I've already built the targeting range (with  Lego Movie theme) and even some servos to knock back guys down.  I just need a way to keep score.

I have some Arduino Uno's, some nanos, some trinkets, some LCDs (2X16 and 4X8) and 4X 7segment displays so I have some options for parts,but at the moment I do not have a lot of time to go through a self-learning curve to get the coding to work.  Can anyone please help?  It would be greatly appreciated. Thanks.

sort by: active | newest | oldest

The code is very, very easy, but what do you want for a display ? If you use the 16 x 2 can you wire it to the Arduino OK ?

jedii72 (author)  steveastrouk3 years ago

I must be stupid because I'm having a very difficult time on something I know should be very easy. Help. Two things:

1. kid shoots a gun with laser with laser diode installed, laser hits photocell, photocell triggers relay. I want the relay to connect to Arduino. When relay triggers, display on 16X2 says "1". When kid hits target again, display says "2". I want it to count the number of hits to keep score and decide who wins the prize.

2. I followed the wiring from Arduino.cc for 16X2 but none of my LCDs come on or even give me the "hello world" display. I've switched potentiometer, arduinos, and the displays and still nothing. I must be missing something.

Any advise would be greatly appreciated. I'm new to this and the coding is really giving me a headache; mainly because I'm just new to it. Thanks.

1.) Why the relay ? Is it doing something else ? You can connect the cell to the arduino directly.

2.) The LCD won't do ANYTHING until there is some code to drive it.

Here is the minimum test program.

http://arduino.cc/en/Tutorial/LiquidCrystal

/*

LiquidCrystal Library - Hello World



Demonstrates the use a 16x2 LCD display. The LiquidCrystal

library works with all LCD displays that are compatible with the

Hitachi HD44780 driver. There are many of them out there, and you

can usually tell them by the 16-pin interface.



This sketch prints "Hello World!" to the LCD

and shows the time.



The circuit:

* LCD RS pin to digital pin 12

* LCD Enable pin to digital pin 11

* LCD D4 pin to digital pin 5

* LCD D5 pin to digital pin 4

* LCD D6 pin to digital pin 3

* LCD D7 pin to digital pin 2

* LCD R/W pin to ground

* 10K resistor:

* ends to +5V and ground

* wiper to LCD VO pin (pin 3)



Library originally added 18 Apr 2008

by David A. Mellis

library modified 5 Jul 2009

by Limor Fried (http://www.ladyada.net)

example added 9 Jul 2009

by Tom Igoe

modified 22 Nov 2010

by Tom Igoe



This example code is in the public domain.



http://www.arduino.cc/en/Tutorial/LiquidCrystal

*/



// include the library code:

#include <LiquidCrystal.h>



// initialize the library with the numbers of the interface pins

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);



void setup() {

// set up the LCD's number of columns and rows:

lcd.begin(16, 2);

// Print a message to the LCD.

lcd.print("hello, world!");

}



void loop() {

// set the cursor to column 0, line 1

// (note: line 1 is the second row, since counting begins with 0):

lcd.setCursor(0, 1);

// print the number of seconds since reset:

lcd.print(millis()/1000);

}
jedii72 (author)  steveastrouk3 years ago

That's the code I've been using. I'm not getting anything. When troubleshooting I discovered there is an "A" and "K" pin on the sides of the LCDs which are apparently to the backlight. Those are not mentioned in the arduino wiring so I'm thinking my LCDs may be different.

I appreciate you taking the time to help. and I hate to ask, but do you have any code for use as the counter so the arduino counts each shot?

When a 16 x 2 LCD is powered, you'll see a single "black" bar across ONE line. First, if you can't, check your contrast pot.

When its INITIALISED by the .begin line, you should see BOTH lines go kind of grey.

Check your connections, and connect the UNUSED pins D0..D3 to ground - which often helps.

That's something like

//declare relay contact as an input

int ctr =0;

setup{

lcd.begin (16,2);

}

while (0==0)

{if (relay==0) then ctr++

while (relay==0); //wait until relay resets

delay (10); //wait 10msec or so.

lcd.print ("Your score is ",ctr)

}