Hello everyone!!!! I'm a newbie here and I need some assistance. I'm trying to control the time that the 2 motors are on after reading a value provided by a load cell. In the code below I created random values from 7 to 15 (which are the values that I'm interested in the load cell once I put everything together). Once the system reads the value, the motor should activate for 1 second and the shut down until the next reading comes in, but I just cannot figure this out. Could someone help this poor ME that has no experience with coding? Thanks
#define enA 9
#define in1 4
#define in2 5
#define enB 10
#define in3 6
#define in4 7
unsigned long previousMillis = 0;
const long interval = 1000;
int motorSpeedA = 0;
int motorSpeedB = 0;
void setup() {
Serial.begin(9600);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
randomSeed(analogRead(0));
}
void loop() {
unsigned long currentMillis = millis();
int weight=random(6,17);
Serial.println(weight);
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (weight <= 11) {
// Set Motor A forward
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// Set Motor B forward
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
motorSpeedA = map(weight, 11, 6, 170, 255);
motorSpeedB = map(weight, 11, 6, 170, 255);
}
else if (weight > 11) {
// Set Motor A forward
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// Set Motor B forward
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
motorSpeedA = map(weight, 16, 12, 0, 169);
motorSpeedB = map(weight, 16, 12, 0, 169);
}
else {
}
// if (motorSpeedA < 70) {
// motorSpeedA = 0;
// }
// if (motorSpeedB < 70) {
// motorSpeedB = 0;
// }
analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
}
delay (5000);
}