This is a code for a bouncing ball on an 8X8 LED. How can i change...

90.2K

Verified Solution

Question

Electrical Engineering

This is a code for a bouncing ball on an 8X8 LED. How can ichange the code to make it a ping pong game against AI by adding 1potentionmeter to control it?

#include //this is a library that uses timer 1of the arduino to trigger interrupts in certain time intervals

//This defines a matrix defining a smiley face for the 8x8 LEDmatrix display
#define BALL { \
{1, 0, 0, 0, 0, 0, 0, 0}, \
{0, 0, 0, 0, 0, 0, 0, 0}, \
{0, 0, 0, 0, 0, 0, 0, 0}, \
{0, 0, 0, 0, 0, 0, 0, 0}, \
{0, 0, 0, 0, 0, 0, 0, 0}, \
{0, 0, 0, 0, 0, 0, 0, 0}, \
{0, 0, 0, 0, 0, 0, 0, 0}, \
{0, 0, 0, 0, 0, 0, 0, 0} \
}


/*Arduino pin --> Display pin----->8x8 matrix row and columncoordinates (1;1) origin
D2-------------->9---------------------->row 1
D3-------------->10-------------------->col 4
D4-------------->11-------------------->col 6
D5-------------->12--------------------->row 4
D6-------------->13-------------------->col 1
D7-------------->14--------------------->row 2
D8-------------->15-------------------->col 7
D9-------------->16-------------------->col 8
D10------------->4--------------------->col 3
D11------------->3--------------------->col 2
D12------------->2---------------------->row 7
D13------------->1---------------------->row 5
A0 (D14)-------->5---------------------->row 8
A1 (D15)-------->6--------------------->col 5
A2 (D16)-------->7---------------------->row 6
A3 (D17)-------->8---------------------->row 3

*/


//rows:
const int col[8] = {
2,7,17,5,13,16,12,14 };//these are the Arduino pins that connect tothe anodes (1-8) of the LEDs

//columns:
const int row[8] = {
6,11,10,3,15,4,8,9 };//these are the Arduino pins that connect tothe cathodes (1-8) of the LEDs

int x = 0;
int y = 0;
int dx = 1;
int dy = 1;

volatile byte c,r,flag,counter;//interrupt routine variables,they need to be specified as 'volatile'

// 2-dimensional array that contains the currently 'ON' LEDs inthe matrix ('1'='ON'); this is used in the refreshScreen() ISRbelow:
byte pattern[8][8] = BALL;

unsigned long previousMillis = 0; //last time we switchedpatterns [ms]
unsigned long interval = 1000; //time between switching [ms]
int currentPattern = 0; //0 for \"Ball\", 1 for \"Ball2\", 2 for\"Ball3\", 3 for \"Ball4\"

void setup() {
// initialize the row and column pins as outputs
// iterate through the pins:
for (int pin = 0; pin < 8; pin++) {
// initialize the output pins:
pinMode(col[pin], OUTPUT);
digitalWrite(col[pin], HIGH);
pinMode(row[pin], OUTPUT);
digitalWrite(row[pin], LOW);
// take the col pins (i.e. the cathodes) high and the row pins(anodes) low to ensure that
// the LEDS are off:   
}
Timer1.initialize(100); // initialize timer1, and set a 100 ussecond period for the interrupt interval (i.e. the ISR will becalled
//every 100 us - this seems to be a good frequency to achieve aflicker-free LED display.
//experiment with this parameter. If it gets too small the ISRstarts 'eating up' all the processor time, and the main loopbecomes very slow
Timer1.attachInterrupt(refreshScreen); // attaches therefreshScreen() function as 'Interrupt Service Routine' (ISR) tothe interrupt
//this means that every time 100 us have passed, therefreshScreen() routine will be called.
}

//main loop...here we can simply busy ourselves with changingthe pattern[][] array; nothing deals with the LED display.
//this is all handled via the ISR
void loop() {
if (x>=7)
{ dx = -1;
dy = random(-1,1);
}
if(x<=0)
{
dx = 1;
dy = random(-1,1);
}
if (y>=7)
{dy = -1;
dx = random(-1,1);
}
if (y<=0)
{ dy = 1;
dx = random(-1,1);
}
pattern[x][y] = 0;
pattern[x+dx][y+dy]=1;
x=x+dx;
y=y+dy;
delay(50);

Answer & Explanation Solved by verified expert
4.4 Ratings (989 Votes)
include LedControlh include Timerh define POTPIN A5 Potentiometer define PADSIZE 3 define BALLDELAY 200 define GAMEDELAY 10 define BOUNCEVERTICAL 1 define BOUNCEHORIZONTAL 1 define NEWGAMEANIMATIONSPEED 50 define HITNONE 0 define HITCENTER 1 define HITLEFT 2 define HITRIGHT 3 define DEBUG 1 byte sad B00000000 B01000100 B00010000 B00010000 B00000000 B00111000 B01000100 B00000000 byte smile B00000000 B01000100 B00010000 B00010000 B00010000 B01000100 B00111000 B00000000 Timer timer LedControl lc LedControl1211101 byte direction Wind rose 0 is north int xball int yball int    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students