Arduino Forum

Using Arduino => Project Guidance => Topic started by: jjeffh on Dec 12, 2018, 08:07 pm

Title: Can't get Coburn's code for Buzz Wire Game to compile
Post by: jjeffh on Dec 12, 2018, 08:07 pm
My 10 year-old grandson and I are both new to Arduino. I want to prepare a project to do with him for Christmas, and I found Joe Coburn's Buzz Wire Game (https://www.makeuseof.com/tag/make-buzz-wire-game-arduino/) on Make Use Of from 9/21/16.

It looks like something he could get into and we could do together, but I can't get the code he posted to work.

He refers to pitches.h and I followed the instructions for creating a new tab and copying in the code. But when I compile, I get an error "No such file or directory", even though I see it in the Arduino\BuzzWire folder along with BuzzWire.ino.

He also gives instructions for bringing in and editing TM1637.cpp. The editing is to expand the numbers and letters that can be displayed. However, I'm also getting an error on this, both with and without the edits. When he explains the editing he says to edit .cpp and not .h, but the code includes the line "#include <TM1637.h>" and that's where the error occurs.

I am working from the Arduino Starter Kit and I've already ordered the additional items needed for this project, but I'm hoping someone can help me with the coding.

Thanks!
- Jeff
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: groundFungus on Dec 12, 2018, 09:07 pm
Read the how to use this forum sticky to see how to post code and error messages.  It will go a lot faster if you know and follow the guidelines.

Then post the code that you are having trouble with and the entire text of the error message(s) and we can help.

Which Arduino board are you using?
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: jjeffh on Dec 12, 2018, 10:34 pm
Read the how to use this forum sticky to see how to post code and error messages.  It will go a lot faster if you know and follow the guidelines.

Then post the code that you are having trouble with and the entire text of the error message(s) and we can help.

Which Arduino board are you using?
Thanks, groundFungus. Sorry for not providing enough information. I had read "How to Post", but I was trying to summarize and get direction before dumping all the code. I'm using an Arduino Uno (from the Starter Kit). [And now, after I had added all the code, I got the forum error "Exceeding 9000 characters". I'm not really sure what is needed in order to isolate the problem, but I'm minimizing it now. Please let me know what is needed.]

I had posted the full code from Coburn's project instructions, but below are just the first few lines, which are getting the errors. I'm also posting the error messages for pitches.h, and the code for TM1367 (which it says is installed when I search for it in Manage Libraries).

The first few lines of Coburn's code for the project:
Code: [Select]
#include <TM1637.h> // include display library
#include <pitches.h> // include pitches

TM1637 *_display = new TM1637(12, 13); // create display object, 12 = CLK (clock), 13 = D10 (data)

// etc. ...


When I compile Coburn's code, the first line returns the error:
Code: [Select]
Arduino: 1.8.8 (Windows Store 1.8.19.0) (Windows 10), Board: "Arduino/Genuino Uno"

BuzzWire:1:47: error: TM1637.h: No such file or directory

compilation terminated.

exit status 1
TM1637.h: No such file or directory

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.


When I rem that line out and recompile, the second line returns this error:
Code: [Select]
Arduino: 1.8.8 (Windows Store 1.8.19.0) (Windows 10), Board: "Arduino/Genuino Uno"

BuzzWire:2:40: error: pitches.h: No such file or directory

compilation terminated.

exit status 1
pitches.h: No such file or directory

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.



Should I also include the code for pitches.h and TM1637.cpp?


Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: groundFungus on Dec 13, 2018, 12:33 am
I followed the instructions in the  Joe Coburn's Buzz Wire Game page that you linked and get the same errors.  I installed the TM1637 library through the library manager.  In the TM1637 library folder are TM1637Display.h (and TM1637Display.cpp) instead of the TM1637.h file that the program is #including.  I believe that that is the source of the error.

OK, I got it to compile, but cannot test.  Here is what I did.

Since the library downloaded from the library manager did not seem to be the right one I started looking for an alternative.  The page mentioned that the library was written by Seeed so I looked there.  There is a library for the TM1637 there.    Download the zip file of the library form this page. (https://github.com/Seeed-Studio/Seeed_Grove_4Digital_Display_g)  Once downloaded, go to Sketch, Include Library, Add Zip Library.  Click that and navigate to where the downloaded zip file is. Double click the
Seeed_Grove_4Digital_Display_g-master.zip file and it will be installed.

If you have not, copy the pitches.h file into a tab in your program in the IDE and name the tab pitches.h. 
Change the line #include <pitches.h> to #include "pitches.h".

The program should compile.  If successful, make the changes to the .ccp file and try again.  The TM1637.cpp file will be in the Seeed_Grove_4Digital_Display_g-master folder in the libraries folder (not a TM1637 folder like the page says).
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: jjeffh on Dec 13, 2018, 02:41 am
Wow! Thanks very much. I haven't tried it yet, but I will do so tomorrow and report back.

- Jeff
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: jjeffh on Dec 13, 2018, 05:49 pm
Those are excellent and clear instructions, groundFungus. This worked perfectly! Many thanks.

I have not built the circuit yet, but I'm expecting the components I need to arrive today so hopefully all will go well.

Again, thanks! This is my first posting, so I don't know how the "karma" feature works, but I'm going to try it now.

- Jeff

Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: groundFungus on Dec 13, 2018, 06:45 pm
Thank you for the feedback.  If you have trouble with the project once you get the parts, I will be glad to help.
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: jjeffh on Dec 13, 2018, 08:28 pm
Well, I don't know if this should be a new thread, but I'm building the circuit now and just realized the starter kit has an LCD display but the project uses a 7-segment display. Coburn specifically references a Seeed model which won't ship until December 23rd and will cost $20 to arrive in 1-3 days, or $5 to arrive in 10-28 days after that.

So I have two questions:

1. Could I adapt Coburn's code to the LCD display (with the understanding that I'm a know-nothing beginner)?

2. Could I use this Adafruit (https://www.amazon.com/Adafruit-4-Digit-7-Segment-Display-Backpack/dp/B00SLYARJQ/ref=sr_1_3?ie=UTF8&qid=1544727210&sr=8-3&keywords=7-segment+display) model from Amazon, which will be here Saturday for free through Prime? And if so, is this description enough to indicate I'm likely to be able to connect it as Coburn describes:
Quote from: Amazon
I2C Backpack (Requires Soldering) that allows the display to be driven with just 4 pins!
Setup guide and Arduino libraries available from Adafruit
To clarify the situation, I'm trying to complete this project and test it now, so I can take it apart and redo it with my 10-year old grandson as a Christmas activity. There is a third option regarding the display: It's not essential to the project, so I could skip it for now and add it in later if the project goes well with my grandson.
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: groundFungus on Dec 13, 2018, 09:10 pm
Quote
1. Could I adapt Coburn's code to the LCD display (with the understanding that I'm a know-nothing beginner)?
I have looked at the code and believe that it will not be terribly difficult to modify the code to use an LCD.  The first challenge is to get the LCD to work by itself and display data that you hard code (the "hello world" example, for example). 

