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.

144 lines
3.5 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. /*
  2. DigitalReadSerial
  3. Reads a digital input on pin 2, prints the result to the serial monitor
  4. This example code is in the public domain.
  5. */
  6. //NOTE: we are using 1.1V VRef, so keep ADC input low.
  7. //current
  8. int ADC_VAL0 = A1;
  9. //
  10. int ADC_VAL1 = A4;
  11. //smoothing
  12. const int numReadings = 100;
  13. int readings[numReadings]; // the readings from the analog input
  14. int readIndex = 0; // the index of the current reading
  15. int total = 0; // the running total
  16. int average = 0; // the average
  17. int readings2[numReadings]; // the readings from the analog input
  18. int readIndex2 = 0; // the index of the current reading
  19. double total2 = 0; // the running total
  20. int average2 = 0; // the average
  21. int start = 0;
  22. int ADCVAL0_State = 0;
  23. int ADCVAL1_State = 0;
  24. int writeresult = 0;
  25. // the setup routine runs once when you press reset:
  26. void setup() {
  27. for (int thisReading = 0; thisReading < numReadings; thisReading++) {
  28. readings[thisReading] = 0;
  29. readings2[thisReading] = 0;
  30. }
  31. // initialize serial communication at 9600 bits per second:
  32. Serial.begin(9600);
  33. // make the ADC_VAL0's pin an input:
  34. //pinMode(ADC_VAL0, INPUT);
  35. analogReference(INTERNAL);
  36. //to find where arduino does this, grep -r analogReference in Arduino install folder
  37. //cryptic shit though
  38. //so instead, just fprint the register before and after the change. much more intuitive.
  39. //do it manually...
  40. //this doesn't work
  41. //ADMUX = (0 << REFS1) | (0 << REFS0) ;
  42. //this doesn't work
  43. //ADMUX = _BV(REFS1) | _BV(REFS0) | _BV(MUX0);
  44. //this doesn't work
  45. //ADMUX = ADMUX | 0b11001110;
  46. //this doesn't work
  47. //ADMUX = (1 << REFS1) | (1 << REFS0) ;
  48. //this also doesn't work
  49. //ADMUX |= (1 << REFS0);
  50. //ADMUX |= (1 << REFS1);
  51. //search engines have been fucked over with endless forum debates, hopeless. a blog post guide would suffice.
  52. //even this doesn't work. why... it should work. must be wiring code breaking things. give up for now.
  53. //ADMUX = ADMUX | 0b11000000;
  54. }
  55. // the loop routine runs over and over again forever:
  56. void loop() {
  57. // subtract the last reading:
  58. total = total - readings[readIndex];
  59. total2 = total2 - readings2[readIndex];
  60. // read from the sensor:
  61. readings[readIndex] = analogRead(ADC_VAL0);
  62. readings2[readIndex] = analogRead(ADC_VAL1);
  63. // add the reading to the total:
  64. total = total + readings[readIndex];
  65. total2 = total2 + readings2[readIndex];
  66. // advance to the next position in the array:
  67. readIndex = readIndex + 1;
  68. // if we're at the end of the array...
  69. if (readIndex >= numReadings) {
  70. // ...wrap around to the beginning:
  71. readIndex = 0;
  72. start = 1;
  73. writeresult = 1;
  74. }
  75. // calculate the average:
  76. average = total / numReadings;
  77. average2 = total2 / numReadings;
  78. // send it to the computer as ASCII digits
  79. if(start){
  80. ADCVAL0_State = average;
  81. ADCVAL1_State = average2;
  82. }
  83. // read the input pin:
  84. //int ADCVAL0_State = analogRead(ADC_VAL0);
  85. if (writeresult){
  86. //int ADCVAL1_State = analogRead(ADC_VAL1);
  87. // print out the state of the button:
  88. Serial.print(" ADCVAL0 Current in mA, ");
  89. Serial.print(ADCVAL0_State);
  90. Serial.print(",");
  91. Serial.print(" ADCVAL1 Battery Voltage divby 10, ");
  92. Serial.println(ADCVAL1_State);
  93. delay(5000); // delay in between reads for stability
  94. writeresult = 0;
  95. }
  96. //Trying to debug why I cant change a bit in a register. fucking wiring. fuck search engines.
  97. //int dog = ADMUX;
  98. //Serial.println(ADMUX,BIN);
  99. }