Coding Challenge for 3 mode game

rxhul

Active member
Dear all,

I am having some difficulties with writing a program which has 4 push to make switches and a pot as the input and 3 LEDs + buzzer as the output.

I am attaching a picture of my PCB below although I appreciate this may not be helpful so I will also attempt to explain the function.

It is a bit complex so if it needs explaining again I am of course happy to do so and apologise if it is confusing.

23695

1. So, the user turns the device on
2. The press PTM (push to make) number 1 to activate mode 1.
3. In mode 1, the 3 LEDs come on at a "slow" fixed intervals.
4. Each LED has a corresponding PTM (out of the three remaining ones).
5. When the LED comes on, the corresponding PTM must be hit.
6. When the PTM is hit, (goes high), then the LED goes off and the buzzer sounds. This happens every time in later modes too.
7. Then another LED comes at the same "slow" fixed intervals and the process repeats
8. The user then uses the potentiometer to shorten the rate at which the LEDs come on - i.e they come on faster.
9. They can then press PTM number 1 again to move to mode 2
10. In this mode the LEDs come on randomly/random intervals but otherwise this is the same as mode 1.
11. The user presses PTM 1 to mode to mode 3
12. In this mode, the PIC measures 20 seconds.
13. The user uses the pot to set a setting - EASY MED or HARD (for example)
14. Each setting has a corresponding number eg. 10 hits, 20 hits, 30 hits
15. The user have to try and hit the PTMs as many times as they can in 20 seconds (but cannot hit the same PTM every time).
16. If the user hits the required number in the time, the buzzer beeps a couple of times.
17. Pressing PTM 1 again goes back to mode 1.

I wanted to write a code for the above function in Picaxe editor for a Picaxe 14M2

Many many thanks in advance to everyone,

Best wishes,
 
Last edited:

hippy

Technical Support
Staff member
Sounds like a fun game. The best way to approach any project is incrementally; get something simple working first then tweak that to do what you actually want, make the timing dependent on the pot position, add more modes, etc.

Don't forget to save each version so you can easily start again from where you were, with something known to work, if you mess something up.

I would start with a simple 'press the main button to start', some time later a LED comes on, hit the corresponding button and that goes off, some other LED comes on, repeat ad infinitum.

Untested, and you will have to adjust the SYMBOL definitions -
Code:
#Picaxe 14M2
#No_Data

Symbol BUTTON_MODE   = pinC.0
Symbol BUTTON_1      = pinC.1
Symbol BUTTON_2      = pinC.2
Symbol BUTTON_3      = pinC.3

Symbol PUSHED        = 1 ; Active high when a button is pushed

Symbol LED1          = B.1
Symbol LED2          = B.2
Symbol LED3          = B.3

Symbol SOME_TIME     = 4000 ; 4 seconds

Symbol randomNumber  = w1 ; b3:b2
Symbol ledNumber     = b4
Symbol lastLedNumber = b5

WaitForMainButtonPush:
  Gosub AllLedsOff
  Do
    Random randomNumber
  Loop Until BUTTON_MODE = PUSHED

Game1:
  Do
    Pause SOME_TIME
    Gosub ChooseLedToLight
    Gosub LightTheLed 
    Gosub WaitUntilButtonPushed
    Gosub AllLedsOff
  Loop

AllLedsOff:
  Low LED1
  Low LED2
  Low LED3
  Return

ChooseLedToLight:
  lastledNumber = ledNumber
  Do
    Random randomNumber
    ledNumber = randomNumber // 3 + 1 ; 1 to 3
  Loop Until ledNUmber <> lastLedNumber
  Return

LightTheLed:
  Select Case ledNumber
    Case 1 : High LED1
    Case 2 : High LED2
    Case 3 : High LED3
  End Select
  Return

WaitUntilButtonPushed:
  Select Case ledNumber
    Case 1 : Do : Loop Until BUTTON_1 = PUSHED
    Case 2 : Do : Loop Until BUTTON_2 = PUSHED
    Case 3 : Do : Loop Until BUTTON_3 = PUSHED
  End Select
  Return
 

rxhul

