Flex Sensor Programming

Hi there! I do not understand the programming sentence for my project. Which is "int flex0to100 = map(flexSensorReading, 150, 400, 100, 0);" I do not understand what those values stands for.

I use the flex sensor to make it into a glove to produce musical chords. However, when I interface with the flex sensor, the number shown on the serial monitor when the flex sensor is flat is negative. Could you help me see if the connection is correct? (the pin with the square boxes is connected to a 27K ohm resistor than the pin with a thin line is connected to 5V in the arduino UNO pin. From the resistor, one connects to Analog Pin 0 and the other connects to ground.) Please help me with it.

robin0301

The schematic looks about right...

But the line of code will map the variable flexSensorReading into the variable flex0to100. The last will be 100 if the input is 150 and will be 0 is the input is 400.

But yeay, without the rest of the code we can't say anything more useful.

Hi,

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Where did you get the sketch from?

Thanks Tom..... :slight_smile:

You should see what sensor values you get from this sensor before mapping it.
Try this.

int sensorValue;

void setup() {
  Serial.begin(9600);
}

void loop() {
  sensorValue = analogRead(A0);
  Serial.println(sensorValue);
  delay(500);
}

It would be safer to swap supply and ground. So 27k to supply, and sensor to ground.
That way it's not possible to short the 5volt supply accidently to ground.
"map(flexSensorReading, 150, 400, 100, 0)" then becomes "map(flexSensorReading, 400, 150, 100, 0)"
Value 400 and 150 might need adjusting once you know the true values of your sensor.
Leo..

int flexSensorPin = A0; // analog pin 0
int speaker = 8; // digital pin 8
const int chipSelect = 4;
#include <SD.h>
#include <SPI.h>
void setup() {
  Serial.begin(9600);
  
  pinMode (speaker, OUTPUT); // sets the digital pin as output
  pinMode (flexSensorPin, INPUT); //sets the analog pin as input
  pinMode(10, OUTPUT); 
  // setup SD-card
  Serial.print("Initializing SD card...");
  if (!SD.begin(4)) {
    Serial.println(" failed!");
    return;
  }
  Serial.println(" done.");
  // hi-speed SPI transfers
  SPI.setClockDivider(4);

  // 44100 Hz stereo => 88200 sample rate
  // 100 mSec of prebuffering.
}

void loop() {
 int flexSensorReading = analogRead (flexSensorPin);
 int thisPitch = map(flexSensorReading, 150, 400, 100, 0);
  
  
if ((flexSensorReading > -40)&&(flexSensorReading < -30))  // Small bend; play C note
 {
   digitalWrite(speaker,HIGH);
   File myFile = SD.open("C_Major_Chord_C_Note_.wav");
   
   
 }

else if ((flexSensorReading>0)&&(flexSensorReading<12)) // Medium bend; play E Note
 {
   digitalWrite(speaker,HIGH);
   File myFile = SD.open("C_Major_Chord_E_Note_.wav");
   
   
   while((flexSensorReading < 0)&&(flexSensorReading > -90)) {

   digitalWrite(speaker,LOW);
  }
 }
else if ((flexSensorReading>30)&&(flexSensorReading<40)) // Large bend; play G Note
 {
   digitalWrite(speaker,HIGH);
   File myFile = SD.open("C_Major_Chord_G_Note_.wav");
   
   
   while((flexSensorReading<30)&&(flexSensorReading>-90)) {

   digitalWrite(speaker,LOW);
   }
 } 

else
 {
   digitalWrite(speaker,LOW);
 }

}

TomGeorge:
Hi,

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Where did you get the sketch from?

Thanks Tom..... :slight_smile:

This is the program that I did for my project. Need troubleshooting though. The electronics that I use is are the Groove Speaker,flex sensor and the micro SD card. Even though the SD card can be initialized, there is still no sound produced when I interact with the flex sensor. As for the sketch, is I draw it myself one.

Wawa:
You should see what sensor values you get from this sensor before mapping it.
Try this.

int sensorValue;

void setup() {
  Serial.begin(9600);
}

void loop() {
  sensorValue = analogRead(A0);
  Serial.println(sensorValue);
  delay(500);
}



It would be safer to swap supply and ground. So 27k to supply, and sensor to ground.
That way it's not possible to short the 5volt supply accidently to ground.
"map(flexSensorReading, 150, 400, 100, 0)" then becomes "map(flexSensorReading, 400, 150, 100, 0)"
Value 400 and 150 might need adjusting once you know the true values of your sensor.
Leo..

