Hey guys and girls,
I'm having some programming problems with getting my vibration motor and flex sensor to play along nicely.
I want the the vibration motor to vibrate when the flex sensor bends past a certain point. I can get readings from the sensor but I can't seem to get the program to make it vibrate depending on the readings.
HELP!
//flex sensor is connected to A0 and to 22kohm resistor which leads to ground
//vibration motor is connected to digital pin 7 and ground
int vib = 7;
void setup(){
Serial.begin(9600);
pinMode(vib, OUTPUT);
}
void loop(){
int vibrun = 0; //switch for turning on or off the vibration
if (vibrun = 0){
digitalWrite(vib, HIGH); // for some reason the voltage is reversed so forget the high and low swtich around.
}
else {
digitalWrite(vib, LOW);
}
int sensor, degrees;
// read the voltage from the voltage divider (sensor plus resistor)
sensor = analogRead(0);
// convert the voltage reading to inches
// the first two numbers are the sensor values for straight (768) and bent (853)
// the second two numbers are the degree readings we'll map that to (0 to 90 degrees)
degrees = map(sensor, 715, 549, 0, 90);
// note that the above numbers are ideal, your sensor's values will vary
// to improve the accuracy, run the program, note your sensor's analog values
// when it's straight and bent, and insert those values into the above function.
// print out the result
Serial.print("analog input: ");
Serial.print(sensor,DEC);
Serial.print(" degrees: ");
Serial.println(degrees,DEC);
// pause before taking the next reading
delay(500);
int flexy = analogRead(sensor);
if (flexy < 690){ //if the sensor reads a value less than 690 then the vibration motor will be on.
vibrun = 1;
}
}