Active member
Many thanks hippy, so as far as I see it, the above code works for stage 1 in what you call incremental coding. i.e for now this works for Game 1 and I still have to do Game 2 and 3...?

I will programme this onto my circuit tomorrow and try to make additions like you suggest, making sure to save.

Hopefully you won't mind me coming back to this thread if I run into trouble again!

Many thanks
 
Last edited:

rxhul

Active member
Also, sorry to bother you - what do you mean when you say I will have to adjust some of the symbol definitions?
 

hippy

Technical Support
Staff member
what do you mean when you say I will have to adjust some of the symbol definitions?
Basically you need to ensure the BUTTON definitions refer to the pins you have your buttons connected to and the LED definitions to the pins you have your LEDs connected to.
 

rxhul

Active member
Thanks hippy for the help earlier this month.

Due to COVID-19, my plans were disrupted, but now I can say that the initial program works as I want.

What is the next stage incrementally.

Also, I would like to change one function:

mode 4:
HANDHELD mode - only LED 2 can come, and it just comes on randomly. When the corresponding PTM is hit, the led goes off and the buzzer sounds.

Many thanks :)
 

hippy

Technical Support
Staff member
It might be best to post your code so far so whatever is suggested can be best integrated with that but in general you probably want something like -
Code:
Game4:
  Do
    Gosub AllLedsOff
    Gosub WaitForSomeRandomTime
    ledNumber = 2
    Gosub LightTheLed 
    Gosub WaitUntilButtonPushed
    Gosub SoundBuzzer
  Loop
 

rxhul

Active member
Symbol BUTTON_MODE = pinC.0
Symbol BUTTON_1 = pinC.1
Symbol BUTTON_2 = pinC.2
Symbol BUTTON_3 = pinC.3

Symbol PUSHED = 1 ; Active high when a button is pushed

Symbol LED1 = B.1
Symbol LED2 = B.2
Symbol LED3 = B.3

Symbol SOME_TIME = 4000 ; 4 seconds

Symbol randomNumber = w1 ; b3:b2
Symbol ledNumber = b4
Symbol lastLedNumber = b5

WaitForMainButtonPush:
Gosub AllLedsOff
Do
Random randomNumber
Loop Until BUTTON_MODE = PUSHED

Game1:
Do
Pause SOME_TIME
Gosub ChooseLedToLight
Gosub LightTheLed
Gosub WaitUntilButtonPushed
Gosub AllLedsOff
Loop

AllLedsOff:
Low LED1
Low LED2
Low LED3
Return

ChooseLedToLight:
lastledNumber = ledNumber
Do
Random randomNumber
ledNumber = randomNumber // 3 + 1 ; 1 to 3
Loop Until ledNUmber <> lastLedNumber
Return

LightTheLed:
Select Case ledNumber
Case 1 : High LED1
Case 2 : High LED2
Case 3 : High LED3
End Select
Return

WaitUntilButtonPushed:
Select Case ledNumber
Case 1 : Do : Loop Until BUTTON_1 = PUSHED
Case 2 : Do : Loop Until BUTTON_2 = PUSHED
Case 3 : Do : Loop Until BUTTON_3 = PUSHED
End Select
Return

This is my code up until now (The one you suggested - it worked a treat :) )
 

rxhul

Active member
I still need to:

incorporate my POT (see first post) and my buzzer into this code
and I need to do 8-17

8. The user then uses the potentiometer to shorten the rate at which the LEDs come on - i.e they come on faster.
9. They can then press PTM number 1 again to move to mode 2
10. In this mode the LEDs come on randomly/random intervals but otherwise this is the same as mode 1.
11. The user presses PTM 1 to mode to mode 3
12. In this mode, the PIC measures 20 seconds.
13. The user uses the pot to set a setting - EASY MED or HARD (for example)
14. Each setting has a corresponding number eg. 10 hits, 20 hits, 30 hits
15. The user have to try and hit the PTMs as many times as they can in 20 seconds (but cannot hit the same PTM every time).
16. If the user hits the required number in the time, the buzzer beeps a couple of times.
17. Pressing PTM 1 again goes back to mode 1.
 
Top