You are reading a single comment by @furuskog and its replies. Click here to read the full conversation.
  • here's the arduino code i'll try to convert to a pixl when i get home this weekend. it reads from a moisture sensor, and a temperature/humidity sensor, and displays the values to an rgb display. unfortunately i won't be able to change the display color based on the moisture level, but it will be a good test. in my case i'm using an arduino shield with a grove sensor kit, so it will be a good test on the pixl as a shield as well.

    [#include](http://forum.espruino.com/sea­rch/?q=%23include) "Arduino.h"
    [#include](http://forum.espruino.com/sea­rch/?q=%23include) "Wire.h" 
    [#include](http://forum.espruino.com/sea­rch/?q=%23include) <rgb_lcd.h>
    [#include](http://forum.espruino.com/sea­rch/?q=%23include) <TH02_dev.h>
    [#include](http://forum.espruino.com/sea­rch/?q=%23include) <stdio.h>
    [#include](http://forum.espruino.com/sea­rch/?q=%23include) <math.h>
    [#include](http://forum.espruino.com/sea­rch/?q=%23include) <TimeLib.h>
    [#include](http://forum.espruino.com/sea­rch/?q=%23include) <TimeAlarms.h>
    
    rgb_lcd lcd;
    
    const int button = 2;       // connect a button
    const int buzzer = 3;       // connect a buzzer
    // Test code for Grove - Moisture Sensor 
    int sensorPin = A0; // select the input pin for the potentiometer
    int moisture = -1; // variable to store the value coming from the sensor7
    int prec = 0;
    int buttonPress = 0;
    int displayOn = 1;
    
    void setup() {
        Serial.begin(9600);
        // declare the ledPin as an OUTPUT:
        pinMode(button, INPUT); //set button as an INPUT device
        pinMode(buzzer, OUTPUT);   //set LED as an OUTPUT device
        // set up the LCD's number of columns and rows:
        lcd.begin(16, 2);
        // Set the LCD background color
        setColor();
        Serial.println("****TH02_dev demo by seeed studio****\n");
        /* Power up,delay 150ms,until voltage is stable */
        delay(150);
        /* Reset HP20x_dev */
        TH02.begin();
        delay(100);
        
        /* Determine TH02_dev is available or not */
        Serial.println("TH02_dev is available.\n");   
    
        Alarm.timerRepeat(5, readSensors);
        Alarm.timerRepeat(60, displayOff);
    }
    
    void loop() {
        buttonPress = digitalRead(button); //read the status of the button
        if( buttonPress && !displayOn ) {
          lcd.display();
          displayOn = 1;
          setColor();
        }
        //digitalWrite(buzzer, btn);
    
        Alarm.delay(10);
    }
    
    void readSensors() {
      Serial.println("Reading sensors");
      // read the value from the sensor:
      moisture = analogRead(sensorPin);
      Serial.print("Moisture = " );
      Serial.println(moisture);
      float temper = TH02.ReadTemperature(); 
      Serial.println("Temperature: ");   
      Serial.print(temper);
      Serial.println("C\r\n");
    
      float humidity = TH02.ReadHumidity();
      Serial.println("Humidity: ");
      Serial.print(humidity);
      Serial.println("%\r\n");
      char message1[16];
      char message2[16];
      float ftemp = 1.8*temper + 32;
      long t = lround(ftemp);
      int tm = int((ftemp-floor(ftemp))*100)%100;
      long h = lround(humidity);
      int hm = int((humidity-floor(humidity))*100)%100;­
      long m = moisture;
      sprintf(message1, "%ld.%dF, %ld.%dRH", t, tm, h, hm);
      Serial.println(int((temper-floor(temper)­)*100)%100);
      setColor();
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print(message1);
      lcd.setCursor(0,1);
      sprintf(message2, "Moisture: %3ld", m);
      lcd.print(message2);
    }
    
    void displayOff() {
      if( buttonPress ) {
        buttonPress = 0;
      }
      else {
        lcd.noDisplay();
        lcd.setColorAll();
        displayOn = 0;
      }
    }
    
    void setColor() {
      if( displayOn ) {
        if( moisture < 0 ) {
          lcd.setColorAll();
        }
        else if( moisture < 300 ) {
          lcd.setColor(RED);
        }
        else if( moisture < 650 ) {
          lcd.setColor(GREEN);
        }
        else {
          lcd.setColor(BLUE); 
        }
      }
    }
    
    
About

Avatar for furuskog @furuskog started