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 temperature
int light_lim = 300;
double temp_lim = 26; // temperature limit
void setup() {
Serial.begin(9600);
pinMode(A0, INPUT); // light sensor
pinMode(A2, INPUT); // temperature sensor
pinMode(11, OUTPUT); // light LED (white)
pinMode(9, OUTPUT); // temperature LED hot (red)
pinMode(8, OUTPUT); // temperature LED cold (blue)
}
void loop() {
// read the value
int 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 cold
digitalWrite(11, HIGH);
digitalWrite(9, LOW);
digitalWrite(8, HIGH);
Serial.print("dark and cold|");
}
else if ((light_value >= light_lim) && (temp_value <= temp_lim)){ // illuminated and cold
digitalWrite (11, LOW);
digitalWrite(9, LOW);
digitalWrite(8, HIGH);
Serial.print("illuminated and cold|");
}
else if ((light_value >= light_lim) && (temp_value >= temp_lim)){ // illuminated and hot
digitalWrite(11, LOW);
digitalWrite(9, HIGH);
digitalWrite(8, LOW);
Serial.print("illuminated and hot|");
}
else if ((light_value <= light_lim) && (temp_value >= temp_lim)){ // dark and hot
digitalWrite(11, HIGH);
digitalWrite(9, HIGH);
digitalWrite(8, LOW);
Serial.print("dark and hot|");
}
else { // in any other case
digitalWrite(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.