forked from OttoDIY/OttoNinja
-
Notifications
You must be signed in to change notification settings - Fork 2
/
OTTO_TouchSensor
170 lines (163 loc) · 4.41 KB
/
OTTO_TouchSensor
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//----------------------------------------------------------------
//-- Otto basic firmware v2 adapted from Zowi (ottodiy.com)
//-- CC BY SA
//-- 10 September 2017
//-----------------------------------------------------------------
//-- Otto with Touch Sensor!
//-----------------------------------------------------------------
#include <Servo.h>
#include <Oscillator.h>
#include <US.h>
#include <Otto.h>
Otto Otto; //This is Otto!
//---------------------------------------------------------
//-- First step: Make sure the pins for servos are in the right position
/*
----/\---- <== Touch Sensor Pin 12
---------------
| O O |
|---------------|
YR 3==> | | <== YL 2
---------------
|| ||
RR 5==> ----- ------ <== RL 4
|----- ------|
*/
#define PIN_YL 2 //servo[2]
#define PIN_YR 3 //servo[3]
#define PIN_RL 4 //servo[4]
#define PIN_RR 5 //servo[5]
const int sensorPin = 12;
///////////////////////////////////////////////////////////////////
//-- Global Variables -------------------------------------------//
///////////////////////////////////////////////////////////////////
//-- Movement parameters
int movimiento = 1;
//---------------------------------------------------------
bool obstacleDetected = false;
///////////////////////////////////////////////////////////////////
//-- Setup ------------------------------------------------------//
///////////////////////////////////////////////////////////////////
void setup(){
//Set the servo pins
Otto.init(PIN_YL,PIN_YR,PIN_RL,PIN_RR,true, -1, 10, 8, 9);
pinMode(sensorPin, INPUT);
Otto.home();
Otto.sing(S_happy); // a happy Otto :)
}
///////////////////////////////////////////////////////////////////
//-- Principal Loop ---------------------------------------------//
///////////////////////////////////////////////////////////////////
void loop() {
int estado = digitalRead(sensorPin);
if(obstacleDetected){
Serial.println("OBJETO DETECTADO");
Otto.sing(S_fart1);
Otto.walk(3,500,-1);
Otto.tiptoeSwing(2,500,40);
Otto.home();
Otto.sing(S_fart3);
delay(50);
obstacleDetector();
}
else{
obstacleDetector();
}
//mandar mensaje a puerto serie en función del valor leido
if (estado == HIGH)
{
if (movimiento == 1)
{
Otto.sing(S_surprise);
Otto.walk(2,1300,1);
Otto.home();
}
if (movimiento == 2)
{
Otto.sing(S_OhOoh);
Otto.moonwalker(2,1000,30,1);
Otto.home();
}
if (movimiento == 3)
{
Otto.sing(S_happy);
Otto.crusaito(2,3000,40,1);
Otto.home();
}
if (movimiento == 4)
{
Otto.sing(S_superHappy);
Otto.shakeLeg(2,1000,1);
Otto.home();
}
if (movimiento == 5)
{
Otto.sing(S_fart1);
Otto.flapping(2,500,40,1);
Otto.home();
}
if (movimiento == 6)
{
Otto.sing(S_confused);
Otto.tiptoeSwing(2,1000,30);
Otto.home();
}
if (movimiento == 7)
{
Otto.sing(S_connection);
Otto.swing(2,500,40);
Otto.home();
}
if (movimiento == 8)
{
Otto.sing(S_disconnection);
Otto.updown(2,1000,30);
Otto.home();
}
if (movimiento == 9)
{
Otto.sing(S_buttonPushed);
Otto.jitter(10,500,40);
Otto.home();
}
if (movimiento == 10)
{
Otto.sing(S_fart3);
Otto.ascendingTurn(2,500,50);
Otto.home();
}
if (movimiento == 11)
{
Otto.sing(S_sad);
Otto.jump(2,500);
Otto.home();
}
if (movimiento == 12)
{
Otto.sing(S_cuddly);
Otto.turn(3,1000,1);
Otto.home();
}
if (movimiento == 13)
{
Otto.sing(S_superHappy);
Otto.bend(2,1000,1);
Otto.home();
}
movimiento = movimiento +1;
if (movimiento == 14)
{
movimiento = 1;
}
}
}
///////////////////////////////////////////////////////////////////
//-- Function to read distance sensor & to actualize obstacleDetected variable
void obstacleDetector(){
int distance = Otto.getDistance();
if(distance<15){
obstacleDetected = true;
}else{
obstacleDetected = false;
}
}