-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoopingLouie.ino
88 lines (69 loc) · 1.91 KB
/
LoopingLouie.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
#include <AFMotor.h>
#define MAXSPEED 100
#define MINSPEED 40
#define SPEEDCHANGE 3000
AF_DCMotor motorL(1);
unsigned long targetTimeMode = 0;
unsigned long nextSpeedChange = 0;
unsigned int targetSpeed = MINSPEED;
unsigned int rndfor = 0;
typedef struct{
int minspeed;
int maxspeed;
int mintime;
int maxtime;
bool forward;
bool randomspeed;
} mode;
//minspeed, maxspeed, mintime, maxtime, forward/backward (1/-1), randomspeed 1/0
const mode modeForward = {MINSPEED, MAXSPEED, 2000, 30000, true, true};
const mode modeBackward = {MINSPEED, MAXSPEED, 500, 1000, false, true};
const mode modeDrop = {0, 0, 1000, 1000, true, false};
const mode modeEscalate = {MAXSPEED, MAXSPEED, 5000, 5000, true, false};
mode modes[] = {modeForward, modeBackward, modeDrop, modeEscalate};
mode *currentMode = NULL;
void nextMode() {
int index = random(0, sizeof(modes) / sizeof(mode));
currentMode = &modes[index];
targetTimeMode = millis() + random(currentMode->mintime, currentMode->maxtime);
targetSpeed = random(currentMode->minspeed, currentMode->maxspeed);
Serial.print("Mode changed: ");
Serial.print(index);
Serial.print("\n");
}
uint8_t speed = 0;
uint8_t generateSpeed() {
if (currentMode->randomspeed) {
if(millis() > nextSpeedChange){
targetSpeed = random(currentMode->minspeed, currentMode->maxspeed);
}
} else {
nextSpeedChange = millis() + SPEEDCHANGE;
}
if(speed < targetSpeed) {
speed = (speed+2);
} else {
speed = (speed-2);
}
speed = min(speed, MAXSPEED);
return speed;
}
void setup() {
// put your setup code here, to run once:
motorL.run(RELEASE);
targetTimeMode = millis();
nextMode();
Serial.begin(9600);
}
void loop() {
if(millis() > targetTimeMode){
nextMode();
}
motorL.setSpeed(generateSpeed());
if(currentMode->forward) {
motorL.run(FORWARD);
} else {
motorL.run(BACKWARD);
}
delay(100);
}