Hello and thanks for reading.
This is what I've made so far:
A motor that spins according to datas from an ultrasonic sensor. The further an object is, the slower the motor rotates, the closer it gets the faster it spins. It also has a potentiometer to regulate how far the sensor "sees", meaning turning it will will ignore objects after a certain distance.
The problem is:
I'm trying to add a second potentiometer that will let me regulate the speed at which the motor is turning --> the motor will vibrate (as i've added an unbalanced weight) and the goal is to be able to decide how intense that vibration should be.
so, SENSOR will increase how fast the motor rotates (and vibrates) but i would also need the POTENTIOMETER to have some kind of control over the intensity at which the motor rotates.
P.S,: The ultrasound sensor signal range is 0- 19000 (0- 400 cm)
The potentiometer signal range is 0 - 674
Here's the code, thanks if you can help me out!!!
//Sensor info 100-19000 sensor data min-max
const int trigPin = 10;
const int echoPin = 11;
long duration;
int distanceCm, distanceInch;
// Motor A connections
int enA = 9;
int in1 = 8;
int in2 = 7;
void setup() {
// Set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
// pinmode of SCH-04 sensor
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Turn off motors - Initial state
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
//initialize communication with console
Serial.begin(9600);
delay(100);
}
void loop() {
speedControl();
delay(10);
//read DISTANCE from Sensor
delay(100); //delay new_cycle lettura sensore
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); //Temporizza segnale echoPin - ms
distanceCm = duration*0.034/2; //Calcolo distanza in CM
distanceInch = duration*0.0133/2; //Calcolo distanza in INCHES
}
//Controllo velocità motori in base a "distanceCm"
void speedControl() {
// Turn on motors if distance < 150
int pot_value = analogRead(A0);
int motor_speed = map(duration, 0, 9000, 255, 0);
int pot_valuee = map(pot_value, 0, 675, 0, 19000);
if (duration < pot_valuee) {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(enA, motor_speed); //funciton for SPEED opposite to CM
Serial.print(duration);
Serial.print(" -- ");
Serial.print(motor_speed);
Serial.print(" -- ");
Serial.println(pot_value);
}
else {
Serial.print("Nessun ostacolo rilevato entro 150cm -- ");
Serial.print(duration);
Serial.print(" -- ");
Serial.print(motor_speed);
Serial.print(" -- ");
Serial.println(pot_value);
analogWrite(enA, LOW);
}
}