Weighing-scale arduino code

This commit is contained in:
Maryne DEY 2023-05-19 22:44:44 +02:00
parent f2319a42f5
commit 7ae8cf0aa7
1 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1,71 @@
// Task: Signal Conditioning and Weight Calculation Code for Vintage-Style Weighing Scale.
// Input:
// - Analog sensor readings from the Velostat-based transducer.
// Output:
// - Servo motor control signals for positioning the vintage-style display,
// - Weight values displayed through the Arduino Serial Monitor.
// Author: Maryne Dey and Alexandre Verot (maryne.dey@ecam.fr and alexandre.verot@ecam.fr)
// Created: 2023-05-15
#include <Servo.h>
#include <math.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
float val; // variable to read the value from the analog pin
float meanValue;
float val2; // variable to read the value from the analog pin
float meanValue2;
int i = 0;
int n = 10;
float array[10];
int voltage;
double weight;
void setup() {
myservo.attach(3); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
}
void loop() {
//filter using the average of the last n values
while (i < n){
val = analogRead(potpin);
array[i] = val;
i++;
}
int sum = 0;
for (int j = 0; j < n; j++) {
sum += array[j]; // Add each element to the sum
}
meanValue = sum/n;
//send the values to the servo motor
meanValue = map(meanValue, 0, 620, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(meanValue); // sets the servo position according to the scaled value
//filter using the average of the last n values
while (i < n){
val2 = analogRead(potpin);
array[i] = val;
i++;
}
int sum2 = 0;
for (int k = 0; k < n; k++) {
sum2 += array[k]; // Add each element to the sum
}
meanValue2 = sum2/n;
//display the weight
meanValue2 = map(meanValue2, 0, 1023, 0, 500); //output is in centi volts to be able to reach decimal values
weight = -83.734*pow(meanValue2/100,3) + 578.89*pow(meanValue2/100,2) - 1353.2*meanValue2/100 + 1100.2;
Serial.println(weight);
//delay to clearly see the weight and avoid oscillating
delay(500);
i=0;
}