Problem with delay function

Does anybody can help why the code does not run at all on Arduino UNO when d1 and d2 are both set to 200, but works fine on Arduino Mega .

FYI: the code runs a motor with operates a tapper. Tap number is measured and finally the frequency is calculated. I used the two delays in order to control the motor speed. I know that I can control it using the driver; however, when it is quite low, the motor does not rotate at all. I though by turning on and off the motor for certain times, I can control the frequency.


// For counter:
float counter = 0;
int sence_pin = 4;
int pin_state;
int previous_pin_state = 0;
unsigned long startMillis = 0;
unsigned long currentMillis = 0;
const unsigned long period = 5000;  // Set the time

float frequecny = 0;

//...............
// for motor control:

int MotorPin1 = 10;  // connected to IN1
int MotorPin2 = 9;   // connected to IN2
int MotorPower = 5;  // connected to ENA

// Change the motor speed, d1, and d2 to reach the appropriate frequency

//speed	= 255, d1 = 0 ,	d2 =	0, Frequency (Hz) = 4.2
//speed	= 255, d1 = 50,	d2 =	75, Frequency (Hz) = 3.15
//speed	= 255, d1 = 50,	d2 =	150, Frequency (Hz) = 1.8
//speed	= 200, d1 = 50,	d2 =	300, Frequency (Hz) = 1
//speed	= 200, d1 = 200,	d2 =	200, Frequency (Hz) = 0.4


int speed = 200;  // Set the speed
int d1 = 0;     // Set the motor on/off delay
int d2 = 100;     // Set the motor on/off delay
void setup() {

  // for tapper:
  pinMode(sence_pin, INPUT);
  // for motor:
  pinMode(MotorPin1, OUTPUT);
  pinMode(MotorPin2, OUTPUT);
  pinMode(MotorPower, OUTPUT);
  Serial.begin(9600);
  Serial.println("Type something in the serial monitor and send to start the code");
  delay(1000);
}

void loop() {
  if (Serial.available() > 0) {
    counter_function();
    if (currentMillis - startMillis <= period) {
      motor_on();
    } else {
      motor_off();
    }
    delay(d1);
    if (d2 > 0) {
      motor_off();
      delay(d2);
    }
  }
}

void counter_function() {

  int pin_state = digitalRead(sence_pin);
  if (pin_state != previous_pin_state) {
    counter = counter + 1;
    Serial.println(currentMillis - startMillis);

    if (counter == 1) { startMillis = millis(); }
    currentMillis = millis();
    if (currentMillis - startMillis > period) {
      Serial.println("Frequency (tap/s(Hz))= ");
      frequecny = counter / 2 / (period / 1000);
      Serial.println(frequecny);
      Serial.println(counter);
    }
    previous_pin_state = pin_state;
  }

  delay(20);
}

void motor_on() {
  digitalWrite(MotorPin1, LOW);
  digitalWrite(MotorPin2, HIGH);
  analogWrite(MotorPower, speed);
}
void motor_off() {
  analogWrite(MotorPower, 0);
}

what is this for?

It keeps the motor off for a certain time (d2).

Is it possible "d1, d2, dx..." are reserved on some boards for DIO Pin Number? Try using a word to replace "d".

2 Likes

try replacing d2 with delay2 and d1 with delay1

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.