47 lines
908 B
C++
47 lines
908 B
C++
#include <Servo.h>
|
|
|
|
Servo serv;
|
|
|
|
//Global variable
|
|
int pin = A0; //input Velostat pin
|
|
int pinServo = 9; //servo pin
|
|
|
|
float sensorVal;
|
|
float voltage;
|
|
float angle;
|
|
float weight;
|
|
|
|
//Calibration
|
|
//float scale = 54.54;
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
serv.attach(pinServo); //attach servo pin
|
|
|
|
pinMode(pin,INPUT_PULLUP);
|
|
digitalWrite(pin, HIGH);
|
|
}
|
|
|
|
void loop() {
|
|
sensorVal = analogRead(pin);
|
|
voltage = sensorVal * (5.0 / 1023.0); //Convert the sensor value to output voltage
|
|
|
|
angle = map(voltage, 0, 3.3, 0, 180);
|
|
//angle = voltage*scale; //calculating the angular value
|
|
|
|
weight = 3.304-voltage/0.436; //calculating weight using calibration curve equation
|
|
|
|
//Serial Printing
|
|
Serial.print(sensorVal);
|
|
Serial.print(" , ");
|
|
Serial.print(voltage);
|
|
Serial.print(" , ");
|
|
Serial.print(angle);
|
|
Serial.print(" , ");
|
|
Serial.println(weight);
|
|
|
|
|
|
serv.write(angle);
|
|
delay(500);
|
|
}
|