Pressure Counter

by Xiphoz in Circuits > Arduino

88 Views, 0 Favorites, 0 Comments

Pressure Counter

IMG_20230820_220959.jpg

This device measures the pressure put on a force sensor and shows that force on two seven-segment displays, on a scale from 0 to 99. A piezo buzzer plays a tone that increases in pitch with the force. If the maximum value of 99 is reached, the buzzer plays a short tune and a rainbow LED lights up to celebrate this feat.

The device can be used as a musical instrument by carefully varying pressure on the sensor. The displays help with practicing and getting the pitch right, however it is still quite challenging to play even short songs. The song that plays at max pressure can also be deceivingly difficult to achieve, as the force required is likely higher than you might expect! (This also makes it a good de-stresser after messing up a song)


My earlier attempt at this project was soldered on an Arduino Shield which could connect directly onto the Arduino Uno. Unfortunately, it became too complex because I kept wanting to add more components, which resulted in a short that I cannot fix.

Because of this experience, I decided to use 'point-to-point' soldering for this project. This means that I will connect each component directly with wires without the use of a shield or protoboard. This method makes it easier to connect and disconnect components, with the additional benefit of making it easier to fit inside of the box.

Supplies

Connection Diagram

connection diagram.PNG

First I made a diagram of how every component would be connected.

It looks intimidating, but this diagram shows the case of six displays when I only ended up using two. I used a library for the displays that used every digital pin of the Arduino (0-7 for each segment, 8-13 to select displays), so I decided to use the analog pins for the remaining three components. I will explain the code in detail next.

Coding

maincode.PNG
musiccode.PNG

Though I have manually written code for a seven-segment display before using Binary Coded Decimal and a decoder, this time I chose to use a library. This allowed me to easily write numbers to the displays and change the brightness.

The rest of the code is quite simple. I read the value from the force sensor, which returns a number between 0 and 1023. I divide this by 10 to scale it to be a number from 0 to 99 (I tried more exact approximations, but this made it too hard or impossible to reach the max value).

I use this value to set the pitch of the buzzer, which only activates from the threshold value of 5 to avoid activation from random noise. I also write the scaled pressure value directly to the displays.

Once the max value of 99 is reached, I turn on the LED and play a short tune that I hard coded with the tone and delay functions.

I used analogRead and analogWrite for the non-display components, since they are connected to analog pins.

Included are pictures of the full code, with the hard coded music separated for clarity.

Remember to include the 7 segment display library if you want to copy the code!

Raw Code:

void setup() {
 pinMode(pressurePin, INPUT);
 pinMode(buzzerPin, OUTPUT);
 pinMode(ledPin, OUTPUT);
 byte numDigits = 2;
 byte digitPins[] = {8, 9}; // D1
 byte segmentPins[] {0, 1, 2, 3, 4, 5, 6, 7}; // A, B, C, D, E, F, G, DP
 byte hardwareConfig = COMMON_CATHODE;
 Display.begin(hardwareConfig, numDigits, digitPins, segmentPins);
 Display.setBrightness(100);
}

void loop() {
 pressure = analogRead(pressurePin);
 number = pressure / 10;
 if (number > 5 && number < 99) {
  tone(buzzerPin, pressure, 10);
 }
 if (number >= 99) {
  number = 99;
  analogWrite(ledPin, 255);
  music();
  analogWrite(ledPin, 0);
 }
 Display.setNumber(number);
 Display.refreshDisplay();
}

void music() {
 tone(buzzerPin, 146, 150);
 delay(250);
 tone(buzzerPin, 146, 150);
 delay(250);
 tone(buzzerPin, 293, 150);
 delay(500);
 tone(buzzerPin, 220, 150);
 delay(750);
 tone(buzzerPin, 207, 150);
 delay(500);
 tone(buzzerPin, 196, 150);
 delay(500);
 tone(buzzerPin, 174, 150);
 delay(500);
 tone(buzzerPin, 146, 150);
 delay(250);
 tone(buzzerPin, 174, 150);
 delay(250);
 tone(buzzerPin, 196, 150);
}

Downloads

Soldering

IMG_20231030_023450.jpg

I soldered each component to a header, following the connection diagram. I made sure to strip the least I could from the cables, as open connections can cause shorts.

The picture shows the soldered connections of the two seven-segment displays.

The Box

IMG_20231030_023510.jpg

I chose a chest-like wooden box that hinges open at the top, so I could easily remove the Arduino during testing.

I made holes for the seven-segment displays, rainbow LED and power cable to fit through, as they need to be visible and accessible. The picture shows the final result of the box, everything fits neatly in the holes.

Assembly

IMG_20230820_220959.jpg

Finally, I fit everything neatly in the box and, once the USB cable is plugged in, everything works!

Once the program is written to the Arduino, it is saved until overwritten. This means that the device only needs power from a USB port to function, not a device with the code.

You can see the device in action (and a poor attempt at playing the Mario theme song) in this video.

This video shows the device inside of the box.