Ping Pong Arduino program

#1

So i was asked to make an Arduino program which simulates ping pong on your LEDs. Each LED should light up in succession, then the lit LED should “bounce back” in the opposite direction when the last LED is reached. This pattern should continue indefinitely.


int ledPins[] = {2,3,4,5,6,7,8,9};

void setup()
{
  int index;

  for(index = 0; index <= 7; index++)
  {
    pinMode(ledPins[index],OUTPUT);
    // ledPins[index] is replaced by the value in the array.
    // For example, ledPins[0] is 2
  }
}


void loop()
{
  
}

void oneAfterAnotherNoLoop()
{
  int delayTime = 100; // time (milliseconds) to pause between LEDs
                       // make this smaller for faster switching

  // turn all the LEDs on:

  digitalWrite(ledPins[0], HIGH);  //Turns on LED #0 (pin 2)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[1], HIGH);  //Turns on LED #1 (pin 3)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[2], HIGH);  //Turns on LED #2 (pin 4)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[3], HIGH);  //Turns on LED #3 (pin 5)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[4], HIGH);  //Turns on LED #4 (pin 6)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[5], HIGH);  //Turns on LED #5 (pin 7)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[6], HIGH);  //Turns on LED #6 (pin 8)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[7], HIGH);  //Turns on LED #7 (pin 9)
  delay(delayTime);                //wait delayTime milliseconds  

  // turn all the LEDs off:

  digitalWrite(ledPins[7], LOW);   //Turn off LED #7 (pin 9)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[6], LOW);   //Turn off LED #6 (pin 8)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[5], LOW);   //Turn off LED #5 (pin 7)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[4], LOW);   //Turn off LED #4 (pin 6)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[3], LOW);   //Turn off LED #3 (pin 5)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[2], LOW);   //Turn off LED #2 (pin 4)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[1], LOW);   //Turn off LED #1 (pin 3)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[0], LOW);   //Turn off LED #0 (pin 2)
  delay(delayTime);                //wait delayTime milliseconds  
}

void oneAfterAnotherLoop()
{
  int index;
  int delayTime = 100; // milliseconds to pause between LEDs
                       // make this smaller for faster switching

  // Turn all the LEDs on:

  // This for() loop will step index from 0 to 7
  // (putting "++" after a variable means add one to it)
  // and will then use digitalWrite() to turn that LED on.

  for(index = 0; index <= 7; index++)
  {
    digitalWrite(ledPins[index], HIGH);
    delay(delayTime);                
  }                                  

  // Turn all the LEDs off:

  // This for() loop will step index from 7 to 0
  // (putting "--" after a variable means subtract one from it)
  // and will then use digitalWrite() to turn that LED off.

  for(index = 7; index >= 0; index--)
  {
    digitalWrite(ledPins[index], LOW);
    delay(delayTime);
  }               
}

void oneOnAtATime()
{
  int index;
  int delayTime = 100; // milliseconds to pause between LEDs
                       // make this smaller for faster switching

  // step through the LEDs, from 0 to 7

  for(index = 0; index <= 7; index++)
  {
    digitalWrite(ledPins[index], HIGH);  // turn LED on
    delay(delayTime);                    // pause to slow down
    digitalWrite(ledPins[index], LOW);   // turn LED off
  }
}


/*
pingPong()

This function will step through the LEDs,
lighting one at at time in both directions.
*/

void pingPong()
{
  int index;
  int delayTime = 100; // milliseconds to pause between LEDs
                       // make this smaller for faster switching

  // step through the LEDs, from 0 to 7

  for(index = 0; index <= 7; index++)
  {
    digitalWrite(ledPins[index], HIGH);  // turn LED on
    delay(delayTime);                    // pause to slow down
    digitalWrite(ledPins[index], LOW);   // turn LED off
  }

  // step through the LEDs, from 7 to 0

  for(index = 7; index >= 0; index--)
  {
    digitalWrite(ledPins[index], HIGH);  // turn LED on
    delay(delayTime);                    // pause to slow down
    digitalWrite(ledPins[index], LOW);   // turn LED off
  }
}

  for(index = 0; index <= 3; index++) // Step from 0 to 3
  {
    digitalWrite(ledPins[index], HIGH);    // Turn a LED on
    digitalWrite(ledPins[index+4], HIGH);  // Skip four, and turn that LED on
    delay(delayTime);                      // Pause to slow down the sequence
    digitalWrite(ledPins[index], LOW);     // Turn the LED off
    digitalWrite(ledPins[index+4], LOW);   // Skip four, and turn that LED off
  }
}



