More on the plant sensor

The plan is to have some plant sensing working for a groworld installation we are doing at Camp Pixelache so I’ve made the circuitry a little more durable with a bit of soldering:

Here’s the circuit diagram – the values of components are influenced by what I happen to have in my collection of surplus/donated stock but are based on Nik’s original circuit:

The code running on the arduino can be found here.

This is the code that reads the arduino in fluxus. It contains some hastily written auto calibration, which records the highest and lowest values received from each sensor and scales the output between 0 and 1 using these values. This isn’t necessarily the right thing to do in all situations.

;; p l a n t   e y e s  [ copyright (c) 2010 foam vzw : gpl v3 ]

#lang scheme/base

; read values from arduino on the serial port, and hand them out to the game

(provide (all-defined-out))

(define serial (open-input-file "/dev/ttyUSB0"))
;(define serial #f)

(define current-raw '(0 0))
(define light-range '(999999 0))
(define moisture-range '(9999999 0))
(define current-cali '(0 0))

; return the raw sensor values
(define (sensor-light)
  (car current-raw))

(define (sensor-moisture)
  (cadr current-raw))

; return the calibrated sensor values
(define (sensor-cali-light)
  (car current-cali))

(define (sensor-cali-moisture)
  (cadr current-cali))

(define (fit a l h)
  (if (zero? (- h l)) 
      0
      (exact->inexact (/ (- a l) (- h l)))))

(define (sensor-update)
  (when (and serial (char-ready? serial))
    (set! current-raw (sensor-process (read-line serial)))
    
    ; some auto calibration
    (set! light-range 
          (list 
           (if (< (sensor-light) (car light-range))
               (sensor-light) (car light-range))
           (if (> (sensor-light) (cadr light-range))
               (sensor-light) (cadr light-range))))
    
    (set! moisture-range 
          (list 
           (if (< (sensor-moisture) (car moisture-range))
               (sensor-moisture) (car moisture-range))
           (if (> (sensor-moisture) (cadr moisture-range))
               (sensor-moisture) (cadr moisture-range))))
    
    (set! current-cali (list (fit (sensor-light) (car light-range) (cadr light-range))
                             (fit (sensor-moisture) (car moisture-range) (cadr moisture-range))))
    (printf "current-cali ~a~n" current-cali)                           
    (printf "ranges ~a ~a~n" light-range moisture-range)))

(define (convert n)
  (string->number n))

(define (sensor-process str)
  (let ([data (regexp-split #rx"," str)])
    (list
     (convert (list-ref data 0))
     (convert (list-ref data 1)))))

Leave a Reply

Your email address will not be published. Required fields are marked *