Advertisement
Guest User

15.06.2014 AS-MLV-P @ Arduino UNO

a guest
Jun 16th, 2014
583
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.89 KB | None | 0 0
  1. ///////////////////////////////////////////////////////
  2. //
  3. // AS-MLV_P VOC Sensor
  4. //
  5. // Test program
  6. //
  7. // (c) 2014 by Koepi
  8. //
  9. ///////////////////////////////////////////////////////
  10.  
  11. #define heater 0b00000100  // Heater VCC on D2 (PinD2)
  12. #define sensor 0b00000001  // Sensor VCC/Input on A0 (PinC0)
  13. #define GREEN 0b00001000  // RGB LED green
  14. #define BLUE 0b00010000  // RGB LED blue
  15. #define RED 0b00100000  // RGB LED red
  16. uint16_t sensor_val = 0;
  17. uint32_t sensor_ovs = 0;
  18.  
  19. void setup() {
  20.   Serial.begin(115200);
  21.   DDRD |= heater;  // Heater is OUTPUT
  22.   PORTD &= ~(heater);  // Heater is OFF
  23.   DDRC &= ~(sensor);  // Sensor is INPUT
  24.   PORTC &= ~(sensor);  // Disable PullUp
  25.   DDRD |= RED | GREEN | BLUE;  // RGB LED is OUTPUT
  26.   // ADC setup
  27.   ADCSRA |= ((1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0));  //Prescaler at 128 -> 125Khz ADC clock source
  28. //  ADMUX |= (1<<REFS0);  // Aref as woltage reference
  29.   ADMUX |= (1<<REFS1);  // Avcc(+5v) as voltage reference
  30.   ADMUX |= 0;  // Pin A0 -> No change ...
  31.   ADCSRA |= (1<<ADEN);  //Power up the ADC
  32.   ADCSRA |= (1<<ADSC);  //Start converting
  33. }
  34.  
  35. void loop() {
  36.   Serial.print("> ");
  37.   for (int i=0; i<10; i++) {  // emulate PWM 50% for connection without resistor
  38.     PORTD |= heater; // turn on heating
  39.     delay(1);
  40.     PORTD &= ~(heater); // turn off heating
  41.     delay(1);
  42.   }
  43.   delay(80);
  44.   for (int i=0; i<64; i++) {  // Oversampling
  45.     ADCSRA |= (1<<ADSC);  // Start a new conversion
  46.     while(ADCSRA & (1<<ADSC));  // Wait for the conversion to finish
  47.     sensor_ovs += ADC << 1;
  48.     }
  49.   sensor_val = (sensor_ovs >> 5);
  50.   sensor_ovs=0;
  51.   Serial.println(sensor_val);
  52.   if (sensor_val<500) {
  53.     PORTD |= GREEN;
  54.     PORTD&=~(RED|BLUE);
  55.     } else if (sensor_val<1000) {
  56.       PORTD |= BLUE;
  57.       PORTD&=~(RED|GREEN);
  58.       } else if (sensor_val>=1000) {
  59.         PORTD |= RED;
  60.         PORTD&=~(GREEN|BLUE);
  61.     }
  62.   delay(850);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement