Flex sensor and Vibrate program.

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.


//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(){
  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("   degrees: ");
  // pause before taking the next reading
  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; 


Well, that will never work, anyway:

  sensor = analogRead(0);
  int flexy = analogRead(sensor);
  if (vibrun = 0){

= != ==.

Why bother testing vibrun when you just set it to 0?

  int vibrun = 0; //switch for turning on or off the vibration
  if (vibrun = 0){

Oh dear. Set vibrun to 0 then attempt to test if is zero. Even if you got the test right what will the outcome always be I wonder ?


It's working, thanks.