This project aims to make Misty a temperature and light tracker in your room!
Last updated
When you are using Arduino to collect data there is a big obstacle: Arduino can't move alone!
Misty will adapt her expression based on the conditions she will be in, for example when it will be dark and cold she will be scared and so on. You can adapt your actions and the path she will drive.
// light and temperatureint light_lim =300;double temp_lim =26; // temperature limitvoidsetup() {Serial.begin(9600);pinMode(A0, INPUT); // light sensorpinMode(A2, INPUT); // temperature sensorpinMode(11, OUTPUT); // light LED (white)pinMode(9, OUTPUT); // temperature LED hot (red)pinMode(8, OUTPUT); // temperature LED cold (blue)}voidloop() { // read the valueint light_value =analogRead(A0);double temp_value = (analogRead(A2) -32) /1.8; // the value is originally read in Fahrenheit, so we convert it in Celsius //compare the value with limits if ((light_value <= light_lim) && (temp_value <= temp_lim)) { // dark and colddigitalWrite(11, HIGH);digitalWrite(9, LOW);digitalWrite(8, HIGH);Serial.print("dark and cold|"); } elseif ((light_value >= light_lim) && (temp_value <= temp_lim)){ // illuminated and colddigitalWrite (11, LOW);digitalWrite(9, LOW);digitalWrite(8, HIGH);Serial.print("illuminated and cold|"); }elseif ((light_value >= light_lim) && (temp_value >= temp_lim)){ // illuminated and hotdigitalWrite(11, LOW);digitalWrite(9, HIGH);digitalWrite(8, LOW);Serial.print("illuminated and hot|"); }elseif ((light_value <= light_lim) && (temp_value >= temp_lim)){ // dark and hotdigitalWrite(11, HIGH);digitalWrite(9, HIGH);digitalWrite(8, LOW);Serial.print("dark and hot|"); } else { // in any other casedigitalWrite(11, LOW);digitalWrite(9, LOW);digitalWrite(8, LOW);Serial.print("searching|"); } String values ="light value: "+String(light_value) +" temperature value: "+String(temp_value); Serial.println(values); delay(2000); // scan every 2 seconds}
First of all, you can set some values of light and temperature that will be the limits to decide if an environment is dark or illuminated and cold or hot.
In this void setup, you open the serial and set the pins you’ll use in this project. In this void loop, you read the sensors and write a specific serial message based on your conditions:
There will be four possible conditions, and it has been also added an extra case that will save the program in case none of the conditions is selected.
The Serial message, which is our way of transmitting the information to Misty is built in this way:
light condition and temperature condition | light value and temperature value
We insert the | because in this way, in the Misty python code, we can split the message and use them indipendently. We want it because:
it would be harder to build an if statement with all the different values each time
you can print the data and only the data that Misty captured in your output
Arduino will scan the different conditions every two seconds.
To allow Misty to read in the Serial you need to build an event, so you can import the usual libraries that Misty uses to register events. Then you can create the robot and the function that will be called with the event.
In this function Misty reads the serial message and if the condition that Arduino sent is verified Misty will act according to what you wrote in that case.
In the function, you can split values and conditions so you can use them independently as previously said. In the end, you can loop the space that will be tracked several times, in this case 2 and meanwhile Misty will track all your data.