pseudo code for a concept?

Guys I am inspired by looking at this video Speed Of Light - Ticket Redemption Exergame Arcade Game - BMIGaming.com - LAI Games - YouTube And I am planning to do the same as home project using 3 led's and 3 push buttons, can anyone please give me pseudo code for this project, I am intermediate at coding I guess?

Thanks

Additional info I am going to use 2 seven segment display for displaying scores

It's better if you go first and post what you have tried so far, even if it's pseudo code. (Better yet is for you try real code.) Also have a go at the circuit, and post a sketch you first take on that.

function loop

check current state
if waiting for start
  check input
  if true 
    next light
    change state to playing

if playing
  compare game countdown to zero
     if true 
       show score
       change state to waiting
       return

compare countdown to zero
    if true next light

   look for button
      compare with illuminated LED
      if true 
         Add score to total
          next light next
end of loop

function light next
   Pick a random LED
   Illuminate it 
   start countdown timer

Code so far// haven't included countdown timer so far

void setup()
{
// declaring 3 pins for LED output
// declaring 3 pins for pushbutton input respectively
}


void loop
{
int a=  digitalRead(5);
int b = digitalRead(6);
int c = digitalRead(7); //  5, 6, 7 pushbutton input pins

for(i =2; i<5; i++)   // 2, 3, 4 led output
{

digitalWrite(i, HIGH);
}

if(a == 1)
{
digitalWrite(2, LOW);
}

if( b == 1)
{
digitalWrite(3, LOW);
}

if(c == 1)
{
digitalWrite(4, LOW);
}

After this I am ignorant like how to glow the led again, and if the button not pressed it should go off in 4 seconds.

So you chose to ignore the concept of a separate function to illuminate the LED.

You may be interested in the approach I used in the Thread planning and implementing a program

It shows how pseudo code can evolve into real code

...R

So you chose to ignore the concept of a separate function to illuminate the LED.

No way, getting valuable guidance is the toughest task which I will never ignore, started to code based on your pseudo code let me get you the updates as soon as possible

You may be interested in the approach I used in the Thread planning and implementing a program

Went through your thread, it was really helpful, let me get back asap

based on the pseudo code and all other inputs, I have formulated the code for the game, Please ignore flaws in the code, please correct the code, it would be of great help.

/* code for 4 led's and 4 pushbuttons and doing the same stuff)
 
 
 AND STARTING WITH A CODE WITHOUT TIMER AND SCOREBOARD(i.e.displays) AFTERWARDS 
 LET ME EVOLVE IT, AT FIRST THIS CODE DEALS WITH ILLUMINATING THE LED'S AND LED'S GOES OFF AFTER YOU PRESS
 RESPECTIVE PUSHBUTTON, AND AT  NEXT INSTANCE, OTHER SET OF LED'S GOES ON. THIS CONTINUES UNTIL TIME IS OVER.*/


int led1 = 2;  // declaring pins for 4 led's 
int led2 = 3;
int led3 = 4;
int led4 = 5;
int pb1 = 6;  // declaring pins for push button(pb) as INPUTS, pb1 corresponds to led1
int pb2 = 7;  //pb2 corresponds to led2
int pb3 = 8;  //pb3 corresponds to led3
int pb4 = 9;  // pb4 corresponds to led4
int pbs  = 10;  // push button for starting the program
unsigned long timer;
unsigned long previoustimer = 0;

void setup()
{
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(pb1, INPUT);
  pinMode(pb2, INPUT);
  pinMode(pb3, INPUT);
  pinMode(pb4, INPUT);
  pinMode(pbs, INPUT);
  
}

void loop()
{
  int x = digitalRead(pbs);// CHECKING FOR PUSH BUTTON ON
  if(x == 1)
  {
    timer = millis();// STARTING THE TIMER
    ledillumination();// CALLING ILLUMATING LED FUNCTION
    checkpushbutton();// CALLING THE CHECK PUSH BUTTON FUNCTION
    if((timer - previoustimer)>=60000) //  IF 60 SECONDS IS OVER, END OF GAME
    {
      for(int i=2; i<6; i++)
      {
        digitalWrite(i, LOW);
      }
      
  }
}
}
  
  void ledillumination()
  {
    for(int i = 0; i<2; i++)
    {
      int a = random(2,6);// ILLUMINATING 2 LED'S AT A TIME RANDOMNLY AT THIS INSTANT
      digitalWrite(a, HIGH);
    }
  }
  
  void checkpushbutton()
  {
    
    int a1 = digitalRead(pb1); // DECLARING A SEPARATE VARIABLE FOR EACH PUSHBUTTON FOR EASY ACCESS
    int b1 = digitalRead(pb2);// DECLARING A SEPARATE VARIABLE FOR EACH PUSHBUTTON FOR EASY ACCESS
    int c1 = digitalRead(pb3);// DECLARING A SEPARATE VARIABLE FOR EACH PUSHBUTTON FOR EASY ACCESS
    int d1 = digitalRead(pb4);// DECLARING A SEPARATE VARIABLE FOR EACH PUSHBUTTON FOR EASY ACCESS
    if(a1 == 1) // if first pushbutton is high
    {
      digitalWrite(led1, LOW);
      nextsetledfn1(); //  calling the nextset led function to illuminate the other set of led's
     }
    if(b1 == 1)
    {
      digitalWrite(led2, LOW);
      nextsetledfn2();
    }
    if(c1 ==  1)
    {
      digitalWrite(led3, LOW);
      nextsetledfn3();
    }
    if(d1 ==  1)
    {
      digitalWrite(led4, LOW);
      nextsetledfn4();
    }
    
    if((timer - previoustimer)==3000)// one led can't be glowing for more than 3 seconds it has to go off, as per the game rules
    {
      digitalWrite(led1, LOW);
      digitalWrite(led2, LOW);
      digitalWrite(led3, LOW);
      digitalWrite(led4, LOW);
    }
  }
  
  void nextsetledfn1()
  {
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
  }
  
  void nextsetledfn2()
  {
    digitalWrite(led3, HIGH);
    digitalWrite(led4, HIGH);
  }
  
  void nextsetledfn3()
  {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
  }
  
  void nextsetledfn4()
  {
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
  }

Immediately before the game starts (probably the last thing in setup() ) you need to have previoustimer = millis(); in order to have base from which timer - previoustimer makes sense. This is because millis() starts incrementing when the power goes on and you have mo way of knowing what the value will have reached unless you save it.

If you use an array to hold the led pin numbers you can probably roll all the nextsetledfn1() functions into one. That way, with only one piece of code, there is less opportunity for silly mistakes.

...R

I'd probably modify it a bit, something like this

/* code for 4 led's and 4 pushbuttons and doing the same stuff)
 
 AND STARTING WITH A CODE WITHOUT TIMER AND SCOREBOARD(i.e.displays) AFTERWARDS 
 LET ME EVOLVE IT, AT FIRST THIS CODE DEALS WITH ILLUMINATING THE LED'S AND LED'S GOES OFF AFTER YOU PRESS
 RESPECTIVE PUSHBUTTON, AND AT  NEXT INSTANCE, OTHER SET OF LED'S GOES ON. THIS CONTINUES UNTIL TIME IS OVER.*/


int led[4] = {2,3,4,5}; //declaring pins for 4 LEDS
int pb[4] = {6, 7, 8,9}; // declaring pins for 4 push buttons
int startButton  = 10;  // push button for starting the program

unsigned long gameLength=30000;//games last 30 seconds
unsigned long timePerLed=2000;// allow 2 seconds per light
unsigned int maxPointsPerLed=100;
bool playing=false;
unsigned long gameStart;
unsigned long ledStart;
int currentLed;
unsigned long score;


void setup()
{
  for(int n=0;n<4;n++)
    {pinMode(led[n], OUTPUT);
     pinMode(pb[n], INPUT_PULLUP);
    }
pinMode(startButton, INPUT_PULLUP);
Serial.begin(9600);
Serial.print("Press start button to start!");

}

void loop()
{
unsigned long t=millis();

//if not playing start a new game when startButton is pressed
 if(!playing)
  {
    if (digitalRead(startButton)==LOW)
    {
     playing=true;
     //wait for button to be released
     while(digitalRead(startButton)==LOW);
     gameStart=t;
     score=0;
     nextLight();
     }
   return;
  }
//only continue on if we're in the midst of a game

//Check for end of game
if ( (t-gameStart) >= gameLength) 
   {endGame();
    return;
   }

//Check for timeout on current led
if( (t - ledStart) > timePerLed)
  {nextLight();
   return;
  }

//Check for appropriate button pressed for current led
if (digitalRead(led[currentLed]) == LOW )
  {//evaluate value of this hit based on time taken
  int LEDvalue= map (t - ledStart, 0, timePerLed, maxPointsPerLed,0);
  score = score + LEDvalue;
  nextLight(); 
  Serial.print("BANG + ");
  Serial.print (LEDvalue);
  Serial.println(" points");
  }

}

void nextLight()
{
//extinguish current LED  
digitalWrite (led[currentLed], LOW);
//pick another
currentLed=random(4);
//and light it.
digitalWrite(led[currentLed], HIGH);
ledStart=millis();  
}

void endGame()
{ 
Serial.println("Game OVER");
Serial.print("Score: ");
Serial.print(score,DEC);
Serial.println("Points");

if (score < 100)
  Serial.println("That's pathetic");
else
  Serial.println("A reasonable effort");

playing=false;
}

@KenF, In this part of your code

//Check for appropriate button pressed for current led
if (digitalRead(led[currentLed]) == LOW )
  {//evaluate value of this hit based on time taken
  int LEDvalue= map (t - ledStart, 0, timePerLed, maxPointsPerLed,0);
  score = score + LEDvalue;
  nextLight(); 
  Serial.print("BANG + ");
  Serial.print (LEDvalue);
  Serial.println(" points");
  }

first line if(digitalRead(led[currentLed]) == LOW) should be if(digitalRead(pb[currentled]) == LOW), am i right? since you have said in comment that

Check for appropriate button pressed for current led

, I suppose it might be pushbutton read, and also because all led's are declared as output, Please don mistake if I have quoted something wrong, Your help is essential.

Logeswari12:
@KenF, In this part of your code

//Check for appropriate button pressed for current led

if (digitalRead(led[currentLed]) == LOW )
  {//evaluate value of this hit based on time taken
  int LEDvalue= map (t - ledStart, 0, timePerLed, maxPointsPerLed,0);
  score = score + LEDvalue;
  nextLight();
  Serial.print("BANG + ");
  Serial.print (LEDvalue);
  Serial.println(" points");
  }





first line `if(digitalRead(led[currentLed]) == LOW)` should be if(digitalRead(pb[currentled]) == LOW), am i right? since you have said in comment that , I suppose it might be pushbutton read, and also because all led's are declared as output, Please don mistake if I have quoted something wrong, Your help is essential.

You're right.

I would have solved it the following way:

  • Represent each led/button pair with a state machine (called solPod in example). The state diagram for these machines is quite simple as shown.
  • Write the code using my State machine library. This permits putting state machine objects into arrays

The code then becomes fairly simple:

#include <SM.h>
const int pods = 4;//#led/btn pods
const int ledpin[pods] = {8, 9, 10, 11};// pins for leds in pods
const int btnpin[pods] = {3, 4, 5, 6};//pins for buttons in pods

int rnDelay[pods];
int maxPods = 2;//max number of active pods
int activePods;//currently active pods

int i;//must be global in order to be accessed from multiple SM

SM solPod[pods] = {SM(Init), SM(Init), SM(Init), SM(Init)};//separate state machine for each pod

void setup(){
  for(i = 0; i<pods; i++){
    pinMode(ledpin[i], OUTPUT);//initialize outputs
    rnDelay[i] = random(200, 1000);//initialie random delays
  }//for(i)
}//setup()

void loop(){
  for(i = 0; i<pods; i++) EXEC(solPod[i]);//run all pod state machines concurrently
}//loop()

State Init(){
  if(solPod[i].Statetime()>rnDelay[i]) solPod[i].Set(Check);//wait random amount of time
}//Init()

State Check(){
  if(activePods<maxPods){//check according to difficulty level
    digitalWrite(ledpin[i], 1);//led on
    solPod[i].Set(ledOn);//change state 
  }else{//max number of pod lit, go back and start over
    rnDelay[i] = random(200, 1000);//new delay
    solPod[i].Set(Init);//start over
  }//else
}//Check()

State ledOn(){
  if(digitalRead(btnpin[i])){//button pressed hen led is on
    reportScore(solPod[i].Statetime());//update score according to reaction time
    rnDelay[i]=random(200, 1000);//new delay
    solPod[i].Set(Init);
  }//if(button pressed)
}//ledOn()

void reportScore(unsigned long reactionTime){
  //some code to update score
}//reportScore()

speed of light pod state diagram.png

@KenF, at this part of your code

if(!playing)
  {
    if (digitalRead(startButton)==LOW)
    {
     playing=true;
     //wait for button to be released
     while(digitalRead(startButton)==LOW);

In the last line, we are checking the startbutton again for proceeding the game,May I know the reason?

and one more question while expression has semicolon at the end of the statement, what does it signify?

It's wiating for the user to get his finger off the button.

The semicolon marks the end of what to do while it's waiting (ie nothing, just wait).

One more thing, how make three led glow at a time and do the same action, I am doing it, but not able to get the output. used a for loop in nextLight function, but not sure how to execute it, In simple words how to make 3 led's glow at a time, and pressing appropriate buttons for switching it off. How to modify the code?

@KenF, one last help,

if( (t - ledStart) > timePerLed)
  {nextLight();
   return;
  }

In this part of your code, (t-ledStart), always returns us a value greater than timePerLed I suppose, Please correct me if I am wrong?

Logeswari12:
@KenF, one last help,

if( (t - ledStart) > timePerLed)

{nextLight();
  return;
  }




In this part of your code, (t-ledStart), always returns us a value greater than timePerLed I suppose, Please correct me if I am wrong?

ledStart gets set to whatever millis() returns as the led is turned on.
t holds the current value of millis()

So (t-ledStart) gives us the number of milliseconds that the current led has been on.
If timePerLed is smaller than this, then the whole expression becomes true.

@KenF, Actually I am trying to glow three led's at a time among 12 led and I have modified the code for it, please do correct me if I am wrong

int led[12] = {2,3,4,5,6,7,8,9,10,11,12,13}; //declaring pins for 12 LEDS
int pb[12] = {22,23,24,25,26,27,28,29,30,31,32,33}; // declaring pins for 12 ir
int coinboxsensor1  = 34;  // push button for starting the program
int coinboxsensor2 = 35;
int lcdons = 36;
int increment = 37;
int decrement = 38;
int reset = 39;

unsigned long gameLength=90000;//games last 30 seconds
unsigned long timePerLed=3000;// allow 2 seconds per light
unsigned int maxPointsPerLed=100;
boolean playing=false;
unsigned long gameStart;
unsigned long ledStart[3];
int currentLed[3];
unsigned long score;
unsigned long timerscore;
int i;
unsigned long timerscore1;

void setup()
{
  for(int n=0;n<12;n++)
    {
      pinMode(led[n], OUTPUT);
     pinMode(pb[n], INPUT);
    }
    pinMode(coinboxsensor1, INPUT);// two sensors for switching the system on 
    pinMode(coinboxsensor2, INPUT);
    pinMode(lcdons, OUTPUT);//  switching the seven segment display for score
    pinMode(increment, OUTPUT);//  incrementing the seven segment display by one
    pinMode(decrement, OUTPUT);//  decrementing the seven segment display
    pinMode(reset, OUTPUT);//  resetting the display
    
    Serial.begin(9600);
    Serial.print("Press start button to start!");

}

void loop()
{
unsigned long t=millis();

//if not playing start a new game when startButton is pressed
 if(playing == false)
  {
    int a= digitalRead(coinboxsensor1);
    int b = digitalRead(coinboxsensor2);
    
    if (a == 1 && b == 1)
    {
     playing=true;
     //wait for button to be released
     digitalWrite(lcdons, HIGH);
     delay(1000);
     digitalWrite(reset, HIGH);
     delay(1000);
     digitalWrite(reset, LOW);
     gameStart=t;
     score=0;
     startlight();
     }
   return;
  }
//only continue on if we're in the midst of a game

//Check for end of game
if ( (t-gameStart) >= gameLength) 

   {
     endGame();
     return;
   }

//Check for timeout on current led
for(i = 0; i<3; i++)
{
  if( (t - ledStart[i]) > timePerLed)
  {
    nextLight();
   return;
  }
}

for(i = 0; i<3; i++)
{
  if (digitalRead(pb[currentLed[i]]) == HIGH ) // check for appropriate ir sensor for the pressed led();
  {
    digitalWrite(increment, HIGH); // incrementing the score board
    timerscore = millis();
    
    //evaluate value of this hit based on time taken

  nextLight(); 
  //Serial.print("BANG + ");
  //Serial.print (LEDvalue);
  //Serial.println(" points");
  }
}

if(timerscore>=50)
{
  digitalWrite(increment, LOW);
}


}

void nextLight()
{
  
//extinguish current LED  
digitalWrite (led[currentLed[i]], LOW);
//pick another
currentLed[i]=random(12);
//and light it.
digitalWrite(led[currentLed[i]], HIGH);
ledStart[i]=millis();  

}


void endGame()
{
 for(int l = 0; l<3; l++)
{
  digitalWrite(lcdons, HIGH);
  delay(1000);
  digitalWrite(lcdons, LOW);
  delay(1000);
}

playing=false;
}

void startlight()
{
  for(i = 0; i<3; i++)
  {
    currentLed[i] = random(12);
    digitalWrite(led[currentLed[i]], HIGH);
    ledStart[i] = millis();
  }
}