Portfolio. BHSAD Arduino Prototyping | L4, Harduino: Drawing light | Page 10
Ping Pong
void setup() {
Serial.begin(9600);
for(int i=0;i=speedball) {
posball=posball+dirball;
if(posball==7) {
res=2;
posball=7; dirball=-1;
millisres2=millis();
millisblinkres2=millis();
blinkres2=true;
}
else if(posball==-1) {
res=2;
posball=0;dirball=1;
millisres2=millis();
millisblinkres2=millis();
blinkres2=true;
}
setleds(posball);
millisgoball=millis();
speedball=map(analogRead(POT),
0, 1023, MAX_SPEEDBALL, MIN_
SPEEDBALL); }
}
if(res==2) {
if(millis()-millisblinkres2>=
TIME_BLINK_RES2) {
blinkres2=!blinkres2;
digitalWrite(pinleds[posball],
blinkres2);
millisblinkres2=millis();
}
if(millis()-millisres2>= TIME_
RES2)
{
digitalWrite(pinleds[posball],
HIGH);
res=1; }
} }
void setleds(int pos) {
for(int i=0;i<8;i++)
digitalWrite (pinleds[i], LOW);
digitalWrite (pinleds[pos], HIGH);
}
#define COUNT_BUTTONS 2
#define COUNT_LEDS 8
const int POT=0;
int valpot = 0;
int valscale=1000;
unsigned long alltimepressing=0;
int posball=0;
int dirball=1;
int pinbuttons[2]={2,3};
int lastbuttons[2]={0,0};
int currentbuttons[2]={0,0};
boolean pressing[2]={false,false};
int res=1;
unsigned long speedball=0;
unsigned long millisgoball=0;
#define MIN_SPEEDBALL 1000
#define MAX_SPEEDBALL 250
unsigned long millisres2=0;
#define TIME_RES2 2000
unsigned long millisblinkres2=0;
boolean blinkres2=true;
#define TIME_BLINK_RES2 200