Advertisement
Guest User

Untitled

a guest
Feb 7th, 2024
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #define ANREDEFT 120
  2. #define SENDSERIALDELAY 1000
  3.  
  4. uint8_t enableAnalogReadA1 = 1;
  5. uint32_t previousMillisAnalogReadA1 = 0;
  6. uint32_t intervalAnalogReadA1 = ANREDEFT; // ANALOG_READ_A1_DFT_DELAY;
  7. uint16_t currAnaReadVal=0;
  8. uint32_t previousMillisSendStatusSerial = 0;
  9. uint32_t intervalSendStatusSerial = SENDSERIALDELAY; // ANALOG_READ_A1_DFT_DELAY;
  10.  
  11.  
  12. void setup()
  13. {
  14.   ADCSRA = bit(ADEN)                               // Turn ADC on
  15.            | bit(ADPS0) | bit(ADPS1) | bit(ADPS2); // Prescaler of 128
  16.   ADMUX = bit(REFS0)                               // AVCC
  17.           | ((1) & 0x07);                          // Arduino Uno to ADC pin 1=A1 , 0= A0 etc.
  18.   // initiate getting value first time now
  19.   bitSet(ADCSRA, ADSC);
  20.   Serial.begin(9600);
  21. }
  22.  
  23. void loop()
  24. {
  25.   unsigned long nowStamp = millis();
  26.  
  27.   //stop the loop to read the analog value requested and request a new read
  28.   if (enableAnalogReadA1 && (nowStamp - previousMillisAnalogReadA1 >= intervalAnalogReadA1))
  29.   {
  30.     if (bit_is_clear(ADCSRA, ADSC))
  31.     {
  32.       currAnaReadVal = ADC;
  33.       bitSet(ADCSRA, ADSC);                  // request new read
  34.       previousMillisAnalogReadA1 = nowStamp; // set timer
  35.       intervalAnalogReadA1 = ANREDEFT;       // aplicable if intervalAnalogReadA1 changed
  36.     }
  37.     else
  38.     {
  39.       intervalAnalogReadA1 += 30; // give it an extra 30 ms to clear.
  40.       // Probably useless, Should never take longer than the hw dft 120ms
  41.     }
  42.   }
  43.   //stop the loop to send the temperature to the serial port every SENDSERIALDELAY
  44.   if (nowStamp - previousMillisSendStatusSerial >= intervalSendStatusSerial)
  45.   {
  46.     previousMillisSendStatusSerial += intervalSendStatusSerial;
  47.     Serial.print("Current A1 value is : ");
  48.     Serial.println(currAnaReadVal,DEC);
  49.   }
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement