quoicoucode
This commit is contained in:
parent
25a724a84c
commit
05af884087
|
|
@ -0,0 +1,64 @@
|
|||
import serial
|
||||
import tkinter as tk
|
||||
|
||||
def map_analog_to_weight(analog_value):
|
||||
if analog_value >= 920:
|
||||
return 0
|
||||
elif analog_value >= 830:
|
||||
return 10
|
||||
elif analog_value >= 720:
|
||||
return 20
|
||||
elif analog_value >= 360:
|
||||
weight = (analog_value - 360) * (20 - 50) / (720 - 360) + 50
|
||||
return round(weight, 2)
|
||||
elif analog_value >= 280:
|
||||
weight = (analog_value - 280) * (50 - 100) / (360 - 280) + 100
|
||||
return round(weight, 2)
|
||||
elif analog_value >= 180:
|
||||
weight = (analog_value - 180) * (100 - 200) / (280 - 180) + 200
|
||||
return round(weight, 2)
|
||||
else:
|
||||
weight = (analog_value - 60) * (200 - 500) / (180 - 60) + 500
|
||||
return round(weight, 2)
|
||||
|
||||
# Configure the COM port settings
|
||||
com_port = 'COM12'
|
||||
baud_rate = 9600
|
||||
|
||||
# Open the serial connection
|
||||
ser = serial.Serial(com_port, baud_rate)
|
||||
|
||||
# Create the Tkinter window
|
||||
window = tk.Tk()
|
||||
window.title("Weight Display")
|
||||
window.geometry("200x100")
|
||||
|
||||
# Create a label to display the weight
|
||||
weight_label = tk.Label(window, text="Weight: -- g", font=("Arial", 16))
|
||||
weight_label.pack(pady=20)
|
||||
|
||||
def update_weight_label(weight):
|
||||
weight_label.config(text=f"Weight: {weight} g")
|
||||
|
||||
try:
|
||||
while True:
|
||||
# Read a line of data from the serial port
|
||||
line = ser.readline().decode().strip()
|
||||
|
||||
# Convert the received data to a numeric value
|
||||
try:
|
||||
analog_value = int(line)
|
||||
weight = map_analog_to_weight(analog_value)
|
||||
update_weight_label(weight)
|
||||
except ValueError:
|
||||
print("Invalid data received")
|
||||
|
||||
# Update the Tkinter window
|
||||
window.update()
|
||||
|
||||
except KeyboardInterrupt:
|
||||
# Close the serial connection when the program is interrupted
|
||||
ser.close()
|
||||
|
||||
# Close the Tkinter window
|
||||
window.destroy()
|
||||
|
|
@ -0,0 +1,80 @@
|
|||
#include <Servo.h>
|
||||
|
||||
Servo myservo; // create servo object to control a servo
|
||||
|
||||
const unsigned long interval = 10; // interrupt interval in milliseconds
|
||||
volatile bool interruptFlag = false; // flag to indicate interrupt occurrence
|
||||
|
||||
int potpin = A0; // analog pin used to connect the potentiometer
|
||||
const int numReadings = 5; // number of readings to average
|
||||
int readings[numReadings]; // array to store the readings
|
||||
int index = 0; // index for storing the readings
|
||||
int total = 0; // sum of the readings
|
||||
|
||||
// Function to map analog values to weight
|
||||
float map_analog_to_weight(int analog_value) {
|
||||
if (analog_value >= 920) {
|
||||
return 0;
|
||||
} else if (analog_value >= 830) {
|
||||
return 10;
|
||||
} else if (analog_value >= 720) {
|
||||
return 20;
|
||||
} else if (analog_value >= 360) {
|
||||
float weight = ((analog_value - 360) / (720.0 - 360.0)) * (50.0 - 20.0) + 20.0;
|
||||
return weight;
|
||||
} else if (analog_value >= 280) {
|
||||
float weight = ((analog_value - 280) / (360.0 - 280.0)) * (100.0 - 50.0) + 50.0;
|
||||
return weight;
|
||||
} else if (analog_value >= 180) {
|
||||
float weight = ((analog_value - 180) / (280.0 - 180.0)) * (200.0 - 100.0) + 100.0;
|
||||
return weight;
|
||||
} else {
|
||||
float weight = ((analog_value - 0) / (180.0 - 60.0)) * (500.0 - 200.0) + 200.0;
|
||||
return weight;
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
myservo.attach(9); // attaches the servo on pin 9 to the servo object
|
||||
Serial.begin(9600); // begin serial baud rate
|
||||
// Set up the timer for the interrupt
|
||||
TCCR2A = 0;
|
||||
TCCR2B = 0;
|
||||
TCNT2 = 0;
|
||||
OCR2A = 249; // for 16 MHz clock, 1 kHz interrupt
|
||||
TCCR2A |= (1 << WGM21);
|
||||
TCCR2B |= (1 << CS22) | (1 << CS21) | (1 << CS20); // prescaler = 1024
|
||||
TIMSK2 |= (1 << OCIE2A);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (interruptFlag) {
|
||||
interruptFlag = false; // reset the interrupt flag
|
||||
|
||||
int sensorValue = averageReading(); // Get the average of the readings
|
||||
|
||||
Serial.println(sensorValue); // Output the filtered sensor value
|
||||
|
||||
float weight = map_analog_to_weight(sensorValue); // map analog value to weight
|
||||
int mappedValue = map(weight, 0, 500, 180, 0); // scale weight to angle (value between 0 and 180)
|
||||
myservo.write(mappedValue); // sets the servo position according to the scaled value
|
||||
}
|
||||
|
||||
// Other code or tasks can be performed here without blocking the interrupt-based functionality
|
||||
}
|
||||
|
||||
// Interrupt Service Routine for Timer 2 Compare A
|
||||
ISR(TIMER2_COMPA_vect) {
|
||||
|
||||
interruptFlag = true; // set the interrupt flag
|
||||
}
|
||||
|
||||
// Function to calculate the average reading
|
||||
int averageReading() {
|
||||
total = total - readings[index]; // subtract the oldest reading
|
||||
readings[index] = analogRead(potpin); // read the new value
|
||||
total = total + readings[index]; // add the new reading
|
||||
index = (index + 1) % numReadings; // advance to the next index
|
||||
|
||||
return total / numReadings; // calculate the average
|
||||
}
|
||||
Loading…
Reference in New Issue