Advertisement
Guest User

Untitled

a guest
Mar 14th, 2018
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <Adafruit_ssd1306syp.h>
  2. #include <math.h>
  3.  
  4. #define SDA_PIN A4
  5. #define SCL_PIN A5
  6. const int analogInPin = A2;
  7. float sensorValue = 0;
  8. int moist = 0;
  9. Adafruit_ssd1306syp display(SDA_PIN,SCL_PIN);
  10.  
  11. void setup() {
  12.   Serial.begin(9600);
  13.   delay(1000);
  14.   display.initialize();
  15. }
  16.  
  17. float readMoisture(){
  18.   float tmpVal=0;
  19.   for (int i=0; i <= 25; i++){
  20.     tmpVal+=analogRead(analogInPin);
  21.     delay(10);
  22.   }
  23.   return (tmpVal / 25);
  24. }
  25.  
  26. void loop() {
  27.   // put your main code here, to run repeatedly:
  28.   sensorValue = readMoisture();
  29.   display.setTextSize(2);
  30.   display.setTextColor(WHITE);
  31.   display.setCursor(20,0);
  32.   display.println("Moisture");
  33.   display.setTextColor(WHITE);
  34.   display.setCursor(38,18);
  35.   display.println("Level");
  36.   Serial.print("Raw: ");
  37.   Serial.println(sensorValue);
  38.   moist=map(sensorValue, 0, 1023, 0, 100);
  39.   Serial.print(moist);
  40.   Serial.println("%");
  41.   if (moist < 10) {
  42.     display.setCursor(48,40);
  43.   }else if(moist < 100){
  44.     display.setCursor(40,40);
  45.   }else{
  46.     display.setCursor(32,40);
  47.   }
  48.   display.print(moist);
  49.   display.println("%");
  50.   display.update();
  51.   delay(1000);
  52.   display.clear();
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement