PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

readPin.ino 2.2KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Example for readPin
  2. * It measures continuously the voltage on pin A9,
  3. * Write a pin number to measure that instead.
  4. * If the pin is wrong, an error message will appear, change the pin and then write -1 to clear error.
  5. */
  6. #include <ADC.h>
  7. #include <ADC_util.h>
  8. int readPin = A2; // ADC0
  9. ADC *adc = new ADC();; // adc object
  10. void setup() {
  11. pinMode(LED_BUILTIN, OUTPUT);
  12. pinMode(readPin, INPUT);
  13. Serial.begin(9600);
  14. ///// ADC0 ////
  15. adc->adc0->setAveraging(16); // set number of averages
  16. adc->adc0->setResolution(16); // set bits of resolution
  17. adc->adc0->setConversionSpeed(ADC_CONVERSION_SPEED::VERY_LOW_SPEED); // change the conversion speed
  18. adc->adc0->setSamplingSpeed(ADC_SAMPLING_SPEED::MED_SPEED); // change the sampling speed
  19. ////// ADC1 /////
  20. #ifdef ADC_DUAL_ADCS
  21. adc->adc1->setAveraging(16); // set number of averages
  22. adc->adc1->setResolution(16); // set bits of resolution
  23. adc->adc1->setConversionSpeed(ADC_CONVERSION_SPEED::MED_SPEED); // change the conversion speed
  24. adc->adc1->setSamplingSpeed(ADC_SAMPLING_SPEED::MED_SPEED); // change the sampling speed
  25. #endif
  26. delay(500);
  27. }
  28. int value1 = 0, value2 = 0;
  29. int pin=0;
  30. void loop() {
  31. if (Serial.available()) {
  32. pin = Serial.parseInt();
  33. if(pin>0) {
  34. Serial.print("Changing to pin ");
  35. Serial.println(pin);
  36. readPin = pin;
  37. }
  38. }
  39. Serial.print("Pin: ");
  40. Serial.print(readPin);
  41. Serial.print(", ADC0 value: ");
  42. value1 = adc->adc0->analogRead(readPin);
  43. Serial.print(value1*3.3/adc->adc0->getMaxValue(), DEC);
  44. #ifdef ADC_DUAL_ADCS
  45. Serial.print(", ADC1 value: ");
  46. value2 = adc->adc1->analogRead(readPin);
  47. Serial.println(value2*3.3/adc->adc0->getMaxValue(), DEC);
  48. #else
  49. Serial.println();
  50. #endif
  51. // Print errors, if any.
  52. if(adc->adc0->fail_flag != ADC_ERROR::CLEAR) {
  53. Serial.print("ADC0: "); Serial.println(getStringADCError(adc->adc0->fail_flag));
  54. }
  55. #ifdef ADC_DUAL_ADCS
  56. if(adc->adc1->fail_flag != ADC_ERROR::CLEAR) {
  57. Serial.print("ADC1: "); Serial.println(getStringADCError(adc->adc1->fail_flag));
  58. }
  59. #endif
  60. adc->resetError();
  61. //digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
  62. delay(200);
  63. }