private void Timer_Tick(ThreadPoolTimer timer) { try { if (button.CurrentState != buttonState) { buttonState = button.CurrentState; blueLed.ChangeState(buttonState); buzzer.ChangeState(buttonState); } actualAmbientLight = lightSensor.SensorValue(); if (actualAmbientLight < ambientLightThreshold) { brightness = Map(ambientLightThreshold - actualAmbientLight, 0, ambientLightThreshold, 0, 255); } else { brightness = 0; } redLed.AnalogWrite(Convert.ToByte(brightness)); byte rgbVal = Convert.ToByte(brightness); display.SetBacklightRgb(rgbVal, rgbVal, 255); display.SetText(String.Format("Thingy\nLight: {0}", actualAmbientLight)); } catch (Exception ex) { System.Diagnostics.Debug.Write("Something happened: " + ex.ToString()); throw; } }
void MainPageLoaded(object sender, RoutedEventArgs e) { IAsyncAction asyncAction = Windows.System.Threading.ThreadPool.RunAsync(async(workItem) => { while (true) { try { //led.AnalogWrite(getAnalogOut()); if (GroveButton.CurrentState == SensorStatus.On) { while (GroveButton.CurrentState == SensorStatus.On) { ; } ledStatus = !ledStatus; System.Diagnostics.Debug.WriteLine("LED light status: " + (ledStatus ? "On" : "Off")); } if (ledStatus) { RotaryDegree = Convert.ToInt16(GroveRotary.Degrees()); byte analogOut = (byte)(RotaryDegree * 255 / 300); GroveLed.AnalogWrite(analogOut); } else { GroveLed.AnalogWrite(0); } // System.Diagnostics.Debug.WriteLine("AnalogOut is " + getAnalogOut()); } catch (Exception ex) { // If you want to see the exceptions uncomment the following: System.Diagnostics.Debug.WriteLine(ex.ToString()); } await Dispatcher.RunAsync(CoreDispatcherPriority.High, () => { Text_AngleSensor.Text = "GroveRotaryAngle: " + RotaryDegree.ToString(); Text_LEDStatus.Text = "LED Status: " + (ledStatus ? "On" : "Off"); }); } }); }
public void Run(IBackgroundTaskInstance taskInstance) { // Connect the Rotary Angle Sensor to analog port 2 IRotaryAngleSensor potentiometer = DeviceFactory.Build.RotaryAngleSensor(Pin.AnalogPin2); // Connect the LED to digital port 5 ILed led = DeviceFactory.Build.Led(Pin.DigitalPin5); // Create a variable to track the LED brightness double brightness = 0; // Capture the current value from the Rotary Angle sensor double angle = 0; // Loop endlessly while (true) { try { // Capture the current value from the Rotary Angle sensor angle = potentiometer.SensorValue(); // Output the agle to the Output Window System.Diagnostics.Debug.WriteLine("Angle is " + angle.ToString()); // If the Rotary Angle sensor value is greater than zero... if (angle > 0) { // Divide the angle (a 10-bit value from 0-1023) by four // to get a single byte value value from 0-255. brightness = Math.Floor(angle / 4); } else { // If the angle is zero, set the brightness to zero brightness = 0; } // Output the brightness to the Output Window System.Diagnostics.Debug.WriteLine("Brightness is " + brightness.ToString()); // AnalogWrite uses Pulse WIdth Modulation (PWM) to // control the brightness of the digital LED. led.AnalogWrite(Convert.ToByte(brightness)); } catch (Exception ex) { // NOTE: There are frequent exceptions of the following: // WinRT information: Unexpected number of bytes was transferred. Expected: '. Actual: '. // This appears to be caused by the rapid frequency of writes to the GPIO // These are being swallowed here/ // If you want to see the exceptions uncomment the following: // System.Diagnostics.Debug.WriteLine(ex.ToString()); } } }
public async void Run(IBackgroundTaskInstance taskInstance) { Debug.WriteLine("Application is running!"); var DeviceConnectionString = "NithinsPI.azure-devices.net"; var DeviceId = "NithinsPI"; var DeviceKey = "3/Di9ndJYFzf5JphzW8YzRM7HebOhKoUGoqmK6Xh/cY="; var device = DeviceClient.Create(DeviceConnectionString, AuthenticationMethodFactory.CreateAuthenticationWithRegistrySymmetricKey(DeviceId, DeviceKey), TransportType.Http1); IDHTTemperatureAndHumiditySensor sensor = DeviceFactory.Build.DHTTemperatureAndHumiditySensor(Pin.DigitalPin4, DHTModel.Dht11); ILed greenLed = DeviceFactory.Build.Led(Pin.DigitalPin5); while (true) { BackgroundTaskDeferral deferral = taskInstance.GetDeferral(); greenLed.AnalogWrite(Convert.ToByte(255)); sensor.Measure(); string sensortemp = sensor.TemperatureInCelsius.ToString(); string sensorhum = sensor.Humidity.ToString(); var telemetry = new Telemetry { Temperature = sensortemp, Humidity = sensorhum }; var payLoad = JsonConvert.SerializeObject(telemetry); var message = new Message(Encoding.ASCII.GetBytes(payLoad)); await device.SendEventAsync(message).ConfigureAwait(false); greenLed.AnalogWrite(Convert.ToByte(0)); await Task.Delay(TimeSpan.FromHours(1)); } }
public void Run(IBackgroundTaskInstance taskInstance) { // Digital Pins 5 and 6 are Pulse Width Modulated (PWM) ILed red = DeviceFactory.Build.Led(Pin.DigitalPin5); ILed blue = DeviceFactory.Build.Led(Pin.DigitalPin6); // Digital port 7 will not PWM. It is purely digital not // Pulse Width Modulated (PWM). ILed white = DeviceFactory.Build.Led(Pin.DigitalPin7); // We will cycle brightness in a while loop. Type is int // so we can perform arithmetic on it. int brightness = 0; // Loop endlessly while (true) { try { System.Diagnostics.Debug.WriteLine("Brightness: " + brightness.ToString()); Task.Delay(100).Wait(); // Delay 0.1 second // Check the brightness, if it's going to overflow, reset it. if (brightness > 250) { brightness = 0; } // Increase the brightness by 5 points. brightness = brightness + 5; // Write the values to the three LEDs. // USA! Red, White, and Blue! red.AnalogWrite(Convert.ToByte(brightness)); blue.AnalogWrite(Convert.ToByte(brightness)); white.AnalogWrite(Convert.ToByte(brightness)); } catch (Exception ex) { // NOTE: There are frequent exceptions of the following: // WinRT information: Unexpected number of bytes was transferred. Expected: '. Actual: '. // This appears to be caused by the rapid frequency of writes to the GPIO // These are being swallowed here/ // If you want to see the exceptions uncomment the following: // System.Diagnostics.Debug.WriteLine(ex.ToString()); } } }
private void Timer_Tick(ThreadPoolTimer timer) { try { // Capture the current value from the Light Sensor actualAmbientLight = lightSensor.SensorValue(); // If the actual light measurement is lower than the defined threshold // then define the LED brightness based on the delta between the actual // ambient light and the threshold value if (actualAmbientLight < ambientLightThreshold) { // Use a range mapping method to conver the difference between the // actual ambient light and the threshold to a value between 0 and 255 // (the 8-bit range of the LED on D6 - a PWM pin). // If actual ambient light is low, the differnce between it and the threshold will be // high resulting in a high brightness value. brightness = Map(ambientLightThreshold - actualAmbientLight, 0, ambientLightThreshold, 0, 255); } else { // If the actual ambient light value is above the threshold then // the LED should be completely off. Set the brightness to 0 brightness = 0; } // AnalogWrite uses Pulse Width Modulation (PWM) to // control the brightness of the digital LED on pin D6. redLed.AnalogWrite(Convert.ToByte(brightness)); } catch (Exception ex) { // NOTE: There are frequent exceptions of the following: // WinRT information: Unexpected number of bytes was transferred. Expected: '. Actual: '. // This appears to be caused by the rapid frequency of writes to the GPIO // These are being swallowed here // If you want to see the exceptions uncomment the following: // System.Diagnostics.Debug.WriteLine(ex.ToString()); } }
private void Timer_Tick(ThreadPoolTimer timer) { try { actualAmbientLight = sensor.SensorValue(); if (actualAmbientLight < ambientLightThreshold) { brightness = Map(ambientLightThreshold - actualAmbientLight, 0, ambientLightThreshold, 0, 255); } else { brightness = 0; } blueLed.AnalogWrite(Convert.ToByte(brightness)); } catch (Exception ex) { System.Diagnostics.Debug.Write("Something happened:" + ex.ToString()); throw; } }
private void Timer_Tick(ThreadPoolTimer timer) { try { // Capture the current ambient noise level soundLevel = soundSensor.SensorValue(); // Check the button state if (button.CurrentState == SensorStatus.On) { // If the button is depressed, turn on the blue LED // and activate the buzzer buzzer.ChangeState(SensorStatus.On); blueLed.ChangeState(SensorStatus.On); // For debugging purposes, log a console message System.Diagnostics.Debug.WriteLine("**** BUTTON ON ****"); } else if (buzzer.CurrentState == SensorStatus.On || blueLed.CurrentState == SensorStatus.On) { // Turn the buzzer and LED off buzzer.ChangeState(SensorStatus.Off); blueLed.ChangeState(SensorStatus.Off); } // Capture the current value from the Light Sensor actualAmbientLight = lightSensor.SensorValue(); // If the actual light measurement is lower than the defined threshold // then define the LED brightness based on the delta between the actual // ambient light and the threshold value if (actualAmbientLight < ambientLightThreshold) { // Use a range mapping method to conver the difference between the // actual ambient light and the threshold to a value between 0 and 255 // (the 8-bit range of the LED on D6 - a PWM pin). // If actual ambient light is low, the differnce between it and the threshold will be // high resulting in a high brightness value. brightness = Map(ambientLightThreshold - actualAmbientLight, 0, ambientLightThreshold, 0, 255); } else { // If the actual ambient light value is above the threshold then // the LED should be completely off. Set the brightness to 0 brightness = 0; } // AnalogWrite uses Pulse Width Modulation (PWM) to // control the brightness of the digital LED on pin D6. redLed.AnalogWrite(Convert.ToByte(brightness)); // Use the brightness value to control the brightness of the RGB LCD backlight byte rgbVal = Convert.ToByte(brightness); display.SetBacklightRgb(rgbVal, rgbVal, rgbVal); // Updae the RGB LCD with the light and sound levels display.SetText(String.Format("Thingy\nL:{0} S:{1}", actualAmbientLight, soundLevel)); } catch (Exception ex) { // NOTE: There are frequent exceptions of the following: // WinRT information: Unexpected number of bytes was transferred. Expected: '. Actual: '. // This appears to be caused by the rapid frequency of writes to the GPIO // These are being swallowed here // If you want to see the exceptions uncomment the following: // System.Diagnostics.Debug.WriteLine(ex.ToString()); } }
public void Run(IBackgroundTaskInstance taskInstance) { // // TODO: Insert code to perform background work // // If you start any asynchronous methods here, prevent the task // from closing prematurely by using BackgroundTaskDeferral as // described in http://aka.ms/backgroundtaskdeferral // // Build Sensors IButtonSensor Button = DeviceFactory.Build.ButtonSensor(Pin.DigitalPin6); ILed Red = DeviceFactory.Build.Led(Pin.DigitalPin2); ILed Blue = DeviceFactory.Build.Led(Pin.DigitalPin3); ILed Green = DeviceFactory.Build.Led(Pin.DigitalPin4); IRotaryAngleSensor Potentiometer = DeviceFactory.Build.RotaryAngleSensor(Pin.AnalogPin0); // Set initial values int State = 0; double Speed = 100; while (true) { Speed = Potentiometer.SensorValue(); // Speed can be adjusted between 0-1023 // Adjust values for a range between 100-1000 if (Speed < 100) { Speed = 100; } if (Speed > 1000) { Speed = 1000; } //Get button state string buttonon = Button.CurrentState.ToString(); bool buttonison = buttonon.Equals("On", StringComparison.OrdinalIgnoreCase); if (buttonison) { // Turn off all Leds and then turn on current Led Red.AnalogWrite(Convert.ToByte(0)); Green.AnalogWrite(Convert.ToByte(0)); Blue.AnalogWrite(Convert.ToByte(0)); switch (State) { case 0: Red.AnalogWrite(Convert.ToByte(255)); break; case 1: Blue.AnalogWrite(Convert.ToByte(255)); break; case 2: Green.AnalogWrite(Convert.ToByte(255)); break; } // If State is above 2 reset loop else add 1 if (State == 2) { State = 0; } else { State++; } } // Delay the task according to the Potentiometer value Task.Delay((int)Speed).Wait(); } }
private void OnTick(object sender, object e) { try { // Check the value of the button. string buttonon = button.CurrentState.ToString(); // bool buttonison = buttonon.Equals("On", StringComparison.OrdinalIgnoreCase); System.Diagnostics.Debug.WriteLine("Button is " + buttonon); } catch (Exception ex) { // NOTE: There are frequent exceptions of the following: // WinRT information: Unexpected number of bytes was transferred. Expected: '. Actual: '. // This appears to be caused by the rapid frequency of writes to the GPIO // These are being swallowed here/ // If you want to see the exceptions uncomment the following: // System.Diagnostics.Debug.WriteLine(ex.ToString()); } try { System.Diagnostics.Debug.WriteLine("Brightness: " + brightness.ToString()); // Check the brightness, if it's going to overflow, reset it. if (brightness > 250) { brightness = 0; } // Increase the brightness by 5 points. brightness = brightness + 5; // Write the values to the three LEDs. // USA! Red, White, and Blue! Led1.AnalogWrite(Convert.ToByte(brightness)); } catch (Exception ex) { // NOTE: There are frequent exceptions of the following: // WinRT information: Unexpected number of bytes was transferred. Expected: '. Actual: '. // This appears to be caused by the rapid frequency of writes to the GPIO // These are being swallowed here/ // If you want to see the exceptions uncomment the following: // System.Diagnostics.Debug.WriteLine(ex.ToString()); } try { // Check the value of the button. string buttonon = button.CurrentState.ToString(); bool buttonison = buttonon.Equals("On", StringComparison.OrdinalIgnoreCase); // Check the state of the buzzer. This is just to output to debug! SensorStatus status = buzzer.CurrentState; bool buzzeron = status.ToString().Equals("On", StringComparison.OrdinalIgnoreCase); // Print out Diagnostics. System.Diagnostics.Debug.WriteLine("Button is " + buttonon); System.Diagnostics.Debug.WriteLine("Buzzer is " + status.ToString()); // If the Button is on . . . . if (buttonison) { buzzer.ChangeState(GrovePi.Sensors.SensorStatus.On); } else { buzzer.ChangeState(GrovePi.Sensors.SensorStatus.Off); } try { // Check the value of the button, turn it into a string. string sensorvalue = light1.SensorValue().ToString(); System.Diagnostics.Debug.WriteLine("light is " + sensorvalue); } catch (Exception ex) { // NOTE: There are frequent exceptions of the following: // WinRT information: Unexpected number of bytes was transferred. Expected: '. Actual: '. // This appears to be caused by the rapid frequency of writes to the GPIO // These are being swallowed here/ // If you want to see the exceptions uncomment the following: // System.Diagnostics.Debug.WriteLine(ex.ToString()); } } catch (Exception ex) { // NOTE: There are frequent exceptions of the following: // WinRT information: Unexpected number of bytes was transferred. Expected: '. Actual: '. // This appears to be caused by the rapid frequency of writes to the GPIO // These are being swallowed here/ // If you want to see the exceptions uncomment the following: // System.Diagnostics.Debug.WriteLine(ex.ToString()); } //Task.Delay(100).Wait(); // Delay 0.1 second // We need to make sure we delay here. If we don't, we won't be able to read // the LCD Screen. try { // First, output to the LCD Display. display.SetText("Light:" + light1.SensorValue()).SetBacklightRgb(255, 50, 255); // Then output to the debug window. System.Diagnostics.Debug.WriteLine("Hello from Dexter Industries!"); } catch (Exception ex) { // NOTE: There are frequent exceptions of the following: // WinRT information: Unexpected number of bytes was transferred. Expected: '. Actual: '. // This appears to be caused by the rapid frequency of writes to the GPIO // These are being swallowed here/ // If you want to see the exceptions uncomment the following: // System.Diagnostics.Debug.WriteLine(ex.ToString()); } try { // Check the value of the Ultrasonic Sensor string sensorvalue = distance1.MeasureInCentimeters().ToString(); System.Diagnostics.Debug.WriteLine("Ultrasonic reads " + sensorvalue); } catch (Exception ex) { // NOTE: There are frequent exceptions of the following: // WinRT information: Unexpected number of bytes was transferred. Expected: '. Actual: '. // This appears to be caused by the rapid frequency of writes to the GPIO // These are being swallowed here/ // If you want to see the exceptions uncomment the following: // System.Diagnostics.Debug.WriteLine(ex.ToString()); } }
//------------------------------------------------------------------------------------------------------------------------ #endregion #region Functions //------------------------------------------------------------------------------------------------------------------------ public void SetBrightness(int val) { led.AnalogWrite((byte)val); }