/
exercise_8.ino
73 lines (61 loc) · 1.81 KB
/
exercise_8.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
#include <smorphi.h>
// Data wire is plugged into port 1 on the Masterboard
#define ONE_WIRE_BUS 16
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// Initialise the robot
Smorphi my_robot;
/*
* The setup function. We only start the sensors here
*/
void setup(void)
{
// start serial port
Serial.begin(115200);
Serial.println("Dallas Temperature IC Control Library Demo");
// Start up the library
sensors.begin();
}
/*
* Main function, get and show the temperature
*/
void loop(void)
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
// After we got the temperatures, we can print them here.
// We use the function ByIndex, and as an example get the temperature from the first sensor only.
float tempC = sensors.getTempCByIndex(0);
// Check if reading was successful
if(tempC != DEVICE_DISCONNECTED_C)
{
Serial.print("Temperature for the device 1 (index 0) is: ");
Serial.println(tempC);
// if temperature is more than 30°C, Smorphi moves forward
if (tempC > 30)
{
my_robot.MoveForward(10);
}
// if temperature is less than 30°C, Smorphi stops
else
{
my_robot.sm_reset_M1();
my_robot.sm_reset_M2();
my_robot.sm_reset_M3();
my_robot.sm_reset_M4();
my_robot.stopSmorphi();
}
}
else
{
Serial.println("Error: Could not read temperature data");
}
}