84 lines
1.7 KiB
C++
84 lines
1.7 KiB
C++
int xPin = A0;
|
|
int yPin = A1;
|
|
int buttonPin = 2;
|
|
|
|
int xPosition = 0;
|
|
int yPosition = 0;
|
|
int buttonState = 0;
|
|
|
|
void setup() {
|
|
// initialize serial communications at 9600 bps:
|
|
Serial.begin(9600);
|
|
|
|
pinMode(xPin, INPUT);
|
|
pinMode(yPin, INPUT);
|
|
|
|
pinMode(3, OUTPUT);
|
|
digitalWrite(3, LOW);
|
|
pinMode(4, OUTPUT);
|
|
digitalWrite(4, LOW);
|
|
pinMode(5, OUTPUT);
|
|
digitalWrite(5, LOW);
|
|
pinMode(6, OUTPUT);
|
|
digitalWrite(7, LOW);
|
|
pinMode(7, OUTPUT);
|
|
//activate pull-up resistor on the push-button pin
|
|
pinMode(buttonPin, INPUT_PULLUP);
|
|
|
|
// For versions prior to Arduino 1.0.1
|
|
// pinMode(buttonPin, INPUT);
|
|
// digitalWrite(buttonPin, HIGH);
|
|
|
|
}
|
|
|
|
void loop() {
|
|
xPosition = analogRead(xPin);
|
|
yPosition = analogRead(yPin);
|
|
buttonState = digitalRead(buttonPin);
|
|
|
|
//Serial.print("X: ");
|
|
//Serial.print(xPosition);
|
|
//Serial.print(" | Y: ");
|
|
//Serial.print(yPosition);
|
|
//Serial.print(" | Button: ");
|
|
//Serial.println(buttonState);
|
|
|
|
if (buttonState == 0) {
|
|
Serial.println("pressed");
|
|
digitalWrite(3,HIGH);
|
|
delay(100);
|
|
digitalWrite(3,LOW);
|
|
delay(500);
|
|
}
|
|
if (xPosition < 400) {
|
|
Serial.println("x <<< ");
|
|
digitalWrite(4,HIGH);
|
|
delay(100);
|
|
digitalWrite(4,LOW);
|
|
//delay(500);
|
|
}
|
|
if (xPosition > 600) {
|
|
Serial.println("x >>> ");
|
|
digitalWrite(5,HIGH);
|
|
delay(100);
|
|
digitalWrite(5,LOW);
|
|
//delay(500);
|
|
}
|
|
|
|
if (yPosition < 400) {
|
|
Serial.println("y <<< ");
|
|
digitalWrite(6,HIGH);
|
|
delay(100);
|
|
digitalWrite(6,LOW);
|
|
//delay(500);
|
|
}
|
|
if (yPosition > 600) {
|
|
Serial.println("y >>> ");
|
|
digitalWrite(7,HIGH);
|
|
delay(100);
|
|
digitalWrite(7,LOW);
|
|
//delay(500);
|
|
}
|
|
delay(50); // add some delay between reads
|
|
}
|