detect state change with flex sensor and wave shield


I'm a new member, this is my first post, and I'm relatively new to arduino.

I'm working on a project which uses a wave shield controlled with a flex sensor. I've already managed to have different sounds play depending on which direction you bend the sensor and for a range of signal but I realized for my project that the sensor will only be bent in one direction and only one out of many sounds will play.

I've found a tutorial for having the arduino control an LED based on how many times a button is pressed

but I can't seem to find anything similar for an analog input such as the flex sensor. Although I've attempted to, I'm not familiar enough with arduino code to successfully translate the tutorial to work for an analog input (if in fact it's possible).

Is this possible? Could you point me in the right direction?


In the digital world, all you can do is detect a change between zero and one, or between one and zero.
In the analogue world, you can detect change from a lower number to a higher, or from a higher number to a lower one.
This is simple arithmetic.

Is this what you are trying to do?

You read the sensor with an analogue read and then take the results and use a set of if statements to see if it is between two values. Then when it is you play your note.
Here is a snippet of code to give you the idea of what to do-

sensorValue = analogRead(0);
if( sensorValue >= 100 && sensorVlaue < 200) { // put the call to play your note1 here
if( sensorValue >= 200 && sensorVlaue < 300) { // put the call to play your note2 here
if( sensorValue >= 300 && sensorVlaue < 400) { // put the call to play your note3 here
// and so on


Yes, that is what I'd like to do.

I'd like my arduino to detect a change in the analog signal coming from the flex sensor, count each time there is a change and play a different sound depending on what number it has counted.

Sorry if I wasn't clear :slight_smile:


Thanks but I've tried something similar to the code you posted.

My problem with using that code is that the flex sensor is in a squeezable toy which limits the range of values the arduino gets from it. Having the sounds triggered by such a small range makes it too unpredictable for my purposes which is why I'd like for the code to count the number of times it is squeezed.

So you have to set a threshold value and when the analogue reading is over that value you treat it as a press and under that you treat it as a releases.
Then you simply count the number of times it is pressed. Use a variable to record the last state of the button and only increase the count when the past state is different from the current state.

Thanks Grumpy_Mike that seems like a great solution. I’ll try it out and see how it goes. Thanks!