public static void Main() { int tempInput = 0; while (true) { tempInput = tempSensor.Read(); float volts = ((float)tempInput / 1024.0f) * 3.3f; float temp_celcius = (volts * 100); float temp_fahrenheit = (temp_celcius * 9) / 5 + 32; if (temp_celcius > 24.0f) { speaker.SetPulse((uint)(1000000 / 2217.46f), (uint)(1000000 / 2217.46f) / 2); } else { speaker.SetDutyCycle(0); } Debug.Print("Celcius: " + temp_celcius.ToString() + " - " + "Fahrenheit: " + temp_fahrenheit.ToString()); Thread.Sleep(1000); } }
public static void Main() { SLH.AnalogInput pot = new SLH.AnalogInput(Pins.GPIO_PIN_A1); PWM redLed = new PWM(PWMChannels.PWM_PIN_D11, 100, 0, false); PWM greenLed = new PWM(PWMChannels.PWM_PIN_D10, 100, 0, false); PWM blueLed = new PWM(PWMChannels.PWM_PIN_D9, 100, 0, false); double dutyCycleMax = .3; // RGB Led doesn't seem to get much brighter than at 30% int hue = 0; // set our range to be the range of possible hues pot.SetRange(0, 360); redLed.Start(); greenLed.Start(); blueLed.Start(); while (true) { double r, g, b; hue = pot.Read(); Debug.Print("Hue: " + hue.ToString()); HsvToRgb(hue, 1, 1, out r, out g, out b); redLed.DutyCycle = (r * dutyCycleMax); greenLed.DutyCycle = (g * dutyCycleMax); blueLed.DutyCycle = (b * dutyCycleMax); } }
public static void Main() { int tempInput = 0; while (true) { //Read the raw sensor value tempInput = tempSensor.Read(); //Debug.Print("Reading :" + tempSensor.Read().ToString()); //The read value is from 0-1024, so this converts it to a % then //multiplies by 3.3v to get the real voltage that was read. float volts = ((float)tempInput / 1024.0f) * 3.3f; //The datasheet for the sensor indicates there's a 500mV (half a volt) //offset, this is to allow it to read under 0 degrees //float temp = (volts - 0.5f); //Finally, every 10mV indicates 1 degree Celcius, so multiply by 100 //to convert the voltage to a reading ///float temp = temp * 100; //float temp = (5.0f * tempInput * 100.0f) / 1024.0f; float temp_celcius = (volts * 100); float temp_fahrenheit = (temp_celcius * 9) / 5 + 32; Debug.Print("Celcius: " + temp_celcius.ToString() + " - Fahrenheit: " + temp_fahrenheit.ToString()); //Debug.Print("Celcius: " + temp.ToString()); Thread.Sleep(1000); } }
public static void Main() { // write your code here int anaIn = 250; // ADC is 12-bit Resolution //SecretLabs.NETMF.Hardware.AnalogInput pinA4 = new Microsoft.NETMF.Hardware.AnalogInput(Cpu.AnalogChannel.ANALOG_4); SecretLabs.NETMF.Hardware.AnalogInput pinA4 = new SecretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A4); OutputPort led = new OutputPort(Pins.ONBOARD_LED, false); //PWM led2 = new PWM(Cpu.PWMChannel.PWM_0, 100, 100, 0); PWM servo = new PWM(Cpu.PWMChannel.PWM_0,20000,1500,PWM.ScaleFactor.Microseconds, false); servo.Start(); servo.DutyCycle = 0; servo.Duration = (uint)1500; while (true) { double anaRead = pinA4.Read(); anaIn = (int)anaRead; //LED stuff //led.Write(true); // turn on the LED //Thread.Sleep(anaIn); // sleep for 250ms //led.Write(false); // turn off the LED //Thread.Sleep(anaIn); // sleep for 250ms servo.Duration = (uint)((1000+anaRead)); } }
public float ReadVoltage() { int intValue = analogInput.Read(); float voltageValue = ((float)intValue) * VOLTS_PER_COUNT_FLOAT; return(voltageValue); }
private static void ManageBackLight(SecretLabs.NETMF.Hardware.AnalogInput lightPort) { using (var lcd = new KanaLCD(0x38, 2, 40)) { // 暗かったらバックライトを自動で消す lcd.BackLight = lightPort.Read() < 900 || backLightCount-- > 0; } }
float ReadRaw() { int humidityRaw = 0; _digitalPort.Write(true); Thread.Sleep(5); humidityRaw = _analogPort.Read(); _digitalPort.Write(false); return(humidityRaw); }
protected int ReadAverageDistance(SecretLabs.NETMF.Hardware.AnalogInput analogInput) { var count = AverageMeasurementCount; var total = 0; while (--count >= 0) { total += analogInput.Read(); } return(total / AverageMeasurementCount); }
public static void Main() { float Voltage = 0; SecretLabs.NETMF.Hardware.AnalogInput ADC1 = new SecretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A0); while(true) { Voltage = (float)(3.3 * ADC1.Read() / 1024); Debug.Print("voltage is "+Voltage.ToString()+"V"); Thread.Sleep(100); }// write your code here }
public float Read() { int sample; _digitalPort.Write(true); Thread.Sleep(5); sample = _analogPort.Read(); _digitalPort.Write(false); Moisture = 100 - Map(sample, 0, 1023, 0, 100); return(Moisture); }
public float Read() { int sample; float humidity; _digitalPort.Write(true); Thread.Sleep(5); sample = _analogPort.Read(); _digitalPort.Write(false); humidity = 100 - Map(sample, 250, 1023, 0, 100); return(humidity); }
public GUVA_Reading Read() { int sample = 0; var sensorReading = _analogPort.Read(); double sensorVoltage = (double)sensorReading / 1024 * _powerVoltage; int uvIndex = (int)(sensorVoltage / 0.1); return(new GUVA_Reading { sensorReading = sensorReading, sensorVoltage = sensorVoltage, uvIndex = uvIndex }); }
public float ReadHumidity() { int intValue = analogInput.Read(); float readVoltage = ((float)intValue) * VOLTS_PER_COUNT_FLOAT; float sensorVoltage = readVoltage * (1 / VOLTAGE_DIVIDER_RATIO); //VOUT=(VSUPPLY)(0.0062(sensor RH) + 0.16), typical at 25 ºC //True RH = (Sensor RH)/(1.0546 – 0.00216T), T in ºC float sensorRH = (sensorVoltage - (0.16f * VOLTAGE_SUPPLY_FLOAT)) / (0.0062f * VOLTAGE_SUPPLY_FLOAT); return(sensorRH); }
public Humidity Read() { int sample; float humidity; _digitalPort.Write(true); Thread.Sleep(20); sample = _analogPort.Read(); _digitalPort.Write(false); humidity = 100 - Map(sample, 250, 1023, 0, 100); return(new Humidity { raw = sample, mapped = humidity }); }
public GUVA_Reading Read() { int sample = 0; var sensorReading = _analogPort.Read(); double sensorVoltage = (double)sensorReading / 1024 * _powerVoltage; int uvIndex = (int)(sensorVoltage / 0.1); Debug.Print("GUVAS12SD: sensorReading=" + sensorReading + ", sensorVoltage=" + sensorVoltage + ", uvIndex = " + uvIndex); return(new GUVA_Reading { sensorReading = sensorReading, sensorVoltage = sensorVoltage, uvIndex = uvIndex }); }
public static void Main() { // write your code here int value = 0; //lightSensor.SetRange(0, 10); while (true) { value = lightSensor.Read(); Debug.Print(value.ToString()); Thread.Sleep(1000); } }
public static void Main() { // Toggle light when shield button is pressed. /*OutputPort led = new OutputPort(Pins.GPIO_PIN_D0, false); * InputPort button = new InputPort(Pins.GPIO_PIN_D1, false, Port.ResistorMode.PullUp); * bool buttonState = false; * * while (true) * { * buttonState = !button.Read(); * led.Write(buttonState); * }*/ // Measure voltage from potentiometer OutputPort led = new OutputPort(Pins.GPIO_PIN_D0, false); var pot = new SecretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A0); //need to inport SecretLabs.NETMF.Hardware.AnalogInput reference. int potValue = 0; while (true) { //read the potentiometer value potValue = pot.Read(); Debug.Print(potValue.ToString()); //limit the range of the potentiometer pot.SetRange(100, 250); //blink value based on value (0-1023 ms) led.Write(true); Thread.Sleep(potValue); led.Write(false); Thread.Sleep(potValue); } }
public static void Main() { // write your code here int anaIn = 250; // ADC is 12-bit Resolution //SecretLabs.NETMF.Hardware.AnalogInput pinA4 = new Microsoft.NETMF.Hardware.AnalogInput(Cpu.AnalogChannel.ANALOG_4); SecretLabs.NETMF.Hardware.AnalogInput pinA4 = new SecretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A4); OutputPort led = new OutputPort(Pins.ONBOARD_LED, false); //PWM led2 = new PWM(Cpu.PWMChannel.PWM_0, 100, 100, 0); PWM servo = new PWM(Cpu.PWMChannel.PWM_0, 20000, 1500, PWM.ScaleFactor.Microseconds, false); servo.Start(); servo.DutyCycle = 0; servo.Duration = (uint)1500; while (true) { double anaRead = pinA4.Read(); anaIn = (int)anaRead; //LED stuff //led.Write(true); // turn on the LED //Thread.Sleep(anaIn); // sleep for 250ms //led.Write(false); // turn off the LED //Thread.Sleep(anaIn); // sleep for 250ms servo.Duration = (uint)((1000 + anaRead)); } }
public static void Main() { //Init MyRoom = new Room(); var Beep = false; // write your code here var Led = new OutputPort(Pins.GPIO_PIN_D3, false); var lightSensor = new LightSensor(Pins.GPIO_PIN_A0); var TempSensor = new Dht11Sensor(Pins.GPIO_PIN_D0, Pins.GPIO_PIN_D1, PullUpResistor.Internal); var SoundSensor = new SecretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A1); //SoundSensor.SetRange(0, 100); var GasSensor = new SecretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A2); //GasSensor.SetRange(0, 100); var PirSensor = new InputPort(Pins.GPIO_PIN_D2, false, Port.ResistorMode.PullUp); var PirState = false; //waiting till connect... if (!Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].IsDhcpEnabled) { // using static IP while (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) ; // wait for network connectivity } else { // using DHCP while (IPAddress.GetDefaultLocalAddress() == IPAddress.Any) ; // wait for DHCP-allocated IP address } //Debug print our IP address Debug.Print("Device IP: " + Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].IPAddress); //setup mqtt client client = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS)); string clientId = Guid.NewGuid().ToString(); client.Connect(clientId); SubscribeMessage(); while (true) { Beep = !Beep; MyRoom.Light = lightSensor.ReadLightSensor(); if (TempSensor.Read()) { MyRoom.Temperature = TempSensor.Temperature; MyRoom.Humidity = TempSensor.Humidity; Debug.Print("DHT sensor Read() ok, RH = " + MyRoom.Humidity.ToString("F1") + "%, Temp = " + MyRoom.Temperature.ToString("F1") + "°C " + (MyRoom.Temperature * 1.8 + 32).ToString("F1") + "°F"); } MyRoom.Sound = SoundSensor.Read(); Debug.Print("Suara : " + MyRoom.Sound); MyRoom.Gas = GasSensor.Read(); Debug.Print("Gas Lpg : " + MyRoom.Gas); var Pir = PirSensor.Read(); if (Pir) { if (PirState == false) { Debug.Print("Ada gerakan."); PirState = true; MyRoom.Movement = true; } } else { if (PirState) { Debug.Print("Gerakan berhenti."); PirState = false; MyRoom.Movement = false; } } Thread.Sleep(1000); //Buzzer.Write(Beep); Led.Write(Beep); //update status PublishMessage("/home/status", JsonSerializer.SerializeObject(MyRoom)); } }
//RFID //private static idReader _idReader; //private static void StartRFID() //{ // _idReader = new idReader(SerialPorts.COM1); // _idReader.RfidEvent += new idReader.RfidEventDelegate(_idReader_RfidEvent); //} //RFID //static void _idReader_RfidEvent(object sender, idReader.RfidEventArgs e) //{ // Debug.Print("Card scanned: " + e.RFID); // sendDataUsingCOMPort("CARD=" + e.RFID); //} public static void Main() { //MOTION SENSOR //port.OnInterrupt += new NativeEventHandler(motion_OnInterrupt); //port.EnableInterrupt(); //UART InputPort button = new InputPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled); bool ButtonState = false; spUART.DataReceived += new SerialDataReceivedEventHandler(receivedDataUsingCOMPort); spUART.Open(); //RFID //StartRFID(); //TEMPERATURE SENSOR //SecretLabs.NETMF.Hardware.AnalogInput temp = new SecretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A0); //const double EFSR = 3.3; //const int N = 1023; int adcValue; //double Q = 0.0; //double sensorVoltage = 0.0, tempC = 0.0; //ULTRASONIC SENSOR double distanceInCm = 0.0; //double Kelvin; //double Fahrenheit; //double Newton; while (true) { //MOTION SENSOR //if (MovementDetected == true) //{ // Debug.Print("Motion detected! Alarm sounding"); // sendDataUsingCOMPort("MOTION STATUS=" + "MOTION DETECTED!"); // Thread.Sleep(5000); //} ButtonState = button.Read(); if (ButtonState == true) { sendDataUsingCOMPort("PUSH BUTTON STATUS=" + "Push button pressed, activating Camera"); } adcValue = sensor.Read(); //Q = EFSR / N; //sensorVoltage = adcValue * Q; //tempC = 100 * (sensorVoltage - 0.5); //Kelvin = tempC + 273; //Fahrenheit = tempC * 18 / 10 + 32; //Newton = tempC * 33 / 100; distanceInCm = adcValue * 0.5 * 2.5; string UltrasonicStatus; Debug.Print("Distance: " + distanceInCm.ToString("N2")); Thread.Sleep(2000); sendDataUsingCOMPort("DISTANCE IN CM=" + distanceInCm.ToString("N2")); Thread.Sleep(3000); if (distanceInCm < 30) { UltrasonicStatus = "You turned the lights and switches ON"; } if (distanceInCm > 30 && distanceInCm < 50) { UltrasonicStatus = "Move your hand a little closer to turn lights and switches ON"; } if (distanceInCm > 51) { UltrasonicStatus = "Unreachable distance, lights and switches OFF"; } } }
public static void Main() { //InputPort button = new InputPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled); //bool ButtonState = false; //MOTION SENSOR //port.OnInterrupt += new NativeEventHandler(motion_OnInterrupt); //port.EnableInterrupt(); //UART spUART.DataReceived += new SerialDataReceivedEventHandler(receivedDataUsingCOMPort); spUART.Open(); //RFID StartRFID(); //TEMPERATURE SENSOR SecretLabs.NETMF.Hardware.AnalogInput temp = new SecretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A0); const double EFSR = 3.3; const int N = 1023; int adcValue = 0; double Q = 0.0; double sensorVoltage = 0.0, tempC = 0.0; //ULTRASONIC SENSOR double distanceInCm = 0.0; double Kelvin; double Fahrenheit; double Newton; while (true) { //MOTION SENSOR //if (MovementDetected == true) //{ // Debug.Print("Motion detected! Alarm sounding"); // sendDataUsingCOMPort("MOTION STATUS=" + "MOTION DETECTED!"); // Thread.Sleep(5000); //} adcValue = temp.Read(); Q = EFSR / N; sensorVoltage = adcValue * Q; tempC = (100 * (sensorVoltage - 0.5)) + 20; Kelvin = tempC + 273; Fahrenheit = tempC * 18 / 10 + 32; Newton = tempC * 33 / 100; Debug.Print("Temperature: " + tempC.ToString("N2")); //ButtonState = button.Read(); //if (ButtonState == true) //{ // sendDataUsingCOMPort("PUSH BUTTON STATUS=" + "Push button pressed, activating Camera"); //} string Status; if (tempC > 30) { Status = "Hot, on air conditioner"; } else if (tempC > 28) { Status = "Warm, on fan at high speed"; } else if (tempC > 26) { Status = "Cool, set fan to low speed or turn off"; } else { Status = "Very cool, turn off additional cooling"; } Debug.Print("Temp in C is: " + tempC.ToString("N2")); Debug.Print(Status); sendDataUsingCOMPort("°C=" + tempC.ToString("N2") + "\n" + "Status = " + Status + "\n" + "°K=" + Kelvin.ToString("N2") + "\n" + "°N=" + Fahrenheit.ToString("N2") + "\n" + "°N=" + Newton.ToString("N2") + "\n" + "--------------------------------------------------------------------------------"); //sendDataUsingCOMPort("DISTANCE=" + distanceInCm.ToString("N2") + "\n" + "Status = " + UltrasonicStatus + "\n"); Thread.Sleep(5000); } }
static void ReadVoltage() { var analogInput = new SecretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A0); while (true) { // this will give us a reading of a percentage of 3.3 v (0 - 1023 since 10 bit ADC) int reading = analogInput.Read(); double voltage = (3.3 / 1023) * (double)reading; Debug.Print("Current reading = " + voltage); Thread.Sleep(1000); } }
static void ReadVoltageWithRange() { var analogInput = new SecretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A0); analogInput.SetRange(0, 3300); while (true) { int reading = analogInput.Read(); double voltage = (double)reading / 1000; Debug.Print("Current reading = " + voltage); Thread.Sleep(1000); } }
public static void Main() { InputPort button = new InputPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled); bool ButtonState = false; //UART spUART.DataReceived += new SerialDataReceivedEventHandler(receivedDataUsingCOMPort); spUART.Open(); //RFID StartRFID(); //MOTION SENSOR port.OnInterrupt += new NativeEventHandler(motion_OnInterrupt); port.EnableInterrupt(); //TEMPERATURE SENSOR SecretLabs.NETMF.Hardware.AnalogInput temp = new SecretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A0); const double EFSR = 3.3; const int N = 1023; int adcValue = 0; int adcValueUltrasonic = 0; double Q = 0.0; double sensorVoltage = 0.0, tempC = 0.0; double distanceInCm = 0.0; double Kelvin = 0.0; double Fahrenheit = 0.0; double Newton = 0.0; string UltrasonicStatus = ""; string Status; while (true) { //MOTION SENSOR if (MovementDetected) { MovementDetected = false; } // Debug.Print("Motion detected! Alarm sounding"); // sendDataUsingCOMPort("MOTION STATUS=" + "MOTION DETECTED!"); // Thread.Sleep(5000); //} adcValueUltrasonic = sensor.Read(); adcValue = temp.Read(); Q = EFSR / N; distanceInCm = adcValue * 0.5 * 2.5; sensorVoltage = adcValueUltrasonic * Q; tempC = System.Math.Abs(100 * (sensorVoltage - 0.5)); Kelvin = System.Math.Abs(tempC + 273); Fahrenheit = System.Math.Abs(tempC * 18 / 10 + 32); Newton = System.Math.Abs(tempC * 33 / 100); Debug.Print("Distance: " + distanceInCm.ToString("N2")); Debug.Print("Temperature: " + tempC.ToString("N2")); ButtonState = button.Read(); if (ButtonState == true) { speaker.SetDutyCycle(50); uint duration1 = 3000, duration2 = 2000; Debug.Print("Alarm sounding"); for (int i = 0; i < 2; i++) { speaker.SetPulse(duration1 * 2, duration1); Thread.Sleep(200); speaker.SetPulse(duration2 * 2, duration2); Thread.Sleep(200); speaker.SetDutyCycle(0); } sendDataUsingCOMPort("DOORBELL=" + "Push button pressed, activating Camera"); } UltrasonicStatus = ""; if (distanceInCm < 30) { UltrasonicStatus = "You turned the lights and switches ON"; } if (distanceInCm > 30 && distanceInCm < 50) { UltrasonicStatus = "Move your hand a little closer to turn lights and switches ON"; } if (distanceInCm > 51) { UltrasonicStatus = "Unreachable distance, lights and switches OFF"; } if (tempC > 30) { Status = "Hot, on air conditioner"; //Status = "> } if (tempC > 28) { Status = "Warm, on fan at high speed"; } if (tempC > 26) { Status = "Cool, set fan to low speed or turn off"; } else { Status = "Very cool, turn off additional cooling"; } Debug.Print("Temp in C is: " + tempC.ToString("N2")); Debug.Print(Status); sendDataUsingCOMPort("°C=" + tempC.ToString("N2") + "\n" + "STATUS=" + Status + "\n" + "°K=" + Kelvin.ToString("N2") + "\n" + "°F=" + Fahrenheit.ToString("N2") + "\n" + "°N=" + Newton.ToString("N2") + "\n"); sendDataUsingCOMPort("DIST CM=" + distanceInCm.ToString("N2") + "\n" + "STATUS=" + UltrasonicStatus + "\n"); Thread.Sleep(5000); } }