-2

i am doing a remember game similar to Simon Game with Twincat structured text. And i have a problem that how would i check the buttons are pressed same as the LED on, and count how many buttons have been pressed in case you press it wrong? Thanks in advance. Here is my code:

VAR
iRandomNumber: DINT;
fb_delay : ton;
Run: BOOL;
GameSpeed: TIME:= T#2S;
io.blue: bool;
io.start: bool;
END_VAR

IF IO.Start THEN
run := TRUE;
IF iRandomNumber = 1 THEN
run := FALSE;    
io.Blue := TRUE;
fb_delay(in:=TRUE,PT := GameSpeed);
IF fb_delay.Q THEN
io.Blue:= FALSE;
run:= TRUE;
END_IF
END_IF
ELSIF iRandomNumber= 2 THEN
...    

I do create the random number with assign to 4 leds, here is the code for 1 blue led, the rest leds are the same. The number are 1,2,3,4 and the leds are blue, green, red, yellow. The RUN var is to activate the random number generator

Hoang Ho
  • 1
  • 1

1 Answers1

0

I would create an eNum for the colors, and then make an array of this eNum. Also I would have an index to point to the array last valid entry.

For each turn, a new color is generated and added to the array, so for each button pressed you have to check the array location and verify it's the same color up to last valid index. Then generate one new index and increase the pointer. You can play all again and verify buttons again.

You can speed up the game when pointing index grow.

Ilya Dan
  • 301
  • 1
  • 8