diff --git a/multidetecteur.ino b/multidetecteur.ino new file mode 100644 index 0000000..e74a167 --- /dev/null +++ b/multidetecteur.ino @@ -0,0 +1,249 @@ +#include +#include + +#define DHTPIN 10 +#define DHTTYPE DHT22 +#define LOG_PERIOD 60000 // cetak tiap detik + +DHT dht(DHTPIN, DHTTYPE); +struct +{ + uint32_t total; + uint32_t ok; + uint32_t crc_error; + + uint32_t time_out; + uint32_t unknown; +} stat = { 0,0,0,0,0 }; + +// Identificateur pour l'affichage +const int UV = 0; +const int TEMP = 1; +const int HUMID = 2; +const int RAD = 3; +const int BATTERY = 4; +const int PARTICULES = 5; + +// Buttons Pins +const int BSELECT = 4; +const int BUP = 2; +const int BDOWN = 5; +const int TEMP_HUMID = 6; + +int posaffich = 0; // position actuelle de l'affichage +int max_posaffich = 5; // nombre de valeur à afficher moins 1 +int UVOUT = A0; //Output from the sensor +int REF_3V3 = A1; //3.3V power on the Arduino board +//int UVsensorIn = A0; //Output from the sensor +//int REFsensorIn = A1; +LiquidCrystal_I2C lcd(0x27,8,2); + +byte bouton; +unsigned long counts; //variable for GM Tube events +unsigned long previousMillis;//variable for time measurement +String texts[6]= {"UV int:", "Temp", "Humid", "Bq/mn", "Part/s", "Battery"}; +float values[6]; +void impulse() { // dipanggil setiap ada sinyal FALLING di pin 2 + counts++; +} + +void setup() +{ + pinMode(UVOUT, INPUT); + pinMode(REF_3V3, INPUT); + pinMode(BSELECT, INPUT_PULLUP); + pinMode(BUP, INPUT_PULLUP); + pinMode(BDOWN, INPUT_PULLUP); + pinMode(TEMP_HUMID, INPUT); + + attachInterrupt(digitalPinToInterrupt(3), impulse, FALLING); //define external interrupts + Serial.println("Start counter"); +// pinMode(UVsensorIn, INPUT); +// pinMode(REFsensorIn, INPUT); + Serial.begin(9600); //open serial port, set the baud rate to 9600 bps + dht.begin(); + lcd.init(); + lcd.init(); + lcd.setCursor(0,0); + lcd.clear(); + lcd.print("hello"); + delay(1000); +} + +void loop() +{ + bouton = test_bouton(); //retourne la valeur entre + Serial.print( "position afficheur =>" ); + Serial.print( posaffich ); + Serial.println(); + lcd.clear(); + + values[UV] = uv(); + values[TEMP] = temp(); + values[HUMID] = humid(); + values[RAD] = geiger(); + values[PARTICULES] = particules(); + values[BATTERY] = battery(); + affich(); + delay(200); +} + +void affich() +{ + Serial.print( "position afficheur => " ); + Serial.print( posaffich ); + Serial.println(); + + lcd.clear(); + Serial.print( "texte => "); + Serial.print( texts[posaffich] ); + Serial.println(); + lcd.setCursor(0,0); + lcd.print(texts[posaffich]); + lcd.setCursor(0,1); + lcd.print(values[posaffich]); +} + +float uv() +{ + float uvLevel = averageAnalogRead(UVOUT); + float refLevel = averageAnalogRead(REF_3V3); +// int uvLevel = averageAnalogRead(UVsensorIn); +// int refLevel = averageAnalogRead(REFsensorIn); +// Serial.print(" Ulevel: "); +// Serial.print(uvLevel); +// Serial.print(" reflevel"); + +// Serial.print(refLevel); + float outputVoltage = 3.3 * uvLevel/refLevel; +// Serial.print(" output voltage: "); +// Serial.print(outputVoltage); + + float uvIntensity = mapfloat(outputVoltage, 0.97, 2.9, 0.0, 15.0); + + Serial.print(" UV Intensity: "); + Serial.print(uvIntensity); +// Serial.print(" mW/cm^2"); +// Serial.print(" input:"); +// Serial.print(uvLevel); + Serial.println(); + return uvIntensity; + +} + +//Takes an average of readings on a given pin +//Returns the average +int averageAnalogRead(int pinToRead) +{ + byte numberOfReadings = 20; + float runningValue = 0; + float Value = 0; + for(int x = 0 ; x < numberOfReadings ; x++) + { + float val = analogRead(pinToRead); + runningValue += val; + } + Value = runningValue / (float)numberOfReadings; + return Value; +} + +float mapfloat(float x, float in_min, float in_max, float out_min, float out_max) +{ + return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; +} + +byte test_bouton() +{ + + if ( digitalRead( BSELECT ) == 0) + { + // put here the code if select button is pushed + Serial.print( "SELECT" ); + Serial.println(); + return BSELECT; + }else if ( digitalRead( BUP ) == 0 ) + { + // code for button up pushed + Serial.print( "UP" ); + Serial.println(); + if ( posaffich == 0 ) + { + posaffich = max_posaffich; + }else + { + posaffich--; + } + return BUP; + }else if ( digitalRead( BDOWN ) == 0 ) + { + // code for button down pushed + Serial.print( "Down" ); + Serial.println(); + if ( posaffich == max_posaffich ) + { + posaffich = 0; + }else + { + posaffich++; + } + return BDOWN; + } + return 0; +} + +float temp() +{ + uint32_t start = micros(); + float t = dht.readTemperature(); + uint32_t stop = micros(); + + stat.total++; + if (isnan(t)) { + Serial.println("Failed to read temperature from DHT"); + return -199; + } else { + return t; + } +} + +float humid() +{ + uint32_t start = micros(); + float h = dht.readHumidity(); + uint32_t stop = micros(); + + stat.total++; + if (isnan(h)) { + Serial.println("Failed to read humidity from DHT"); + return -1; + } else { + return h; + } + +} + +float geiger() +{ + //here the code for acquiring radiations + //affich(RAD, text, value); + unsigned long currentMillis = millis(); + if (currentMillis - previousMillis > LOG_PERIOD) { + previousMillis = currentMillis; + Serial.println(counts); + return counts; // affiche le nombre de particules comptées en une minute + counts = 0; + } +} + +float particules() +{ + //here the code for acquiring particules + //affich(PARTICULES, text, value); +} + +float battery() +{ + //here the code for acquiring battery + //affich(BATTERY, text, value); +} +