Thanks! I will try that and I will update you the results! :slight_smile:

Wawa, I tried what you have suggested me already but the value keeps staying at 1023 when I interact with the flex sensor.

Then the ground connection is missing.

I think you have one of those long breadboards with interrupted side rails.
Leo..

Hi,
Good point Wawa.
See attached pics.

Do you have a DMM to measure some voltages for us?

I think a picture of your project so we can see all you wiring may be helpful, thanks.

Tom... :slight_smile:

Protoboards.jpg

Protoboards1.jpg

Ok I have already change a breadboard and when the sensor is in the flat position, the reading on the serial monitor is at around 474 but when I bend the sensor to the maximum, the reading is at around 750. When I bend the sensor to the maximum at the opposite direction, the reading is around 275. May I know what is the significance of that and how do I program in such a way that the requirements are being met?

int thisPitch = map(flexSensorReading, 150, 400, 100, 0);

This line changes those sensor values to 100-0 for the rest of the program.

Up to you how you want it to react.

int thisPitch = map(flexSensorReading, 275, 750, 100, 0);

or

int thisPitch = map(flexSensorReading, 250, 275, 100, 0);

or movement restricted.

int thisPitch = map(flexSensorReading, 300, 725, 100, 0);

Leo..

Hi,
With the map function, it does not limit.

If you input is outside the input values you supply, the output values will also be outside the outside values you supply.
From what I understand the map function just does some simple y=mx+c calculations, using solution to two point data.

I think you need to incorporate constrain function.

Tom.... :slight_smile:

Hi there! I am a student currently working on a project where I am creating a music glove which produces musical chords. The electronics that I use are Arduino UNO, Micro SD Module V1.0 from DFRobot.com, flex sensor, 27K ohms resistor and the groove speaker. I use the SD Card to store the sound file and when the flex sensor reach up till a certain reading, it should play the sound out. However, there is nothing coming out of the speaker even though the SD card can be initialized.

Could you please help me check is the code correct.

int flexSensorPin = A0; // analog pin 0
int speaker = 8; // digital pin 8
const int chipSelect = 4;
#include <SD.h>
#include <SPI.h>
void setup() {
  Serial.begin(9600);
  
  pinMode (8, OUTPUT); // sets the digital pin as output
  pinMode (flexSensorPin, INPUT); //sets the analog pin as input
  pinMode(10, OUTPUT); 
  // setup SD-card
  Serial.print("Initializing SD card...");
  if (!SD.begin(4)) {
    Serial.println(" failed!");
    return;
  }
  Serial.println(" done.");
  // hi-speed SPI transfers
  SPI.setClockDivider(4);
  // 44100 Hz stereo => 88200 sample rate
  // 100 mSec of prebuffering.
}

void loop() {
 int flexSensorReading = analogRead(flexSensorPin);
 int flex0to180 = map (flexSensorReading, 460, 760, 0, 180);
     flex0to180 =  constrain(flexSensorReading, 0, 180); 

if ((flexSensorReading>540)&&(flexSensorReading<550))  // Small bend; play C note
 {
   digitalWrite(8,HIGH);
   File myFile = SD.open("C Major Chord (C Note).wav");
   
   
 }

else if ((flexSensorReading>660)&&(flexSensorReading<690)) // Medium bend; play E Note
 {
   digitalWrite(8,HIGH);
   File myFile = SD.open("C Major Chord (C Note).wav");
   
   
   while(flexSensorReading<659){

   digitalWrite(8,LOW);
  }
 }
else if ((flexSensorReading>720)&&(flexSensorReading<750)) // Large bend; play G Note
 {
   digitalWrite(8,HIGH);
   File myFile = SD.open("C_C Major Chord (G Note).wav");
   
   
   while(flexSensorReading<719) {

   digitalWrite(8,LOW);
   }
 } 

else
 {
   digitalWrite(8,LOW);
 }

}

When the flex sensor is flat, the reading is around 474. When it is bent to the maximum, the reading is around 750. While bending the flex sensor at the opposite direction, the reading is around 275.

Also, could you please help me check if the connection is correct (It is on the attachment).

Thank you,
robin0301 :slight_smile:

Identical topics merged.