Can you post photos of the LCD from the starter kit so that I know what we are working with?  Front and back sides?

How to post images. (https://forum.arduino.cc/index.php?topic=364156.0)
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: jjeffh on Dec 13, 2018, 09:24 pm
Here goes ...

(https://forum.arduino.cc/index.php?action=dlattach;topic=584777.0;attach=285768)

(https://forum.arduino.cc/index.php?action=dlattach;topic=584777.0;attach=285770)
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: groundFungus on Dec 13, 2018, 10:07 pm
OK, that is a "standard" 1602 display.  Can you install the hd44780 LCD library?  Use the library manager like you did with the TM1637 library.  Once the library manager window is open select displays in the Topic drop down and enter hd44780 in the Filter box.   Select and install the hd44780 by Bill Perry library.  

Looking at the photo of the front of the LCD, pin 1 is on the top left and pin 16 on the right.  The LCD is right side up in the photo.  Plug the LCD into your breadboard and wire the LCD as follows.

Uno pin                       LCD pin
ground                          1
Vcc(5V)                        2
see below1                 3
     11                             4
to ground                     5
      12                            6
                              7-10 not connected
      4                             11
      5                             12
      6                             13
      7                             14
Vcc(5V)                         15
see below2             16

1 tie pin 3 of the LCD to ground through a 1K resistor.  This sets the contrast.
2 Tie pin 16 of the LCD to ground through a 220 to 470 Ohm resistor.  This sets the backlight.

I will put some test code in the next post.  
 
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: groundFungus on Dec 13, 2018, 10:14 pm
Here is the test code.  This has been tested on my Uno with a 1602 16x2 LCD with 4 pin interface.

Code: [Select]
//Modified hd44780 library (by Bill Perry) "hello world" example.  Mod by C. Goulding

#include <hd44780.h>
#include <hd44780ioClass/hd44780_pinIO.h> // Arduino pin i/o class header

const int rs = 11, en = 12, db4 = 4, db5 = 5, db6 = 6, db7 = 7; // for all other devices

hd44780_pinIO lcd(rs, en, db4, db5, db6, db7);

// LCD geometry
const int LCD_COLS = 16;
const int LCD_ROWS = 2;

void setup()
{
   lcd.begin(LCD_COLS, LCD_ROWS);
   // Print a message to the LCD
   lcd.print("Hello, Jeff!");
}

void loop() {}
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: jjeffh on Dec 13, 2018, 11:51 pm
I think I have it wired correctly, but when I upload I get this error:
Code: [Select]
Arduino: 1.8.8 (Windows Store 1.8.19.0) (Windows 10), Board: "Arduino/Genuino Uno"

Sketch uses 3148 bytes (9%) of program storage space. Maximum is 32256 bytes.
Global variables use 108 bytes (5%) of dynamic memory, leaving 1940 bytes for local variables. Maximum is 2048 bytes.
avrdude: ser_open(): can't open device "\\.\COM3": The system cannot find the file specified.


Problem uploading to board.  See http://www.arduino.cc/en/Guide/Troubleshooting#upload for suggestions.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

I see it's looking for COM3, but I'm using a different UNO for this and my computer automatically sets it up as COM4. I can't see where to change that.

Here is the wiring as I set it up. I've tied pins 4, 6, 11, 12, 13, and 14 to keep them out of the way and show the Grounding pins (1, 3, 16) and the Power pins (2 and 15). Pin 3 has a Brown/Black/Red resistor (1k) and pin 15 has a Red/Red/Brown (220).

(https://forum.arduino.cc/index.php?action=dlattach;topic=584777.0;attach=285796)
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: jjeffh on Dec 14, 2018, 12:11 am
Hold on!! I went into Device Manager and deleted COM4, refreshed, then deleted COM3, and rebooted. That cleared the ports and set up COM3 for this test. (So does Arduino require COM3? I'm using Windows 10.)

And it worked! "Hello, Back-at-you!" (I feel funny calling you groundFungus ... feels almost like an insult.  :smiley-confuse: )

So now I need to replicate these connections in my Buzz Wire project and replace Coburn's display code with your code, right?

You have been very patient with me and very much appreciate it. I'm hoping you are willing to bear with me a little longer because my initial tests with the project setup did not go that well. I want to bring this display in then retest and I might need to ask a couple more questions.

Many thanks,
- Jeff
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: groundFungus on Dec 14, 2018, 12:29 am
Glad to know that the LCD works. 

You choose the com port in the Tools menu.  Click on Ports and the port that the Uno is connected to will show and can be selected.

It is a bit more complicated than just replacing a few lines to put the LCD in place of the 7 seg. display.  The data was written to the TM1637 a digit at a time, but with the LCD you can write the line at a time.  Use the setCursor() function to place the cursor on the character position and line that you want to write to.  Give me a little time and I will get started and post code later.
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: groundFungus on Dec 14, 2018, 01:02 am
Here is the code with the TM1637 stuff removed and the LCD stuff inserted.  Note that I had to change one pin as there was a conflict between one of the buzzers and the LCD.  Changed the buzzer to pin 8.  See comment in code.  Don't forget to copy the pitches.h file into a tab.  The #include is already fixed.  Code tested to the extent that I can without building the entire circuit.  You now have a lot more flexibility in terms of what you can display.

Code: [Select]
#include <hd44780.h>
#include <hd44780ioClass/hd44780_pinIO.h> // Arduino pin i/o class header
#include "pitches.h" // include pitches

const int rs = 11, en = 12, db4 = 4, db5 = 5, db6 = 6, db7 = 7; // for all other devices

hd44780_pinIO lcd(rs, en, db4, db5, db6, db7);

// LCD geometry
const int LCD_COLS = 16;
const int LCD_ROWS = 2;

// music
int songState = 0;

int melody[] =
{
   NOTE_F4, NOTE_E4, NOTE_D4, NOTE_CS4,
   NOTE_C4, NOTE_B3, NOTE_AS3, NOTE_A3,
   NOTE_G3, NOTE_A3, NOTE_AS3, NOTE_A3,
   NOTE_G3, NOTE_C4, 0,

   NOTE_C4, NOTE_A3, NOTE_A3, NOTE_A3,
   NOTE_GS3, NOTE_A3, NOTE_F4, NOTE_C4,
   NOTE_C4, NOTE_A3, NOTE_AS3, NOTE_AS3,
   NOTE_AS3, NOTE_C4, NOTE_D4, 0,

   NOTE_AS3, NOTE_G3, NOTE_G3, NOTE_G3,
   NOTE_FS3, NOTE_G3, NOTE_E4, NOTE_D4,
   NOTE_D4, NOTE_AS3, NOTE_A3, NOTE_A3,
   NOTE_A3, NOTE_AS3, NOTE_C4, 0,

   NOTE_C4, NOTE_A3, NOTE_A3, NOTE_A3,
   NOTE_GS3, NOTE_A3, NOTE_A4, NOTE_F4,
   NOTE_F4, NOTE_C4, NOTE_B3, NOTE_G4,
   NOTE_G4, NOTE_G4, NOTE_G4, 0,

   NOTE_G4, NOTE_E4, NOTE_G4, NOTE_G4,
   NOTE_FS4, NOTE_G4, NOTE_D4, NOTE_G4,
   NOTE_G4, NOTE_FS4, NOTE_G4, NOTE_C4,
   NOTE_B3, NOTE_C4, NOTE_B3, NOTE_C4, 0
};

int tempo[] =
{
   8, 16, 8, 16,
   8, 16, 8, 16,
   16, 16, 16, 8,
   16, 8, 3,

   12, 16, 16, 16,
   8, 16, 8, 16,
   8, 16, 8, 16,
   8, 16, 4, 12,

   12, 16, 16, 16,
   8, 16, 8, 16,
   8, 16, 8, 16,
   8, 16, 4, 12,

   12, 16, 16, 16,
   8, 16, 8, 16,
   8, 16, 8, 16,
   8, 16, 4, 16,

   12, 17, 17, 17,
   8, 12, 17, 17,
   17, 8, 16, 8,
   16, 8, 16, 8, 1
};

// non blocking setup
// free play
unsigned long previousMillis1 = 0; // time words last changed
const long interval1 = 1500; // interval between changing

// music
unsigned long previousMillis2 = 0; // time last changed
const long interval2 = 100; // interval between notes

int displayStatus = 0; // keep track of what's displayed
int mode = 0; // keep track of game mode -- change to 0 or 1 for different modes

bool countdown = false;

unsigned long previousMillis3 = 0; // time last changed
const long interval3 = 1000; // interval between countdown
int count = 20; // challenge mode timer

void setup()
{
   // put your setup code here, to run once:
   pinMode(9, INPUT); // setup circuit
   pinMode(10, OUTPUT); // setup buzzer 1
   pinMode(8, OUTPUT); // setup buzzer 2  // had to change this pin. conflict with LCD
   pinMode(2, INPUT); // setup button

   lcd.begin(LCD_COLS, LCD_ROWS);
   lcd.print("BUZZ REAADY");
   delay(1000);
   lcd.clear();
}

void loop()
{
   // put your main code here, to run repeatedly:
   if (mode == 0)
   {
      // challenge mode
      if (digitalRead(2) == HIGH)
      {
         delay(25);
         if (digitalRead(2) == HIGH)
         {
            countdown = true; // stop the countdown
         }
         else
         {
            countdown = false; // stop the countdown
         }
      }
      if (countdown)
      {
         showCountdown(); // advance countdown
      }
   }
   else
   {
      // free play
      toggleFreePlay();
   }
   if (digitalRead(10) == HIGH)
   {
      delay(25);
      if (digitalRead(10) == HIGH)
      {
         while (digitalRead(10) == HIGH)
         {
            buzz(11, NOTE_B0, 1000 / 24);
         }
      }
   }
   else
   {
      sing();
    }
}

void showCountdown()
{
   // countdown the time remaining
   unsigned long currentMillis = millis(); // current time
   if (currentMillis - previousMillis3 >= interval3)
   {
      previousMillis3 = currentMillis;
      --count;
      showNumber(count);
      if (count == 0)
      {
         // game over
         countdown = false;
         count = 20;
         // reset countdown
         // buzz 3 times
         buzz(11, NOTE_B0, 1000 / 24);
         delay(100);
         buzz(11, NOTE_B0, 1000 / 24);
         delay(100);
         buzz(11, NOTE_B0, 1000 / 24);
      }
   }
}

void showNumber(int number)
{
  lcd.clear();
  lcd.setCursor(4,0);
  lcd.print("-- "); 
  lcd.setCursor(7,0);
  lcd.print(number);
  lcd.print(" --");
}

void toggleFreePlay()
{
   // scroll between words without blocking
   unsigned long currentMillis = millis(); // current time
   if (currentMillis - previousMillis1 >= interval1)
   {
      previousMillis1 = currentMillis;
      if (displayStatus == 1)
         showPlay();
      else
         showFree();
   }
}

void showPlay()
{
   // write "PLAY" to the display
   lcd.clear();
   lcd.setCursor(6,0);
   lcd.print("PLAY");
   displayStatus = 2;
}

void showFree()
{
   // write "Free" to the display
   lcd.clear();
   lcd.setCursor(6,1);
   lcd.print("FREE");
   displayStatus = 1;
}

void buzz(int targetPin, long frequency, long length)
{
   /* Buzzer example function by Rob Faludi
      http://www.faludi.com
      https://gist.github.com/AnthonyDiGirolamo/1405180
   */
   long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
   //// 1 second's worth of microseconds, divided by the frequency, then split in half since
   //// there are two phases to each cycle
   long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
   //// multiply frequency, which is really cycles per second, by the number of seconds to
   //// get the total number of cycles to produce
   for (long i = 0; i < numCycles; i++) // for the calculated length of time...
   {
      digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphragm
      delayMicroseconds(delayValue); // wait for the calculated delay value
      digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphragm
      delayMicroseconds(delayValue); // wait again for the calculated delay value
   }
}

void sing()
{
   // play the song in a non blocking way
   unsigned long currentMillis = millis();

   if (currentMillis - previousMillis2 >= interval2)
   {
      previousMillis2 = currentMillis;
      int noteDuration = 1000 / tempo[songState];
      buzz(10, melody[songState], noteDuration);
      int pauseBetweenNotes = noteDuration;
      delay(pauseBetweenNotes);

      // stop the tone playing:
      buzz(10, 0, noteDuration);

      ++songState;
      // start song again if finished
      if (songState > 79)
      {
         songState = 14; // skip intro
      }
   }
}


Have fun, Charlie.
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: jjeffh on Dec 14, 2018, 04:18 am
Outstanding! I'm looking forward to trying this, Charlie. I've done a good deal of coding in VBA for Excel, but never in C++ (or any other language). I'm getting a bit of education about C++ and circuits both.




Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: ardy_guy on Dec 14, 2018, 04:55 am
I'm getting a bit of education about C++ and circuits both.
And that's all part of it: you start by just trying to get something done because you have a result in mind, which looks dead simple because it's all written in a book, and learn stuff along the way too. (Although my kids are both engineers in their 20s (fire and electrical, respectively*), no grandkids here yet. I guess my days of Christmas projects have still to come.)

*Just had a thought about that: if one's sparks cause a fire, we should be safe.

Kudos to groundFungus for the way he handled this thread. Certain other members could learn from that....
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: groundFungus on Dec 14, 2018, 08:42 am
Thanks, ardy_guy and Jeff. 

Jeff, as you go along with learning you will see that the circuit could use some small improvements.  The push button switch could be wired to ground and use the internal pullup resistor instead of the external pull down and wand could be wired the same without the voltage divider.  That is, the course wired to ground and the wand to a digital input with the internal pullup.  The logic for the push button and the wand would need to be adjusted for active low logic.
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: jjeffh on Dec 14, 2018, 03:41 pm
... Kudos to groundFungus for the way he handled this thread. ...
Hear! Hear!

Jeff, as you go along with learning you will see that the circuit could use some small improvements.  The push button switch could be wired to ground and use the internal pullup resistor instead of the external pull down and wand could be wired the same without the voltage divider.  That is, the course wired to ground and the wand to a digital input with the internal pullup.  The logic for the push button and the wand would need to be adjusted for active low logic.
I'm afraid all that just zinged right over my head. I'm still working on what ardy-guy called the "dead simple" stuff: copying directly out of the book and hoping something works.

I haven't started yet today, but in my tests yesterday the Monty Python theme Coburn programmed in played fine, but when I touched the wand and course leads, I didn't get a buzz, which is the point of the project.

And regarding the Monty Python theme, much as I like it, the circuit tweak that occurred to me was a way to turn it on and off, or at least subdue it. Coming out of that little piezo it kind of grates after a while. (Not to mention it clearly bothered my cat!)

I'll be starting over today with the new code to see what I get. Wish me luck!

BTW, the lesson in how to use the 2 line LCD display really gives me something I think I can play with with Chance (the grandson) aside from this project.
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: groundFungus on Dec 14, 2018, 05:56 pm
I will look and see if the buzzer problem is in the code or circuit.

You could add a button to turn the MP theme off and on.  If you want, I will look at that.


Wow, here is a coincidence.  My son's name is Chance.
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: jjeffh on Dec 14, 2018, 11:23 pm
That's pretty cool about the shared name!

Today completely got away from me, so I won't be able to rebuild the circuit until tomorrow (I'm in EST, 5- the forum time).

I don't know if the lack of a buzzer when the wand and course leads touch was a construction or code error, so I'm going to revisit that when I get back to the project.

A music-on-off button would be cool. I'd like to see how that's done. One problem I have with the push-buttons in general is that they don't seat well on the solderless breadboard. I tried straightening the contacts, but that didn't help. Could that just be the ones supplied in the Starter Kit?

The one thing I was able to do today was read through your code. I found 4 instances of "buzz()" which included an argument for "11" (1 under "// free play", and 3 under "void showCountdown()"). I'm assuming I should change those 11's to 8's, right? But I also see "const int rs = 11, ..." at the top, in the 4th line of the code. I'm not sure if that reference to 11 is about the pin and/or whether it should be 8.
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: groundFungus on Dec 15, 2018, 12:15 am
You are right about those switches.  They can be a pain.  I take a pair of needle nose and twist the legs 90 degrees so they fit into the breadboard easier.  A marginal improvement.

You are also right about the pin 11.   I missed changing that.   Change those pins to 8.  That is another thing about that code.  Definitely written by an inexperienced coder.  You should not use pin numbers like that in code.  Give the pin a name, in global scope, then use the name in the code.  That way, to change the pin number it needs done in only one place.

Leave the int rs =11;  that is for the LCD.  Just change the ones related to the buzzer.



Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: jjeffh on Dec 15, 2018, 07:27 pm
I'm back...

I've got the board set up for Buzz Wire, with the LCD display including the change of buzzer 2 to pin 8. I've compiled and uploaded your code (with the corrections to buzzer 2). I've been over the connections with a flashlight and magnifying glass and they seem to be correct. But all I'm getting is the Monty Python theme.

When I touch the course and wand leads (wand to power; course grounded through two 220 resistors), there's no buzz. (Monty Python only plays through buzzer 1, incidentally. I'm guessing Coburn wrote it to play the music through buzzer 1 and the touch buzzer through buzzer 2.)

There's no display on the LCD (should be "BUZZ READY", right?). And when I press the button nothing happens except that the music slows down slightly. (BTW, good tip about twisting the button contacts inward.)

I also uploaded your LCD, "Hello World" code, which worked fine when I set up just for that, but I'm getting nothing on the LCD in this configuration.

In case you still think there's any hope, I've uploaded an annotated pdf of the LCD setup. But I'll understand if it looks hopeless ... it kind of feels that way.

Maybe I should just take Chance to Chuck E Cheese!

Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: groundFungus on Dec 15, 2018, 10:22 pm
I don't see anything wrong with the wiring.  Can you post the code that you are running?   Does the display show anything?
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: jjeffh on Dec 15, 2018, 10:48 pm
I don't see anything wrong with the wiring.  Can you post the code that you are running?   Does the display show anything?
Nothing on the display, not even the back lighting.

I've attached the code as a Word doc. I copied it directly from Arduino, then added line numbers in Word in case that might help.

Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: groundFungus on Dec 15, 2018, 11:50 pm
It is OK for now, but in the future, please post in code tags.  That way I can copy to the IDE (the IDE will insert line numbers).  I have to remove the line numbers to be able put the code in the IDE. 

How are the breadboard's bottom power rails getting power and ground.  They are not tied to the top rails unless you make the connection.

And a hint to make things easier.  Use red jumpers for 5V and black for ground.  I use the colors of the jumpers to make things easier to trace.  I go by the resistor color code and the jumpers color reflects the Uno pin number that the jumper comes from. 

Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: jjeffh on Dec 16, 2018, 12:02 am
Whoops!! Sorry about that. I wasn't thinking. In case you haven't already gone to all that trouble, here it is, below.

Regarding power and ground, in the picture you can sort of see a red and a black lead going to the power and ground rails in the upper right corner of the board (from 5v and GND). It's pretty much in the middle of the picture. Then at the other end of those rails I have two orange leads, one taking the power and the other taking the ground to the opposite rails of the board.

Code: [Select]
#include <hd44780.h>
#include <hd44780ioClass/hd44780_pinIO.h> // Arduino pin i/o class header
#include "pitches.h" // include pitches

const int rs = 11, en = 12, db4 = 4, db5 = 5, db6 = 6, db7 = 7; // for all other devices

hd44780_pinIO lcd(rs, en, db4, db5, db6, db7);

// LCD geometry
const int LCD_COLS = 16;
const int LCD_ROWS = 2;

// music: Monty Python theme
int songState = 0;

int melody[] =
{
   NOTE_F4, NOTE_E4, NOTE_D4, NOTE_CS4,
   NOTE_C4, NOTE_B3, NOTE_AS3, NOTE_A3,
   NOTE_G3, NOTE_A3, NOTE_AS3, NOTE_A3,
   NOTE_G3, NOTE_C4, 0,

   NOTE_C4, NOTE_A3, NOTE_A3, NOTE_A3,
   NOTE_GS3, NOTE_A3, NOTE_F4, NOTE_C4,
   NOTE_C4, NOTE_A3, NOTE_AS3, NOTE_AS3,
   NOTE_AS3, NOTE_C4, NOTE_D4, 0,

   NOTE_AS3, NOTE_G3, NOTE_G3, NOTE_G3,
   NOTE_FS3, NOTE_G3, NOTE_E4, NOTE_D4,
   NOTE_D4, NOTE_AS3, NOTE_A3, NOTE_A3,
   NOTE_A3, NOTE_AS3, NOTE_C4, 0,

   NOTE_C4, NOTE_A3, NOTE_A3, NOTE_A3,
   NOTE_GS3, NOTE_A3, NOTE_A4, NOTE_F4,
   NOTE_F4, NOTE_C4, NOTE_B3, NOTE_G4,
   NOTE_G4, NOTE_G4, NOTE_G4, 0,

   NOTE_G4, NOTE_E4, NOTE_G4, NOTE_G4,
   NOTE_FS4, NOTE_G4, NOTE_D4, NOTE_G4,
   NOTE_G4, NOTE_FS4, NOTE_G4, NOTE_C4,
   NOTE_B3, NOTE_C4, NOTE_B3, NOTE_C4, 0
};

int tempo[] =
{
   8, 16, 8, 16,
   8, 16, 8, 16,
   16, 16, 16, 8,
   16, 8, 3,

   12, 16, 16, 16,
   8, 16, 8, 16,
   8, 16, 8, 16,
   8, 16, 4, 12,

   12, 16, 16, 16,
   8, 16, 8, 16,
   8, 16, 8, 16,
   8, 16, 4, 12,

   12, 16, 16, 16,
   8, 16, 8, 16,
   8, 16, 8, 16,
   8, 16, 4, 16,

   12, 17, 17, 17,
   8, 12, 17, 17,
   17, 8, 16, 8,
   16, 8, 16, 8, 1
};

// non blocking setup
// free play
unsigned long previousMillis1 = 0; // time words last changed
const long interval1 = 1500; // interval between changing

// music
unsigned long previousMillis2 = 0; // time last changed
const long interval2 = 100; // interval between notes

int displayStatus = 0; // keep track of what's displayed
int mode = 0; // keep track of game mode -- change to 0 or 1 for different modes

bool countdown = false;

unsigned long previousMillis3 = 0; // time last changed
const long interval3 = 1000; // interval between countdown
int count = 20; // challenge mode timer

void setup()
{
   // put your setup code here, to run once:
   pinMode(9, INPUT); // setup circuit
   pinMode(10, OUTPUT); // setup buzzer 1
   pinMode(8, OUTPUT); // setup buzzer 2  // had to change this pin. conflict with LCD
   pinMode(2, INPUT); // setup button

   lcd.begin(LCD_COLS, LCD_ROWS);
   lcd.print("BUZZ READY");
   delay(1000);
   lcd.clear();
}

void loop()
{
   // put your main code here, to run repeatedly:
   if (mode == 0)
   {
      // challenge mode
      if (digitalRead(2) == HIGH)
      {
         delay(25);
         if (digitalRead(2) == HIGH)
         {
            countdown = true; // stop the countdown [shouldn’t this be ‘start the countdown’?]
         }
         else
         {
            countdown = false; // stop the countdown
         }
      }
      if (countdown)
      {
         showCountdown(); // advance countdown
      }
   }
   else
   {
      // free play
      toggleFreePlay();
   }
   if (digitalRead(10) == HIGH)
   {
      delay(25);
      if (digitalRead(10) == HIGH)
      {
         while (digitalRead(10) == HIGH)
         {
            buzz(8, NOTE_B0, 1000 / 24); //[This was changed from 11 to 8]
         }
      }
   }
   else
   {
      sing();
    }
}

void showCountdown()
{
   // countdown the time remaining
   unsigned long currentMillis = millis(); // current time
   if (currentMillis - previousMillis3 >= interval3)
   {
      previousMillis3 = currentMillis;
      --count;
      showNumber(count);
      if (count == 0)
      {
         // game over
         countdown = false;
         count = 20;
         // reset countdown
         // buzz 3 times
         buzz(8, NOTE_B0, 1000 / 24); //[changed from 11 to 8]
         delay(100);
         buzz(8, NOTE_B0, 1000 / 24); //[changed from 11 to 8]
         delay(100);
         buzz(8, NOTE_B0, 1000 / 24); //[changed from 11 to 8]
      }
   }
}

void showNumber(int number)
{
  lcd.clear();
  lcd.setCursor(4,0);
  lcd.print("-- "); 
  lcd.setCursor(7,0);
  lcd.print(number);
  lcd.print(" --");
}

void toggleFreePlay()
{
   // scroll between words without blocking
   unsigned long currentMillis = millis(); // current time
   if (currentMillis - previousMillis1 >= interval1)
   {
      previousMillis1 = currentMillis;
      if (displayStatus == 1)
         showPlay();
      else
         showFree();
   }
}

void showPlay()
{
   // write "PLAY" to the display
   lcd.clear();
   lcd.setCursor(6,0);
   lcd.print("PLAY");
   displayStatus = 2;
}

void showFree()
{
   // write "Free" to the display
   lcd.clear();
   lcd.setCursor(6,1);
   lcd.print("FREE");
   displayStatus = 1;
}

void buzz(int targetPin, long frequency, long length)
{
   /* Buzzer example function by Rob Faludi
      http://www.faludi.com
      https://gist.github.com/AnthonyDiGirolamo/1405180
   */
   long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
   //// 1 second's worth of microseconds, divided by the frequency, then split in half since
   //// there are two phases to each cycle
   long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
   //// multiply frequency, which is really cycles per second, by the number of seconds to
   //// get the total number of cycles to produce
   for (long i = 0; i < numCycles; i++) // for the calculated length of time...
   {
      digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphragm
      delayMicroseconds(delayValue); // wait for the calculated delay value
      digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphragm
      delayMicroseconds(delayValue); // wait again for the calculated delay value
   }
}

void sing()
{
   // play the song in a non blocking way
   unsigned long currentMillis = millis();

   if (currentMillis - previousMillis2 >= interval2)
   {
      previousMillis2 = currentMillis;
      int noteDuration = 1000 / tempo[songState];
      buzz(10, melody[songState], noteDuration);
      int pauseBetweenNotes = noteDuration;
      delay(pauseBetweenNotes);

      // stop the tone playing:
      buzz(10, 0, noteDuration);

      ++songState;
      // start song again if finished
      if (songState > 79)
      {
         songState = 14; // skip intro
      }
   }
}
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: groundFungus on Dec 16, 2018, 12:11 am
But without changing anything and uploading my test code the LCD works? 
If the bottom rails are getting power the backlight should light.
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: jjeffh on Dec 16, 2018, 12:22 am
After I set it up for the full Buzz Wire game and nothing happened (except Monty Python), then I left it all as you see it and uploaded your LCD code. That didn't work either. It would seem that I must be doing something wrong, but I sure can't see it.

Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: jjeffh on Dec 16, 2018, 10:20 pm
After I set it up for the full Buzz Wire game and nothing happened (except Monty Python), then I left it all as you see it and uploaded your LCD code. That didn't work either. It would seem that I must be doing something wrong, but I sure can't see it.

But without changing anything and uploading my test code the LCD works?  
If the bottom rails are getting power the backlight should light.
FYI, Charlie, I found my mistake. I set up just the LCD display on a different UNO and board, using your display code and it worked perfectly as I tested it with different placements of text. So I revisited the full Buzz Game set up and I found that I had connected LCD pin 16 to power instead of ground!

Now I see "Buzz Ready" and when I press the button I get the countdown from 20 seconds, resulting in a chirp from buzzer 2 when it reaches 0. When I manually change the variable "mode" to 1 in the code, then I get your flashing "Free" and "Play".

However, I'm still not getting a buzz when I touch the two leads together, so I'll investigate that set up.

- Jeff
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: jjeffh on Dec 16, 2018, 11:17 pm
I have not found another stupid mistake like the last one (yet), but there's something about the code I don't understand.

I see "pinMode(9, INPUT)" specified in the code, but I don't see any other reference to pin 9 in the code. Doesn't there have to be some place where the code says that if pin 9 is HIGH the buzzer should sound and the game should end? Or is that handled in a different way?

Pin 9 is connected to the "course" which is connected through two 220 resistors. The setup shows the lead to pin 9 goes to 1e on the board, with a 220 resistor in 1d. The resistor goes to 5d and another 220 resistor goes from 5e to 9e. The course is connected at 5a, and row 9 is grounded.

The the handle lead is connected directly to power. So if that circuit is completed, pin 9 should be HIGH and the buzzer should sound. I just can't see where in the code pin 9's input is read and the buzzer activated.
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: groundFungus on Dec 17, 2018, 12:38 am
Quote
The the handle lead is connected directly to power. So if that circuit is completed, pin 9 should be HIGH and the buzzer should sound. I just can't see where in the code pin 9's input is read and the buzzer activated.
Code: [Select]
   if (digitalRead(10) == HIGH)
   {
      delay(25);
      if (digitalRead(10) == HIGH)
      {
         while (digitalRead(10) == HIGH)
         {
            buzz(8, NOTE_B0, 1000 / 24); //[This was changed from 11 to 8]
         }
      }
   }

It looks to me like the 10 in that part of the code should be 9.  Why would one read the buzzer1 output?
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: jjeffh on Dec 17, 2018, 04:22 am
You got it! That worked.

So that's the whole project: music, display, two modes, and the wire-touch buzz. Now I just have to cut the pieces for the game board and lay out a suitable procedure that Chance and I can work on together to built this thing.

Thanks so much, Charlie! I've learned quite a bit from you in this thread. I've been looking at the Tutorials Point C++ training online. Perhaps as I go through their tutorials I can work on this code to test things out.

A few things I'd like to do are: 1) start and stop, or at least lower the volume of, the music; 2) stop the game and display "Game Over" when the wand touches the course; and 3) change between Free Play and Challenge Mode on the board instead of in the code.

For now, I'm just delighted to see it all working and to know this Christmas plan is actually going to work.

I doubt we're allowed to add more than one karma point at a time, but I'm going to see if I can give you a couple extra. You've been great.

- Jeff
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: groundFungus on Dec 17, 2018, 04:50 am
Thank you.  Glad you got it working.  Hope that you and Chance have as enjoyment with this hobby as i have.

Merry Christmas to you and your's.

Charlie
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: jjeffh on Dec 17, 2018, 01:24 pm
Thank you.  Glad you got it working.  Hope that you and Chance have as enjoyment with this hobby as i have.

Merry Christmas to you and your's.

Charlie
Merry Christmas to you, too.

- Jeff
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: n_nip on May 24, 2019, 06:33 am
Hi did you end up getting the circuit to work? I'm trying to make the same one but can't seem to get the buzzer to buzz when the handle touches the circuit
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: jjeffh on May 24, 2019, 05:52 pm
Well this is a blast from the past! It feels like long time since I was doing that project. Which is to say, memory fades.

I seem to remember there was an error in Coburn's published code regarding the buzzers. (Actually, there were a few errors and omissions which groundFungus graciously corrected and created for me.) I had thought the wire buzz and the Monty Python theme both sounded on both buzzers, but I think it turned out we had to separate them. Check out posts 31, 32, and 33 above.

I'll post what think was the code I ended up with in two subsequent posts (the posting limit is 9,000 characters and with the comments, the code is 9,600).

In the end, there were 3 button options:
1. The board's reset button restarts the game and displays, "BUZZ PLAY" and after a delay begins the music and displays "Press button for timed play;
2. The switch across rows 28-30 starts the timer;
3. A new switch added across rows 23-25 stops the music.

That last one is important! The Monty Python music is some real nice coding and gives the game a carnival feel, but it'll drive you crazy on that short loop.

In the final product, touching the wire with the wand beeps, stops the game, and displays, "Game Over! RESET to play again"

Good luck!!

Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: jjeffh on May 24, 2019, 06:08 pm
Here's a link to pictures of the finished game: Buzz Game (https://photos.app.goo.gl/FBmExU3QutgUP8fb8)
And here's the first half of the code:

Code: [Select]
//PART 1 OF 2 PART POSTING
/*This version incorporates Charlie's solution
*for manually starting and stopping the music
*with the playMontyFlag.
*It also in the quit flag for ending the game
*as soon as the circuit on pin 9 is completed
*(i.e. when the handle touches the wire).
*/

#include <hd44780.h>
#include <hd44780ioClass/hd44780_pinIO.h> // Arduino pin i/o class header
#include "pitches.h" // include pitches

//Setting LCD pins 4, 6, 11, 12, 13, and 14
//Which correspond to UNO pins 4>11, 6>12, 11>4, 12>5, 13>6, 14>7
const int rs = 11, en = 12, db4 = 4, db5 = 5, db6 = 6, db7 = 7; // for all other devices (don't know what devices this refers to -jh)

//==== Charlie's code ====
//pins 3 and 13 are available
const int  buttonPin = 13;    // the pin that the pushbutton is attached to
const int ledPin = 3;       // the pin that the (built-in) LED is attached to

// Variables will change:
bool buttonState = 0;         // current state of the button
bool lastButtonState = 0;     // previous state of the button
bool playMontyFlag = true;  // initial value of play the tune flag
//^^^^ Charlie's code ^^^^

hd44780_pinIO lcd(rs, en, db4, db5, db6, db7);

// LCD geometry
const int LCD_COLS = 16;
const int LCD_ROWS = 2;

// music: Monty Python theme
int songState = 0;

int melody[] =
{
   NOTE_F4, NOTE_E4, NOTE_D4, NOTE_CS4,
   NOTE_C4, NOTE_B3, NOTE_AS3, NOTE_A3,
   NOTE_G3, NOTE_A3, NOTE_AS3, NOTE_A3,
   NOTE_G3, NOTE_C4, 0,

   NOTE_C4, NOTE_A3, NOTE_A3, NOTE_A3,
   NOTE_GS3, NOTE_A3, NOTE_F4, NOTE_C4,
   NOTE_C4, NOTE_A3, NOTE_AS3, NOTE_AS3,
   NOTE_AS3, NOTE_C4, NOTE_D4, 0,

   NOTE_AS3, NOTE_G3, NOTE_G3, NOTE_G3,
   NOTE_FS3, NOTE_G3, NOTE_E4, NOTE_D4,
   NOTE_D4, NOTE_AS3, NOTE_A3, NOTE_A3,
   NOTE_A3, NOTE_AS3, NOTE_C4, 0,

   NOTE_C4, NOTE_A3, NOTE_A3, NOTE_A3,
   NOTE_GS3, NOTE_A3, NOTE_A4, NOTE_F4,
   NOTE_F4, NOTE_C4, NOTE_B3, NOTE_G4,
   NOTE_G4, NOTE_G4, NOTE_G4, 0,

   NOTE_G4, NOTE_E4, NOTE_G4, NOTE_G4,
   NOTE_FS4, NOTE_G4, NOTE_D4, NOTE_G4,
   NOTE_G4, NOTE_FS4, NOTE_G4, NOTE_C4,
   NOTE_B3, NOTE_C4, NOTE_B3, NOTE_C4, 0
};

int tempo[] =
{
   8, 16, 8, 16,
   8, 16, 8, 16,
   16, 16, 16, 8,
   16, 8, 3,

   12, 16, 16, 16,
   8, 16, 8, 16,
   8, 16, 8, 16,
   8, 16, 4, 12,

   12, 16, 16, 16,
   8, 16, 8, 16,
   8, 16, 8, 16,
   8, 16, 4, 12,

   12, 16, 16, 16,
   8, 16, 8, 16,
   8, 16, 8, 16,
   8, 16, 4, 16,

   12, 17, 17, 17,
   8, 12, 17, 17,
   17, 8, 16, 8,
   16, 8, 16, 8, 1
};

// non blocking setup (what is "non blocking"? -jh)
// free play
unsigned long previousMillis1 = 0; // time words last changed
const long interval1 = 1500; // interval between changing

// music
unsigned long previousMillis2 = 0; // time last changed
const long interval2 = 100; // interval between notes

int displayStatus = 0; // keep track of what's displayed
int mode = 0; // keep track of game mode: change to 0 (challenge) or 1 (free play) for different modes

bool countdown = false;

unsigned long previousMillis3 = 0; // time last changed (used in showCountdown)
const long interval3 = 1000; // interval between countdown (1 second)
int count = 21; // challenge mode timer

//Exit flag
bool quit = false;

void setup()
{
   //==== Charlie's code ====
   // initialize the button pin as an input with internal pullup enabled
   pinMode(buttonPin, INPUT_PULLUP);
   // initialize the LED as an output:
   pinMode(ledPin, OUTPUT);
   // initialize serial communication:
   Serial.begin(9600);
   //^^^^ Charlie's code ^^^^
  
  
   // setup code runs once:
   pinMode(9, INPUT); // setup circuit
   pinMode(10, OUTPUT); // setup buzzer 1
   pinMode(8, OUTPUT); // setup buzzer 2
   pinMode(2, INPUT); // setup button

   lcd.begin(LCD_COLS, LCD_ROWS);
   lcd.print("   BUZZ READY");
   delay(5000);
   lcd.clear();
   lcd.setCursor(2,0);
   lcd.print("Press button");
   lcd.setCursor(1,1);
   lcd.print("for timed play");
}

void loop()
{
  //==== Charlie's code ====
  // read the pushbutton input pin:
   buttonState = digitalRead(buttonPin);
   // compare the buttonState to its previous state
   if (buttonState != lastButtonState)
   {
      if (buttonState == LOW)
      {
         // if the current state is LOW then the button
         // went from not pressed to pressed:        
         digitalWrite(ledPin, !digitalRead(ledPin));
         // toggle a flag to run the sing function or not
         playMontyFlag = !playMontyFlag;
         if(playMontyFlag == true)
         {
          Serial.println("playing Monty Python tune");
         }
         else
         {
          Serial.println("Blessed Silence");
         }
      }
      lastButtonState = buttonState;    
   }
  //^^^^ Charlie's code ^^^^
 //END OF PART 1 POSTING


(I can only post again after five minutes...)
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: jjeffh on May 24, 2019, 06:18 pm
I'm going to attempt to upload my Fritz diagram as a jpg. I seem to remember attachments are tricky on this forum, so I may not be able to. Unfortunately, this diagram doesn't include the button that turns off the music, but other than that I think it is up to date.

And here's part 2 of the code:

Code: [Select]
//START OF PART 2 OF 2 POSTINGS
  /*============== START of TIMER BUTTON PRESS ===================*/
  // Change mode manually here, in the code. 0 = challenge; 1 = free play
  // NOTE: Challenge mode is Free Play until the button is pressed to start countdown
   if (mode == 0)
   {
      // challenge mode
      if (digitalRead(2) == HIGH) //Pin 2 is the button
      {
         delay(25); //Presumably there needs to be a 25ms delay to ensure the button was pressed
         if (digitalRead(2) == HIGH)
         {
            countdown = true; // start the countdown
         }
         else
         {
            countdown = false; // stop the countdown
         }
      }
      if (countdown)
      {
         showCountdown(); // advance countdown
      }
   }
   else
   {
      // free play
      toggleFreePlay();
   }
  /*============== END of TIMER BUTTON PRESS ===================*/

  //Pin 9 is the course-handle circuit. When 9 is activated "Game Over" is displayed and game ends.
  //Press the white reset button on the UNO board to reload and play again.
   if (digitalRead(9) == HIGH)
   {
      delay(25);
      if (digitalRead(9) == HIGH)
      {
         quit = true; //When the course circuit is complete the quit flag is set.
         while (digitalRead(9) == HIGH)
         {
            buzz(8, NOTE_B0, 1000 / 24);
           
            lcd.clear();
            lcd.setCursor(0,0);
            lcd.print("Game Over! RESET");
            lcd.setCursor(0,1);
            lcd.print("to play again");
         }
         // Regarding exiting the loop, see Zoul007's post here: https://stackoverflow.com/questions/23096366/how-to-stop-a-loop-arduino
         if (quit == true){exit(0);} //When the course circuit is broken again, the game ends
      }
   }
   else
   {
      if (playMontyFlag == true)
      {
       sing();
      }
    }
} // END OF 'VOID LOOP'

void showCountdown()
{
   // countdown the time remaining
   unsigned long currentMillis = millis(); // current time
   if (currentMillis - previousMillis3 >= interval3)
   {
      previousMillis3 = currentMillis;
      --count;
      showNumber(count);
      if (count == 0)
      {
         // game over
         quit = true; //TEST
         countdown = false;
         // reset countdown
         count = 21;
         // buzz 3 times
         buzz(8, NOTE_B0, 1000 / 24); //[changed from 11 to 8]
         delay(100);
         buzz(8, NOTE_B0, 1000 / 24); //[changed from 11 to 8]
         delay(100);
         buzz(8, NOTE_B0, 1000 / 24); //[changed from 11 to 8]
         lcd.clear();
         lcd.setCursor(0,0);
         /*lcd.print("Handle down");
         lcd.setCursor(0,1);
         lcd.print("Game Over!");*/

         lcd.print("Game Over! RESET");
         lcd.setCursor(0,1);
         lcd.print("to play again");

         if (quit == true){exit(0);} //TEST

      }
   }
}

void showNumber(int number)
{
  lcd.clear();
  lcd.setCursor(4,0);
  lcd.print("-- "); 
  lcd.setCursor(7,0);
  lcd.print(number);
  lcd.print(" --");
}

void toggleFreePlay()
{
   // scroll between words without blocking
   unsigned long currentMillis = millis(); // current time
   if (currentMillis - previousMillis1 >= interval1)
   {
      previousMillis1 = currentMillis;
      if (displayStatus == 1)
         showPlay();
      else
         showFree();
   }
}

void showPlay()
{
   // write "PLAY" to the display
   lcd.clear();
   lcd.setCursor(6,1);
   lcd.print("PLAY");
   displayStatus = 2;
}

void showFree()
{
   // write "Free" to the display
   lcd.clear();
   lcd.setCursor(6,0);
   lcd.print("FREE");
   displayStatus = 1;
}

void buzz(int targetPin, long frequency, long length)
{
   /* Buzzer example function by Rob Faludi
      http://www.faludi.com
      https://gist.github.com/AnthonyDiGirolamo/1405180
   */
   long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
   //// 1 second's worth of microseconds, divided by the frequency, then split in half since
   //// there are two phases to each cycle
   long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
   //// multiply frequency, which is really cycles per second, by the number of seconds to
   //// get the total number of cycles to produce
   for (long i = 0; i < numCycles; i++) // for the calculated length of time...
   {
      digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphragm
      delayMicroseconds(delayValue); // wait for the calculated delay value
      digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphragm
      delayMicroseconds(delayValue); // wait again for the calculated delay value
   }
}

void sing()
{
   // play the song in a non blocking way
   unsigned long currentMillis = millis();

   if (currentMillis - previousMillis2 >= interval2)
   {
      previousMillis2 = currentMillis;
      int noteDuration = 1000 / tempo[songState];
      buzz(10, melody[songState], noteDuration);
      int pauseBetweenNotes = noteDuration;
      delay(pauseBetweenNotes);

      // stop the tone playing:
      buzz(10, 0, noteDuration);

      ++songState;
      // start song again if finished
      if (songState > 79)
      {
         songState = 14; // skip intro
      }
   }
}
//END OF PART 2 POSTING
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: groundFungus on May 24, 2019, 06:22 pm
(https://forum.arduino.cc/index.php?action=dlattach;topic=584777.0;attach=309688)

Guide for posting images. (https://forum.arduino.cc/index.php?topic=364156.0)
Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: jjeffh on May 24, 2019, 06:37 pm
Hey Charlie, nice to hear from you!
In the meantime I went ahead and updated my Fritz. (Hope I get the Modify and Insert Image procedure right.) (...which I didn't, but no matter n_nip, you can download the jpg to see how the second button goes in. I don't know why it needed a 10k-ohm resistor, but it works.)

Anyway, I had wanted to get back to you to let you know Chance and I spent and lovely afternoon putting the project together and it works perfectly. I had set it up as a kit with all the base pieces and the Arduino components in little groups and he really took to it.

(http://BuzzGame_bb.jpg)

Title: Re: Can't get Coburn's code for Buzz Wire Game to compile
Post by: groundFungus on May 24, 2019, 07:57 pm
(https://forum.arduino.cc/index.php?action=dlattach;topic=584777.0;attach=309692)