Created
November 8, 2017 20:27
-
-
Save JasonLS/110b0e4416dd18f798a617dcca542fac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace BrainPadApplication5 | |
{ | |
//Includes all of the libraries necessary for the code to run | |
using System; | |
using System.Diagnostics; | |
using System.Threading; | |
using GHIElectronics.TinyCLR.Devices.Gpio; | |
using GHIElectronics.TinyCLR.Devices.Adc; | |
using GHIElectronics.TinyCLR.Pins; | |
class Plantbot | |
{ | |
// Constant universal variables set to use in our functions later and be able to change for specific plants | |
//or for testing purposes. | |
private const Double TemperatureHotnessThreshold = 80; | |
private const Double TemperatureColdnessThreshold = 45; | |
private const Double MoistureWetnessThreshold = 60; | |
private const Double MoistureDrynessThreshold = 40; | |
private const Double LightDarknessThreshold = 40; | |
//Boolean for checking for errors(if the reading is zero) in our code | |
private Boolean TemperatureErrorCheck(out Double Temperature) => (Temperature = BrainPad.TemperatureSensor. | |
ReadTemperature()) != 0; | |
private Boolean LightErrorCheck(out Double Light) => (Light = BrainPad.LightSensor.ReadLightLevel()) != 0; | |
private Boolean MoistureErrorCheck(out Double Moisture) => (Moisture = analog.ReadRatio() * 100) !=0; | |
//Declaring analog for use in Boolean and for obtaining an analog signal in the later of the code | |
AdcChannel analog; | |
public void BrainPadSetup() | |
{ | |
//Opens up analog channel and and sets GPIO port to high, allowing use | |
AdcController ADC = AdcController.GetDefault(); | |
analog = ADC.OpenChannel(BrainPad.Expansion.AdcChannel.An); | |
var Enable = GpioController.GetDefault().OpenPin(BrainPad.Expansion.GpioPin.Rst); | |
Enable.SetDriveMode(GpioPinDriveMode.Output); | |
Enable.Write(GpioPinValue.High); | |
} | |
public void BrainPadLoop() | |
{ | |
//Declaring all the variables in our loop (outside our functions) | |
Double CurrentLight; | |
String LightMessage; | |
Double CurrentTemperature; | |
Double FahrenheitTemperature; | |
String TemperatureMessage; | |
Double CurrentMoisture; | |
String MoistureMessage; | |
while (true) | |
{ //Light | |
if (!this.LightErrorCheck(out CurrentLight)) BrainPad.Display.DrawSmallText(50, 30, "Error"); | |
LightMessage = LightOutput(CurrentLight); | |
BrainPad.Display.DrawSmallText(0, 30, "L "+CurrentLight.ToString("F2") + "%"); | |
BrainPad.Display.DrawSmallText(50, 30, LightMessage); | |
// | |
//Temperature | |
if (!this.TemperatureErrorCheck(out CurrentTemperature)) BrainPad.Display.DrawSmallText(50, 0, "Error"); | |
FahrenheitTemperature = CelsiusToFahrenheitConversion(CurrentTemperature); | |
BrainPad.Display.DrawSmallText(0, 0, "T "+FahrenheitTemperature.ToString("F2")); | |
TemperatureMessage = TemperatureOutput(FahrenheitTemperature); | |
BrainPad.Display.DrawSmallText(50, 0, TemperatureMessage); | |
// | |
//Moisture | |
if (!this.MoistureErrorCheck(out CurrentMoisture)) BrainPad.Display.DrawSmallText(50, 55, "Error"); | |
BrainPad.Display.DrawSmallText(0, 55,"M "+CurrentMoisture.ToString("F2")+"%"); | |
MoistureMessage = MoistureOutput(CurrentMoisture); | |
BrainPad.Display.DrawSmallText(50, 55, MoistureMessage); | |
// | |
//Puts a delay on the displays | |
BrainPad.Display.ShowOnScreen(); | |
BrainPad.Wait.Seconds(5); | |
BrainPad.Display.ClearScreen(); | |
} | |
} | |
//Function to convert the default Celsius temperature to Fahrenheit | |
public Double CelsiusToFahrenheitConversion(Double TemperatureToBeConverted) | |
{ | |
TemperatureToBeConverted = TemperatureToBeConverted * 1.8 + 32; | |
return TemperatureToBeConverted; | |
} | |
//Function that creates a message for the user if it is too hot or cold | |
public String TemperatureOutput(Double Temperature) | |
{ | |
Double ConvertedTemperature = CelsiusToFahrenheitConversion(Temperature); | |
String TemperatureMessage; | |
if (Temperature > Plantbot.TemperatureHotnessThreshold) | |
{ | |
TemperatureMessage = "It's too hot!"; | |
BrainPad.LightBulb.TurnRed(); | |
} | |
else | |
{ | |
if (Temperature < Plantbot.TemperatureColdnessThreshold) | |
{ | |
TemperatureMessage = "It's too cold!"; | |
BrainPad.LightBulb.TurnBlue(); | |
} | |
else | |
{ | |
TemperatureMessage = ""; | |
BrainPad.LightBulb.TurnOff(); | |
BrainPad.Display.ClearPartOfScreen(50, 0, 128, 20); | |
} | |
} | |
return TemperatureMessage; | |
} | |
//Function that creates a message for the user if it is too dry or too wet/flooded | |
public String MoistureOutput(Double Moisture) | |
{ | |
String MoistureMessage; | |
if (Moisture > Plantbot.MoistureWetnessThreshold) | |
{ | |
MoistureMessage = "Too flooded!"; | |
BrainPad.Buzzer.StartBuzzing(10); | |
} | |
else | |
{ | |
if (Moisture < Plantbot.MoistureDrynessThreshold) | |
{ | |
MoistureMessage = "More water!"; | |
BrainPad.Buzzer.StartBuzzing(20); | |
} | |
else | |
{ | |
MoistureMessage = ""; | |
BrainPad.Buzzer.StopBuzzing(); | |
} | |
} | |
return MoistureMessage; | |
} | |
//Function that creates a message for the user if it is too dark | |
public String LightOutput(Double Light) | |
{ | |
String LightMessage; | |
if (Light < Plantbot.LightDarknessThreshold) | |
{ | |
LightMessage = "It's too dark!"; | |
} | |
else | |
{ | |
LightMessage = ""; | |
} | |
return LightMessage; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment