# Flex sensor and Vibrate program.

**URL:** <https://forum.arduino.cc/t/flex-sensor-and-vibrate-program/197359>\
**Category:** Programming\
**Created:** [December 3, 2013, 11:57am UTC](https://forum.arduino.cc/t/flex-sensor-and-vibrate-program/197359 "2013-12-03T11:57:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 3, 2013, 11:57am UTC](https://forum.arduino.cc/t/flex-sensor-and-vibrate-program/197359/1 "2013-12-03T11:57:28Z")

</div>

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!

```cpp
//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; 
 }

}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 3, 2013, 12:04pm UTC](https://forum.arduino.cc/t/flex-sensor-and-vibrate-program/197359/2 "2013-12-03T12:04:55Z")

</div>

Well, that will never work, anyway:

```cpp
  sensor = analogRead(0);
...
  int flexy = analogRead(sensor);

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 3, 2013, 12:05pm UTC](https://forum.arduino.cc/t/flex-sensor-and-vibrate-program/197359/3 "2013-12-03T12:05:44Z")

</div>

```cpp
  if (vibrun = 0){

```

= != ==.

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

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [December 3, 2013, 12:07pm UTC](https://forum.arduino.cc/t/flex-sensor-and-vibrate-program/197359/4 "2013-12-03T12:07:32Z")

</div>

```cpp
  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 ?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 3, 2013, 12:31pm UTC](https://forum.arduino.cc/t/flex-sensor-and-vibrate-program/197359/5 "2013-12-03T12:31:32Z")

</div>

Gotcha.

It's working, thanks.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 8:26pm UTC](https://forum.arduino.cc/t/flex-sensor-and-vibrate-program/197359/6 "2021-05-05T20:26:19Z")

</div>

Closed on May 5, 2021, 8:26pm UTC


