mouse control with analog inputs

a warm-up, control the mouse with analog inputs, two photoresistors,

The thing was that the should totally catch photoresistors with your fingers. It didn’t work when only put finger over the top or onto her. It worked when gently squeezed.

Mouse goes left mouse goes right, mouse goes left, mouse goes right, thanks God it does, it works.

IMG_0490 copy copy

 

 

int lastButtonState = LOW; // state of the button last time you checked
boolean mouseIsActive = false; // whether or not the Arduino is controlling the mouse

void setup() {
// initialize mouse control:
Mouse.begin();
// initialize serial communication:
Serial.begin(9600);
// make pin 2 an input:
pinMode(2, INPUT);
}

void loop() {
// read the pushbutton:
int buttonState = digitalRead(2);

// if it’s changed and it’s high, toggle the mouse state:
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
// if mouseIsActive is true, make it false;
// if it’s false, make it true:
mouseIsActive = !mouseIsActive;
}
}
// save button state for next comparison:
lastButtonState = buttonState;

// read the analog sensors:
int sensor1 = analogRead(A0);
delay(1);
int sensor2 = analogRead(A1);

// print their values. Remove this when you have things working:
Serial.print(sensor1);
Serial.print(” “);
Serial.println(sensor2);

// if there’s a significant difference to the right:
if (sensor1 > sensor2 + 100){
// Serial.println(“L”);
if (mouseIsActive == true) {
Mouse.move(-1, 0, 0);
}
}

// if there’s a significant difference to the left:
else if (sensor2 > sensor1 + 100) {
// Serial.println(“R”);
if (mouseIsActive == true) {
Mouse.move(1, 0, 0);
}
}
}

Advertisements

Comments are closed.