What controls the timing between loops in this code?

I understand how to set the time of the ramp up and down, blinking, and excited, but what controls the time between ramp down ending and the next ramp up starting? There isn't just no delay, because when I run it, there are random short pauses. Thank you for any help.

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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
 /*
Jar of Fireflies using ATTiny85
Author: Jason Webb
Author website: http://jason-webb.info
Author e-mail: zen.webb@gmail.com

Project wiki: http://jason-webb.info/wiki/index.php?title=Jar_of_Fireflies
Github repo: https://github.com/jasonwebb/Jar-of-Fireflies

GENERAL DESCRIPTION:
An Arduino sketch for the ATTiny85 (using HLT's bootloader)
that flashes six charlieplexed LEDs to mimic the behavior of
fireflies. Using a piezo detector, the LED array can react to 
taps on the enclosure.
*/

// Pin assignments
const byte pinA = 0;
const byte pinB = 1;
const byte pinC = 2;
const byte piezoPin = 2;  // analog pin 2

// LED variables
byte currentLED;
byte previousLED = -1;
byte rampUpSpeed;
byte rampDownSpeed;
byte numBlinks;
int counter;

// Piezo variables
const int piezoBufferSize = 10;
int piezoValues[piezoBufferSize];
int piezoThreshold = 1;
int piezoCounter = 0;

// Behavior states
const byte BEGIN = 0;
const byte RAMP_UP = 1;
const byte BLINKING = 2;
const byte RAMP_DOWN = 3;
const byte EXCITED = 4;
byte CURRENT_ACTION = BEGIN;

void setup() {
  // Flush any stray current from the ports
  turnOffAll();
}

void loop() {
  switch(CURRENT_ACTION) {
    
    // BEGIN =========================================
    // + Set up all the variables to flash new LED
    case BEGIN:
      // Choose a new LED
      previousLED = currentLED;
      
      do {
        currentLED = (int)random(1,6);
      } while(currentLED == previousLED);
      
      // Number of times to blink
      numBlinks = (int)random(1,6);
      
      // Fade in and fade out speed
      rampUpSpeed = random(6);
      rampDownSpeed = rampUpSpeed*2;
      
      counter = 0;
      CURRENT_ACTION = RAMP_UP;
      break;
      
    // RAMP UP ========================================
    // + Fade in the LED
    case RAMP_UP:
      analogOn(currentLED, counter);
      delay(rampUpSpeed);
      
      if(counter >= 255) {
        CURRENT_ACTION = BLINKING;
        counter = 0;
      } else {
        counter++;
      }
      
      break;
      
    // BLINKING ====================================
    // + Blink the LED a certain number of times
    case BLINKING:
      analogOn(currentLED, 0);
      delay(random(10,50));
      analogOn(currentLED, 255);
      delay(random(30,100));
      
      if(numBlinks > 0) {
        numBlinks--;
      } else {
        CURRENT_ACTION = RAMP_DOWN;
        counter = 255;
      }
      
      break;
      
    // RAMP DOWN ===========================================
    // + Fade out the LED
    case RAMP_DOWN:
      analogOn(currentLED, counter);
      delay(rampDownSpeed);
      
      if(counter == 0) {
        CURRENT_ACTION = BEGIN;
      } else {
        counter--;
      }
      
      break;
      
    // EXCITED =========================================
    // + Flash each LED, one at a time, six times in a row
    case EXCITED:
      for(int j=0; j<6; j++) {
        for(int i=1; i<=6; i++) {
          turnOn(i);
          delay(50);
          turnOff(i);
          delay(50);
        }
      }

      CURRENT_ACTION = BEGIN;

      break;
  }
  
  checkPiezo();
}

// Turn on a single LED to full brightness
void turnOn(byte led) {
  analogOn(led, 255);
}

// Turn off an LED completely
void turnOff(byte led) {
  analogOn(led, 0);
}

void turnOffAll() {
  for(int i=0; i<=2; i++) {
    pinMode(i, OUTPUT);
    digitalWrite(i, LOW);
  }
}

// Write an analog value to an LED
void analogOn(byte led, byte value) {
  switch(led) {
    case 1:
      pinMode(pinA, OUTPUT);
      pinMode(pinB, OUTPUT);
      pinMode(pinC, INPUT);
      
      digitalWrite(pinA, LOW);
      analogWrite(pinB, value);
      break;
    case 2:
      pinMode(pinA, OUTPUT);
      pinMode(pinB, OUTPUT);
      pinMode(pinC, INPUT);
      
      digitalWrite(pinB, LOW);
      analogWrite(pinA, value);
      break;
    case 3:
      pinMode(pinA, INPUT);
      pinMode(pinB, OUTPUT);
      pinMode(pinC, OUTPUT);
      
      digitalWrite(pinC, HIGH);
      analogWrite(pinB, 255-value);
      break;
    case 4:
      pinMode(pinA, INPUT);
      pinMode(pinB, OUTPUT);
      pinMode(pinC, OUTPUT);
      
      digitalWrite(pinC, LOW);
      analogWrite(pinB, value);
      break;
    case 5:
      pinMode(pinA, OUTPUT);
      pinMode(pinB, INPUT);
      pinMode(pinC, OUTPUT);
      
      digitalWrite(pinC, HIGH);
      analogWrite(pinA, 255-value);
      break;
    case 6:
      pinMode(pinA, OUTPUT);
      pinMode(pinB, INPUT);
      pinMode(pinC, OUTPUT);
      
      digitalWrite(pinC, LOW);
      analogWrite(pinA, value);
      break;
  }
}

void checkPiezo() {
  if(piezoCounter < piezoBufferSize) {
    // Take a reading
    int val = analogRead(piezoPin);
    piezoValues[piezoCounter] = val;
  } else {
    // Find average
    int avg = 0;
    
    for(int i=0; i<piezoBufferSize; i++)
      avg += piezoValues[i];
      
    avg = avg/piezoBufferSize;
    
    if(avg >= piezoThreshold) {
      for(int j=0; j<6; j++) {
        for(int i=1; i<=6; i++) {
          turnOn(i);
          delay(50);
          turnOff(i);
          delay(50);
        }
      }
    }
    
    // Reset counter
    piezoCounter = 0;
  }
  
  piezoCounter++;
}
Topic archived. No new replies allowed.