void randomLED()
{
  int index;
  int delayTime;

  index = random(8);    // pick a random number between 0 and 7
  delayTime = 100;

  digitalWrite(ledPins[index], HIGH);  // turn LED on
  delay(delayTime);                    // pause to slow down
  digitalWrite(ledPins[index], LOW);   // turn LED off
}

so thats what i have but i’m not sure if it simulates a ping pong .

#2

just a reminder, this is the Processing forum,
so actually the wrong place for a ( pure ) arduino blinky program

also you posted your arduino program with a lot of code ( functions )
but you not talk about ANY problem, so , is there a question?

but looks like none of the functions are called, as

void loop() {}

looks very empty.
so possibly your program just does NOTHING?

also there seem to be redundant versions of the same job,
some using FOR loops ( these ones to keep )
and others, like:

oneAfterAnotherNoLoop()

what could be deleted ( was it a first test?)


even the best can not write a long code like that without any test first,
so i assume there is a problem with your working style.
you start with a 5 line blink version from the examples
upload and test it until it works,
and then start the next step using a more complex function.


2 Likes
#3

Hey there ! Basically here we are translating movement logic on a screen now into singular led’s if you have programmed before you should be able to see moving through each index of the array we can light up each led which will give us the simulation of movement. Each new led’s we light up we should turn off the previous ones so it gives us that good illusion of the light “moving” through each individual led.

What you also try to accomplish is something very ‘hacky’ remember you have a computer at your disposal, you should make it do all the heavy lifting rather than you code up every individual bit. They don’t complain that they have to do more. Use that do your advantage :smiley:

Here’s the code:

int ledPins[] = {2,3,4,5,6,7,8,9};
int index;
boolean left,right;
void setup(){
	for(int i = 0 ;i<lendPins.length; i++){
		pinMode( ledPins[i] , OUTPUT );//Register all led's
	}
	index = 0;
	left = false;
	right = true;
}


void loop(){
  digitalWrite( ledPins[index] , HIGH );
  if(index  >= ledPins.length - 1 ){
	right = false;//At the end of the right so start moving left
	left = true;
  }
  else if( index <= 0 ){
	left = false;//At the end of the left so move back right
	right = true;
  }
  delay(500);
  notOnIt(index);
  
  if(right) index++;
  else if(left)  index--;
}
void notOnIt( int index ){
	for(int i = 0 ; i<ledPins.length ; i++ ){
		if( index != i ) digitalWrite( ledPins[i] , LOW );//To turn off the leds which aren't supposed to be on
	}
}

Play around with the delay( ) function as much as you want to suit the needs of what feels right for your movement pattern through each led. Let me know if you got any further questions!

2 Likes
#4

Hey there!
Just a note to add up, Arduino IDE is C++ based ( I believe ) so therefore there’s no nifty .length to get the size of an array. But to get the same outcome we can get the size of the array in bytes through sizeof() and divide it by sizeof(int) this will give us an integer which is the size of the array.

Updated code:

int ledPins[] = {2,3,4,5,6,7,8,9};
int arSize;
int index;
boolean left,right;
void setup(){
	arSize = sizeof(ledPins)/ sizeof(int);
	for(int i = 0 ;i<arSize; i++){
		pinMode( ledPins[i] , OUTPUT );//Register all led's
	}
	index = 0;
	left = false;
	right = true;
}


void loop(){
  digitalWrite( ledPins[index] , HIGH );
  if(index  >= arSize - 1 ){
	right = false;//At the end of the right so start moving left
	left = true;
  }
  else if( index <= 0 ){
	left = false;//At the end of the left so move back right
	right = true;
  }
  delay(500);
  notOnIt(index);
  
  if(right) index++;
  else if(left)  index--;
}
void notOnIt( int index ){
	for(int i = 0 ; i<arSize ; i++ ){
		if( index != i ) digitalWrite( ledPins[i] , LOW );//To turn off the leds which aren't supposed to be on
	}
}

I gave a with this to my Arduino with a bunch of led’s worked all fine. Now it is up for your circuitry to make sure this behaves properly. Best of luck!

2 Likes