Main controller and lightsaber controllerScheme (main controller)
Scheme (lightsaber controller)
How did I doSound controllerScheme
How did I doDeath Star / Darth Vader Eye ControllerScheme
How did I do
Death Star Controller close-up
Darth Vader eye controller close-upAdditionallySo look the other parts.
Motion sensor, ultrasonic sensor and NeoPixel ring (top to bottom)
One NeoPixel module for testing the eyes of Darth Vader
12V miniature wiring board for all boards
General formCloseup side viewDeath Star / Darth Vader Eye ControllerMain controllerLight sword controller
#include "Controller.h" #include "HC-SR04.h" #include "SensorSmoothing.h" // *** // *** , HC-SR04. // *** #define TRIGGER_PIN D2 #define ECHO_PIN D3 // *** // *** // *** #define MOTION_OUT D4 #define MOTION_ENABLED D5 // *** // *** // *** #define PHOTORESISTOR_PIN A0 // *** // *** // *** #define NUMBER_OF_SAMPLES 5 // *** // *** // *** #define DEFALT_VOLUME 15 // *** // *** , // *** . // *** retained int _volume = DEFALT_VOLUME; // *** // *** . // *** Controller _controller = Controller(); // *** // *** HcSr04 // *** HC-SR04 ( ). // *** HcSr04 _ranging = HcSr04(); // *** // *** // *** . // *** bool _motion = false; // *** // *** // *** . // *** double _distance = 0.0; // *** // *** . // *** int _light = 0; // *** // *** // *** NUMBER_OF_SAMPLES. // *** // *** . // *** SensorSmoothing<double> _distanceSample = SensorSmoothing<double>(NUMBER_OF_SAMPLES); // *** // *** // *** Timer _sensorTimer(1000, onSensorTimer); // *** // *** // *** int _lastEventTime = 0; // *** // *** // *** #define DEFAULT_LIGHT_THRESHOLD 1000 retained int _lightThreshold = DEFAULT_LIGHT_THRESHOLD; bool _lightBelowThreshold = false; bool _deathStarIsOn = false; // *** // *** // *** retained int _minimumEventTime = 20; // retained int _minimumDistance = 127; // void setup() { // *** // *** // *** Particle.publish("Setup", "Started"); // *** // *** // *** STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY)); // *** // *** HC-SR04 ( ) // *** _ranging.begin(TRIGGER_PIN, ECHO_PIN); // *** // *** I2C // *** Wire.begin(); // *** // *** // *** _controller.begin(_volume); // *** // *** D7 // *** // *** pinMode(D7, OUTPUT); digitalWrite(D7, LOW); // *** // *** // *** Particle.variable("motion", _motion); Particle.variable("distance", _distance); Particle.variable("light", _light); // *** // *** . // *** Particle.function("controller", controllerWebApi); Particle.function("parameter", parameterWebApi); Particle.function("test", testWebApi); // *** // *** // *** . // *** _lastEventTime = Time.now(); // *** // *** . // *** _sensorTimer.start(); // *** // *** , // *** . // *** delay(3000); // *** // *** // *** _controller.darthVaderOff(); _controller.lightSaberOff(); _controller.deathStarOff(); // *** // *** // *** Particle.publish("Setup", "Completed"); } void loop() { // *** // *** _lightBelowThreshold // *** bool previousLightBelowThreshold = _lightBelowThreshold; // *** // *** // *** if (_motion) { // *** // *** // *** // *** . // *** if (_light < _lightThreshold) { if (!previousLightBelowThreshold) { Particle.publish("Light", "Below Threshold"); } _lightBelowThreshold = true; // *** // *** . // *** _deathStarIsOn = true; _controller.deathStarOn(); } else { if (previousLightBelowThreshold) { Particle.publish("Light", "Above Threshold"); } _lightBelowThreshold = false; } // *** // *** , . // *** if (_distance < _minimumDistance) { // *** // *** , // *** // *** . // *** if ((Time.now() - _lastEventTime) > _minimumEventTime) { Particle.publish("Object", "Detected"); _lastEventTime = Time.now(); _controller.randomEvent(); } } } else { // *** // *** . // *** if (_deathStarIsOn) { // *** // *** , // *** . // *** _deathStarIsOn = false; _controller.deathStarOff(); } } // *** // *** // *** delay(150); } void onSensorTimer() { // *** // *** // *** bool previousMotion = _motion; // *** // *** . // *** _motion = (digitalRead(MOTION_OUT) == HIGH); // *** // *** // *** . // *** if (!previousMotion && _motion) { Particle.publish("Motion", "Detected"); if (_lightBelowThreshold) { Particle.publish("Light Level", "Below Threshold: " + String(_light) + "/" + String(_lightThreshold)); } else { Particle.publish("Light Level", "Above Threshold: " + String(_light) + "/" + String(_lightThreshold)); } // *** // *** // *** , . // *** Particle.publish("Distance", String(String(_distance / 2.54).toInt()) + " in / " + String(String(_minimumDistance / 2.54).toInt()) + " in"); // *** // *** // *** . // *** Particle.publish("Time", "Last Event: " + String(Time.now() - _lastEventTime) + " second(s) ago"); } else if (previousMotion && !_motion) { Particle.publish("Motion", "Reset"); } // *** // *** // *** // *** _distance = getRangeEx(); // *** // *** // *** _light = analogRead(PHOTORESISTOR_PIN); } // *** // *** -API // *** int testWebApi(String command) { // *** // *** // *** Particle.publish("Test", "Running"); _controller.setVolume(10); delay(500); _controller.darthVaderOn(); delay(1000); _controller.darthVaderVoice(0); delay(18000); _controller.darthVaderOff(); delay(3000); _controller.darthVaderOn(); delay(1000); _controller.darthVaderVoice(1); delay(5000); _controller.darthVaderOff(); delay(3000); _controller.darthVaderOn(); delay(1000); _controller.darthVaderVoice(2); delay(5000); _controller.darthVaderOff(); delay(3000); _controller.darthVaderOn(); delay(1000); _controller.darthVaderVoice(3); delay(5000); _controller.darthVaderOff(); delay(3000); _controller.darthVaderOn(); delay(1000); _controller.darthVaderVoice(4); delay(5000); _controller.darthVaderOff(); delay(3000); _controller.darthVaderOn(); delay(1000); _controller.darthVaderVoice(5); delay(5000); _controller.darthVaderOff(); delay(3000); _controller.darthVaderOn(); delay(1000); _controller.darthVaderVoice(6); delay(5000); _controller.darthVaderOff(); delay(3000); _controller.darthVaderOn(); delay(1000); _controller.darthVaderVoice(7); delay(5000); _controller.darthVaderOff(); delay(3000); _controller.darthVaderOn(); delay(1000); _controller.darthVaderVoice(8); delay(5000); _controller.darthVaderOff(); delay(3000); _controller.lightSaberOn(7000); delay(2000); _controller.deathStarOn(); delay(5000); _controller.deathStarOff(); Particle.publish("Test", "Completed"); return 1; } // *** // *** -API // *** int parameterWebApi(String command) { int returnValue = 0; // *** // *** // *** , , // *** . // *** int equalIndex = command.indexOf("="); // *** // *** // *** . // *** if (equalIndex > 0) { // *** // *** . // *** String parameterName = command.substring(0, equalIndex); String value = command.substring(equalIndex + 1, command.length()); if (parameterName == "volume") { _volume = value.toInt(); _controller.setVolume(_volume); } else if (parameterName == "minimumDistance") { _minimumDistance = value.toInt(); } else if (parameterName == "lightThreshold") { _lightThreshold = value.toInt(); } else if (parameterName == "minimumEventTime") { _minimumEventTime = value.toInt(); } // *** // *** . // *** Particle.publish("Set Parameter", parameterName + " = " + value); // *** // *** . // *** returnValue = value.toInt(); } else if (equalIndex == -1) { // *** // *** , // *** . // *** if (command == "volume") { returnValue = _volume; } else if (command == "minimumDistance") { returnValue = _minimumDistance; } else if (command == "lightThreshold") { returnValue = _lightThreshold; } else if (command == "minimumEventTime") { returnValue = _minimumEventTime; } // *** // *** . // *** Particle.publish("Get Parameter", command); } else if (command == "reset") { // *** // *** . // *** _volume = DEFALT_VOLUME; _controller.setVolume(_volume); _minimumDistance = 50 * 2.54; _lightThreshold = 1000; _minimumEventTime = 20; _lightThreshold = DEFAULT_LIGHT_THRESHOLD; // *** // *** . // *** Particle.publish("Reset Parameters", ""); returnValue = 1; } return returnValue; } // *** // *** -API // *** int controllerWebApi(String command) { int returnValue = 0; // *** // *** . // *** Particle.publish("Controller", command); // *** // *** . // *** returnValue = _controller.executeCommand(command); return returnValue; } double getRangeEx() { double returnValue = 0; // *** // *** . // *** Range range; if (_ranging.getRange(&range)) { // *** // *** . // *** _distanceSample.addSample(range.distance); // *** // *** . // *** returnValue = _distanceSample.average(); } else { // *** // *** . // *** if (range.result == timeFailed) { switch (range.pingTime.result) { case alreadyActive: Particle.publish("Result", "The sensor is busy."); break; case timeout: Particle.publish("Result", "The sensor did not respond."); break; } } else if (range.result == underRange) { switch (range.result) { case underRange: Particle.publish("Result", "The object is too close."); break; case overRange: Particle.publish("Result", "No object is in range."); break; } } else { Particle.publish("Result", "Unknown Error."); } } return returnValue; }
HttpClient
to perform certain functions using Particle.function()
, as shown below. protected async virtual Task<int> Execute(string functionName, string command) { int returnValue = 0; HttpClient httpClient = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage(); var values = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("args", command) }; FormUrlEncodedContent content = new FormUrlEncodedContent(values); request.Headers.Authorization = this.Identity.Authentication; request.Method = this.Method; request.RequestUri = new Uri(this.Identity.BaseUri, string.Format(this.Resource, functionName)); request.Content = content; HttpResponseMessage response = await httpClient.SendAsync(request); if (response.IsSuccessStatusCode) { string json = await response.Content.ReadAsStringAsync(); dynamic obj = JsonConvert.DeserializeObject(json); string value = obj.return_value; returnValue = Convert.ToInt32(value); } else { throw new HttpRequestException(response.ReasonPhrase); } return returnValue; }
Particle.variable()
. protected async override Task<T> Execute(params string[] args) { T returnValue = default(T); HttpClient httpClient = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage(); request.Headers.Authorization = this.Identity.Authentication; request.Method = HttpMethod.Get; request.RequestUri = new Uri(this.Identity.BaseUri, string.Format(this.Resource, args)); HttpResponseMessage response = await httpClient.SendAsync(request); if (response.IsSuccessStatusCode) { string json = await response.Content.ReadAsStringAsync(); dynamic obj = JsonConvert.DeserializeObject(json); string value = obj.result; returnValue = (T)Convert.ChangeType(value, typeof(T)); } else { await this.OnFailed(response); } return returnValue; }
Source: https://habr.com/ru/post/328022/
All Articles