-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path5-Rainbow2.ino
66 lines (55 loc) · 1.92 KB
/
5-Rainbow2.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
/*
Author : Teeraphat Kullanankanjana
Version : 1.0
Date : 17/07/2023
Description : Set the color and turn on the 5 RGB LEDs with rainbow colors moving from left to right on the Arduino MKR IoT Carrier.
Copyright (C) 2023 Teeraphat Kullanankanjana. All right reserved.
*/
// Include the Arduino MKR IoT Carrier header file (Library)
#include <Arduino_MKRIoTCarrier.h>
// Create an instance of the MKRIoTCarrier class
MKRIoTCarrier carrier;
// Define the rainbow colors
uint32_t colors[] = {
carrier.leds.Color(255, 0, 0), // Red
carrier.leds.Color(255, 165, 0), // Orange
carrier.leds.Color(255, 255, 0), // Yellow
carrier.leds.Color(0, 255, 0), // Green
carrier.leds.Color(0, 0, 255), // Blue
carrier.leds.Color(75, 0, 130), // Indigo
carrier.leds.Color(238, 130, 238) // Violet
};
// Define the number of rainbow colors
int numColors = sizeof(colors) / sizeof(colors[0]);
int currentPixel = 0; // Variable to track the current pixel
bool movingRight = true; // Variable to indicate the direction of movement
void setup() {
// Initialize the Arduino MKR IoT Carrier
carrier.begin();
}
void loop() {
// Clear the LED colors from the previous frame
carrier.leds.clear();
// Set the color of each LED based on the rainbow
for (int i = 0; i < 5; i++) {
int colorIndex = (currentPixel + i) % numColors;
carrier.leds.setPixelColor(i, colors[colorIndex]);
}
// Update the LEDs to display the set colors
carrier.leds.show();
// Move the pixel position
if (movingRight) {
currentPixel++;
if (currentPixel == numColors - 1) {
currentPixel = 3; // Reverse direction when reaching the end
movingRight = false;
}
} else {
currentPixel--;
if (currentPixel == 0) {
currentPixel = 2; // Reverse direction when reaching the beginning
movingRight = true;
}
}
delay(100); // Adjust the delay time as needed for the desired animation speed
}