Learning Particle Photon – With a Motion Sensor
This blog post has been written by our 2021 summer intern, Renna. Renna has been a great asset here at Lab651 and we are excited to provide Renna with opportunities to further pursue her passions. Be sure to read part 1. (SciTech Internship Program writeup on Renna.)
~~~~~~~~~
Part 2 – A Servo Motor + HTML
After learning how to use the basic HTML code given to me by Particle, I wanted to figure out how to get my servo motor working with the HTML. Specifically, I want to type in a value on the webpage, have it read into my code, and then turn the servo to that specific degree. I made a few changes to the code from the tutorial; instead of having an on/off button for the LED, I changed it to be an input text box to insert a value and submit to Particle. Adding the input text box was somewhat easy – I did a quick google to ensure I was using the correct syntax to grab a text value. Since I figured that portion out, I didn’t want to stop there, so I added a twist to the LED with multiple inputs. Here are some of the changes I made:
- I changed the digital input on the LED from a high and low setting to an analog input so that the brightness would be on a sliding scale (Because I had already figured out how to get the text value from a user with the servo, it was a matter of copy/paste within the JavaScript for that change).
- I also had to add a function to the particle code I was running to manipulate the LED. It took a little bit of looking into the Particle datasheets to ensure that I had a second port for PWM (it turns out I have four ports for PWM!) and then ensuring I was using the correct code for analog.
Adding A Motion Sensor
My next step was to add another sensor that I had in my possession – a motion sensor. I had the motion sensor from an embedded systems course I took, so it was easy to hook up and add it into my particle code. The hardest part of adding in the motion sensor was the HTML code. All the previous code I had written for the HTML was only pushing data obtained from the user to the particle cloud to control the motor and LED. I wanted the motion sensor to collect the data and publish it on the web page every time it sensed motion. None of the previous tutorials I had looked at gave me that code, so I had to dig a little more. I ended up finding a tutorial where they read a value from a sensor and send it to the web page, but they had done most of the setup quite a bit differently (and more secure than how I did it). I had to change the code, follow the tutorial, and paste the pieces I wanted to keep from my Servo and LED control web page. A difference in the new code is that the user will log into their particle profile, and that is how the web page accesses all the information needed to push/pull any information from the device. I still don’t quite understand some of the code they gave for logging into my profile. I spent time investigating that code and was able to get my original user input code for the Servo and LED to make it to the web page. Sadly, I didn’t end up getting it to work yet. In the original format of the code, I could not translate it over to how the login version has variables and inputs set up. I was also unable to figure out how to translate the login version code to the original format. I’ll keep working on it, but if I don’t, it was still a fun learning experience, and I was able to expand my skills and learn about a different aspect of something I enjoy doing.
The code below shows all of the edits and changes I made to get what I wanted above on the particle size. I added a function titled externalLedToggle that takes a string value, converts it to an integer then sends it to the external LED I connected. The code for the motion sensor was the easiest to add. Since the motion sensor has a digital output (only high or low) it simply reads the value for the motion sensor, and if it’s high, it will publish the event so that the web page could access it. {Tutorials for HTML code}
The Code
void loop() {
if(digitalRead(MOTION_SENSOR) == 1){
Particle.publish(EVENT_NAME, String("Motion Sensed"));
delay(1000);
}
}
int externalLedToggle(String analogValue){
analogvalue = analogValue.toInt();
analogWrite(EX_LED_PIN, analogvalue);
return 1;
}
int servoToggle(String degreeOfServo) {
//find string entered as an int
degreeForServo = degreeOfServo.toInt();
if (degreeForServo >= 90) {
digitalWrite(IN_LED_PIN, HIGH);
myservo.write(degreeForServo);
return 1;
}
else if (degreeForServo < 90) {
digitalWrite(IN_LED_PIN, LOW);
myservo.write(degreeForServo);
return 0;
}
else {
// Unknown option
return -1;
}
}