Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////////////////////////////////////
- //
- // AS-MLV_P VOC Sensor
- //
- // Test program
- //
- // (c) 2014 by Koepi
- //
- ///////////////////////////////////////////////////////
- #define heater 0b00000100 // Heater VCC on D2 (PinD2)
- #define sensor 0b00000001 // Sensor VCC/Input on A0 (PinC0)
- #define GREEN 0b00001000 // RGB LED green
- #define BLUE 0b00010000 // RGB LED blue
- #define RED 0b00100000 // RGB LED red
- uint16_t sensor_val = 0;
- uint32_t sensor_ovs = 0;
- void setup() {
- Serial.begin(115200);
- DDRD |= heater; // Heater is OUTPUT
- PORTD &= ~(heater); // Heater is OFF
- DDRC &= ~(sensor); // Sensor is INPUT
- PORTC &= ~(sensor); // Disable PullUp
- DDRD |= RED | GREEN | BLUE; // RGB LED is OUTPUT
- // ADC setup
- ADCSRA |= ((1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)); //Prescaler at 128 -> 125Khz ADC clock source
- // ADMUX |= (1<<REFS0); // Aref as woltage reference
- ADMUX |= (1<<REFS1); // Avcc(+5v) as voltage reference
- ADMUX |= 0; // Pin A0 -> No change ...
- ADCSRA |= (1<<ADEN); //Power up the ADC
- ADCSRA |= (1<<ADSC); //Start converting
- }
- void loop() {
- Serial.print("> ");
- for (int i=0; i<10; i++) { // emulate PWM 50% for connection without resistor
- PORTD |= heater; // turn on heating
- delay(1);
- PORTD &= ~(heater); // turn off heating
- delay(1);
- }
- delay(80);
- for (int i=0; i<64; i++) { // Oversampling
- ADCSRA |= (1<<ADSC); // Start a new conversion
- while(ADCSRA & (1<<ADSC)); // Wait for the conversion to finish
- sensor_ovs += ADC << 1;
- }
- sensor_val = (sensor_ovs >> 5);
- sensor_ovs=0;
- Serial.println(sensor_val);
- if (sensor_val<500) {
- PORTD |= GREEN;
- PORTD&=~(RED|BLUE);
- } else if (sensor_val<1000) {
- PORTD |= BLUE;
- PORTD&=~(RED|GREEN);
- } else if (sensor_val>=1000) {
- PORTD |= RED;
- PORTD&=~(GREEN|BLUE);
- }
- delay(850);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement