-1

I have three analog sensors like voltage, temperature and pressure. I need to measure the voltage and temperature every 10 ms and I need to measure the pressure alone every 30 ms by using the timers in an Arduino.

How can I do this?

6
  • 3
    see the BlinkWithoutDelay example in Arduino IDE Examples menu Commented Dec 26, 2022 at 17:26
  • Have you seen this library? avdweb.nl/arduino/timing/virtualdelay Commented Dec 26, 2022 at 17:49
  • please add your code to your post Commented Dec 26, 2022 at 19:36
  • You only need one timer for this, which triggers every 10ms. Then read pressure only every third time. Or you can just use millis() or micros() depending on the rest of your code. Please share that code and explain what it should do Commented Dec 26, 2022 at 20:21
  • start with BlinkWithoutDelay as @Juraj mentioned ... do not delete any part of the code ... add a counter variable and set it to zero... go to the part of the program where the LED is updated ... add a function to measure voltage and temperature ... if the counter = 0, then measure pressure ... increment counter ... if the counter = 3, then set counter to zero ... when everything works then change the delay variable from 500 to 10 Commented Dec 26, 2022 at 20:23

1 Answer 1

1

May this help as starting point! Not tested

void setup() {
  Serial.begin(9600);
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
}

unsigned long previousMillis1;
unsigned long previousMillis2;

void loop() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis1 >= 10) {
    int val1 = analogRead(A0);
    int val2 = analogRead(A1);
    Serial.print("Values from A0 and A1: ");
    Serial.print(val1);
    Serial.print(" ");
    Serial.println(val2);
    previousMillis1 = currentMillis;
  }

  if (currentMillis - previousMillis2 >= 30) {
    int val3 = analogRead(A2);
    Serial.print("Value from A2: ");
    Serial.println(val3);
    previousMillis2 = currentMillis;
  }
}
2
  • Thank you for your kind help Mr. Carlos Costa, but i am in need to use timer 1 or 2. without using millis i have to read the sensor data Commented Dec 29, 2022 at 3:32
  • Presumably it is a school project. Are you allowed to use ready made libraries or do you have to use direct configuration of the timer's hardware registers? Which Arduino are you using? In any case, the datasheet for the microcontroller is a good (but heavy going) start. Commented Dec 29, 2022 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.