Light Sensor Example

A simple Insense program requesting sensor readings from the solar sensor and uses the LEDs to indicate and increase/decrease in ambient light. This is achieved through two components – one interacting with the temperature sensor and one controlling the LEDs.

// Interface for Light Sensor Reader
type ISensorReader is interface (
    out bool solarRequestChan ;
    in integer solarValueChan ; 
    out integer ledsChan
)
 
// Light Sensor Reader component
component SensorReader presents ISensorReader {
	constructor() {	}
	behaviour {
		// get solar sensor reading
		send true on solarRequestChan
		receive solarValue from solarValueChan
		send solarValue on ledsChan
	}
} 
 
// Interface for LED output component
type ILedOutput is interface ( 
    in integer input ;
    out bool redLed ; 
    out bool greenLed 
)
 
// LED output component
component LedOutput presents ILedOutput {
 
	brighter = false
	darker = false
	avgSolar = 0
 
	constructor() {	}
	behaviour {
		// get solar radiation reading
		receive solar from input 
 
		// represent reading via LEDs, green - brighter, red darker
		brighter := (solar - avgSolar) > 20
		darker := (avgSolar - solar) > 20
		send brighter on greenLed
		send darker on redLed
 
		// adjust averages
		if solar > avgSolar then { avgSolar := avgSolar + 1 }
		else { avgSolar := avgSolar - 1 }
	}
} 
 
lo = new LedOutput()
sr = new SensorReader()
 
connect sr.solarRequestChan to lightHumidTempSensor.solarRequest
connect sr.solarValueChan to lightHumidTempSensor.solarOutput
connect sr.ledsChan to lo.input
connect lo.redLed to leds.redState
connect lo.greenLed to leds.greenState