int switchValue, xValue, yValue; //declare variables
int scaledXValue;
void setup(){
pinMode(9,OUTPUT); //initialize the motorIn1 pin as output
pinMode(10,OUTPUT); //initialize the motorIn2 pin as output
pinMode(2, INPUT);
digitalWrite(2, HIGH);
Serial.begin(9600);
}
void loop(){
switchValue=digitalRead(2); //read the button
xValue=analogRead(A0); //read x-axis
yValue=analogRead(A1); //read y-axis
Serial.print("Switch: ");
Serial.println(switchValue);
Serial.print("X-axis: ");
Serial.println(xValue);
Serial.print("Y-axis: ");
Serial.println(yValue);
Serial.print("\n\n");
while(switchValue==0){
analogWrite(9,255); // Turn the motor on clockwise, full speed (255)
analogWrite(10,0); //
switchValue=digitalRead(2); //read the button
}
analogWrite(9,0); //Turn the motor off
analogWrite(10,0); //
if(xValue>600){
scaledXValue=map(xValue,600,1023,0,255);
analogWrite(9,scaledXValue); // Turn the motor on clockwise, speed scaledXValue
analogWrite(10,0);
}
else if(xValue<400){
scaledXValue=map(xValue,400,0,0,255);
analogWrite(9,0); // Turn the motor on clockwise, speed scaledXValue
analogWrite(10,scaledXValue);
}
}