This circuit is designed to control a gear-reduced stepper motor based on inputs from a sound sensor and a potentiometer. The Arduino UNO serves as the central microcontroller unit, processing the sensor data and controlling the motor's speed and direction. The sound sensor detects ambient noise levels, while the potentiometer allows for manual control of the motor's speed. The stepper motor's speed is dynamically adjusted based on the sound sensor's input and the potentiometer's position.
#include <Stepper.h>
#include <Wire.h>
// Define the pins
const int soundSensorPin = A0; // Sound sensor connected to A0
const int potPin = A1; // Potentiometer connected to A1
const int stepsPerRevolution = 200;
// Change this to fit the number of steps per revolution for your motor
// Initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
float receivedFloat = 0; // For temperature
float riskFactor = 0;
const float decayRate = 0.95; // Decay rate for risk factor
const float maxSpeed = 90; // Max speed for the stepper motor
const float minSpeed = 20; // Minimum speed for the stepper motor
void setup()
{
Serial.begin(9600);
// Set the initial speed:
myStepper.setSpeed(maxSpeed);
Wire.begin(8); // Set the I2C address of this Arduino as 8
Wire.onReceive(receiveEvent); // Setup to handle incoming data
}
void loop()
{
// Rotate the stepper motor anticlockwise
myStepper.step(-stepsPerRevolution);
// Read the analog value from the sound sensor
int soundValue = analogRead(soundSensorPin);
// Read the potentiometer value
int potValue = analogRead(potPin); // Read the analog value from the potentiometer
// Compute risk factor based on sound and temperature
riskFactor = computeRiskFactor(soundValue, receivedFloat);
// Adjust stepper speed based on the risk factor and potentiometer
float mappedSpeed = map(potValue, 0, 1023, minSpeed, maxSpeed);
// If the risk factor is high, reduce the speed
if (riskFactor > 50) {
mappedSpeed = min(mappedSpeed, 60); // Limit to 60 if risk factor is high
}
myStepper.setSpeed(mappedSpeed);
// Print values for plotting
Serial.print("Sound Value: ");
Serial.println(soundValue); // Print the sound value
Serial.print("Temperature: ");
Serial.println(receivedFloat); // Print the received temperature
Serial.print("Potentiometer Value: ");
Serial.println(potValue); // Print the potentiometer value
Serial.print("Mapped Speed: ");
Serial.println(mappedSpeed); // Print the mapped speed
Serial.print("Risk Factor: ");
Serial.println(riskFactor); // Print the risk factor
// Apply decay to risk factor if it hasn't been updated
riskFactor *= decayRate;
}
// Function to handle incoming data
void receiveEvent(int howMany)
{
byte byteArray[sizeof(float)];
int i = 0;
while (Wire.available() && i < sizeof(float))
{
byteArray[i] = Wire.read(); // Read each byte
i++;
}
// Convert byte array back to float
memcpy(&receivedFloat, byteArray, sizeof(float));
}
// Function to compute risk factor
float computeRiskFactor(int soundValue, float temperature)
{
// Normalize values
float normalizedSound = map(soundValue, 0, 1023, 0, 100); // Assuming 0-1023 range for sound
float normalizedTemp = map(temperature, 0, 100, 0, 100); // Assuming 0-100°C for temperature
// Increase weight for sound
float weightedSound = normalizedSound * 0.9; // Weight for sound
float weightedTemp = normalizedTemp * 0.1; // Weight for temperature
// Calculate risk factor
return weightedSound + weightedTemp; // Combine factors
}
This code is designed to run on an Arduino UNO and controls a stepper motor based on the input from a sound sensor and a potentiometer. The setup()
function initializes the serial communication, stepper motor, and I2C communication. The loop()
function reads the sensor values, computes a risk factor, and adjusts the motor speed accordingly. The receiveEvent()
function handles incoming I2C data, and the computeRiskFactor()
function calculates a risk factor based on sound and temperature.