-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriveMotors.ino
105 lines (92 loc) · 3.03 KB
/
driveMotors.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
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
void driveMotors(byte colorCode, byte *vibrCount)
{
if (colorCode != previousColorCode){ // When there is a new color, or nothing is detected, do not vibrate for one cycle.
Serial.println(""); Serial.print("New color: "); Serial.println(colorNames[colorCode]);
(*vibrCount) = 0;
newVibrationPattern = true;
vibrate(0, 0); // do not vibrate
}
else if (colorCode == previousColorCode && !(colorCode==0)){
vibrate(colorCode, (*vibrCount));
newVibrationPattern = false;
(*vibrCount)++;
}
else if (colorCode==0){
vibrate(0, 0); // do not vibrate
}
if ( (*vibrCount) >= vibrationPatternLength){
(*vibrCount) = 0;
}
previousColorCode = colorCode;
}
void vibrate(byte code, byte count){
const byte halfMotors = floor(motorsNumber/2);
byte power[motorsNumber];
byte vibratingPin = motorsNumber;
memset(power,0,motorsNumber); // DEFAULT: Set motors to zero (DO NOT VIBRATE)
if (code == 4){ // MAGENTA
if (count >= vibrationPatternLength-1){ toggleMag = !toggleMag;
}
if (toggleMag){ code = 1; // assign red code
}
else if (!toggleMag){ code = 3; // assign blue code
}
}
if ((code == 1) && ((count % vibrationPatternLength ) == 0)) { // RED
memset(power, MAX_VIBRATION_INTENSITY, motorsNumber);
}
else if (code == 6){ // ORANGE
if ((count % 2) == 0){
memset(&power[motorsNumber-halfMotors], MAX_VIBRATION_INTENSITY, motorsNumber);
}
else {
memset(power, MAX_VIBRATION_INTENSITY, halfMotors+1);
}
}
else if ( (code == 2) || (code == 3) || (code==5) ){ //SINGLE PIN VIBRATING
if (code == 2){ // GREEN
if (newVibrationPattern){
vibratingPin = rand() % motorsNumber; // Return a number ranging in [0, motorsNumber-1]
}
else if ( !newVibrationPattern && toggleGrn){
vibratingPin = nextPin(previousVibratingPin);
toggleGrn = !toggleGrn;
}
else{
toggleGrn = !toggleGrn;
}
}
else if (code == 3){ // BLU
if (newVibrationPattern){
vibratingPin = rand() % motorsNumber; // Return a number ranging in [0, motorsNumber-1]
}
else {
vibratingPin = nextPin(previousVibratingPin);
}
}
else if (code == 5){ // YELLOW
if (count == 0 ){
vibratingPin = rand() % motorsNumber; // Return a number ranging in [0, motorsNumber-1]
}
else if (count == 1){
vibratingPin = previousVibratingPin;
}
}
if (vibratingPin != motorsNumber){ // If a color has been detected
power[vibratingPin] = MAX_VIBRATION_INTENSITY; // Activate the selected motor
previousVibratingPin = vibratingPin;
}
}
for (byte x=0; x<motorsNumber; x++){ // analog pins A0 to A5
analogWrite(pin[x], power[x]);
if (!(code==0 && count==0)){ Serial.print((bool)power[x]);}
} Serial.print(".");
}
byte nextPin (byte vibrPin)
{
byte newVibrPin = vibrPin + 1;
if (newVibrPin < motorsNumber){
return newVibrPin;}
else{
return 0;}
}