Go Down

Topic: Timing Problems with micros() (Read 363 times) previous topic - next topic

Crameroni

Jun 21, 2018, 11:20 am Last Edit: Jun 21, 2018, 11:45 am by Crameroni
Hello Forum,
Can anyone help me identify why the arduino doesnt stick to my code?

When I am using this code (no warnings or something else) the arduino does not stick to the timings.

I attached a picture from my oscilloscope so you can see what I mean.

The arduino seems to ignore the values I give him vor the Intervals.









Quote
int VVRPin = 42;
int VDRPin = 52;
int VSHPin = 34;
int RESPin = 10;
int CLKPin = 26;

unsigned long previousMicros1 = 0;
unsigned long previousMicros2 = 0;
unsigned long previousMicros3 = 0;
unsigned long previousMicros4 = 0;
const long VVRStart = 15;
const long VVRStop = 30;
const long VDRStop = 210;
const long RESStart = 0;

void setup() {
  Serial.begin(115200);

  pinMode(VVRPin, OUTPUT);
  pinMode(VDRPin, OUTPUT);
  pinMode(VSHPin, OUTPUT);
  pinMode(RESPin, OUTPUT);
  pinMode(CLKPin, OUTPUT);
  digitalWrite(CLKPin, HIGH);

}

void loop() {
  unsigned long currentMicros1 = micros();
  unsigned long currentMicros2 = micros();
  unsigned long currentMicros3 = micros();
  unsigned long currentMicros4 = micros();
  if ( currentMicros1 - previousMicros1 >= RESStart)
  {
    digitalWrite(RESPin, LOW);
    digitalWrite(RESPin, HIGH);
  }
  if (currentMicros2 - previousMicros2 >= VVRStart) {
    digitalWrite(VVRPin, HIGH);
    digitalWrite(VDRPin, HIGH);
  }

  if (currentMicros3 - previousMicros3 >= VVRStop) {
    digitalWrite(VVRPin, LOW);
  }

  if (currentMicros4 - previousMicros4 >= VDRStop) {
    digitalWrite (VDRPin, LOW);
  }

    previousMicros1 = currentMicros1;
    previousMicros2 = currentMicros2;
    previousMicros3 = currentMicros3;
    previousMicros4 = currentMicros4;

  delay(1000); // Delay because ther follows later a lot more code...

Go Up