Single Servo Serial Position
- // Serial to Servo Position
- #include <Servo.h>
- Servo servo;
- int pos;
- void setup() {
- servo.attach(6);
- Serial.begin(9600);
- servo.write(90);
- }
- void loop() {
- if (Serial.available() > 0) {
- pos = Serial.parseInt();
- Serial.println(pos);
- servo.write(pos);
- delay(100);
- }
- }
Multi-Servo Serial Position (VarSpeedServo)
- // Multi Serial to VarSpeedServo
- // https://github.com/netlabtoolkit/VarSpeedServo
- /*
- Type: Servo No. + Position + . + Speed
- Keypad Friendly Position and Speed Testing
- Example: 290.120 (Servo 2 Position 90 Speed 120)
- 10 to 1180 + .0 to .255 Speed for Servo 1
- */
- #include <VarSpeedServo.h>
- // Name Servos
- VarSpeedServo one;
- VarSpeedServo two;
- VarSpeedServo three;
- VarSpeedServo four;
- VarSpeedServo five;
- // Set Values
- String input;
- String also;
- String then;
- int also_int;
- int then_pos;
- int then_int;
- // Default Position and Speed
- const int start = 90;
- const int set_speed = 50;
- void setup() {
- Serial.begin(9600);
- // Assign Pins
- one.attach(2);
- two.attach(3);
- three.attach(4);
- four.attach(5);
- five.attach(6);
- // Default Position and Speed
- one.write(start, set_speed, true);
- two.write(start, set_speed, true);
- three.write(start, set_speed, true);
- four.write(start, set_speed, true);
- five.write(start, set_speed, true);
- }
- void loop() {
- if (Serial.available() > 0)
- {
- // Assign Strings
- input = Serial.readString();
- also = input;
- then = also;
- // Get Servo Number
- input.remove(1);
- // Get Position
- also.remove(0, 1);
- also_int = also.toInt();
- // Get Speed
- then_pos = then.indexOf('.');
- then_pos = then_pos + 1;
- then = then.substring(then_pos);
- then_int = then.toInt();
- // Print Results
- Serial.print(input);
- Serial.print(" ");
- Serial.print(also_int);
- Serial.print(" ");
- Serial.println(then_int);
- // Run Results
- if (input == "1") {
- one.write(also_int, then_int, true);
- } else if (input == "2") {
- two.write(also_int, then_int, true);
- } else if (input == "3") {
- three.write(also_int, then_int, true);
- } else if (input == "4") {
- four.write(also_int, then_int, true);
- } else if (input == "5") {
- five.write(also_int, then_int, true);
- }
- delay(10);
- }
- }
Fastled Random Order Block Segment
- #include "FastLED.h"
- #define NUM_LEDS 200
- CRGB leds[NUM_LEDS];
- #define PIN 9
- int v;
- int rd;
- int gn;
- int bl;
- int w;
- int start;
- int fin;
- int state;
- int last_state;
- // int pot_read;
- const int pos_num = 16;
- int pos_array[pos_num];
- void setup() {
- FastLED.addLeds<WS2812, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
- }
- void loop() {
- blocks(0, 0, 255);
- blocks(0, 0, 0);
- }
- void blocks(int rd, int gn, int bl) {
- randomizelist();
- for (w = 0; w < pos_num; w++) {
- start = pos_array[w];
- start = start * 15;
- fin = start + 15;
- for (v = start; v < fin; v++) {
- setPixel(v, rd, gn, bl);
- }
- showStrip();
- // escape/change function with pot (optional)
- // pot_read = analogRead(A0);
- // state = map(pot_read, 0, 1023, 1, 10);
- // if (state != last_state) {
- // return;
- // }
- delay(750);
- }
- }
- void randomizelist() {
- unsigned char chosen[pos_num];
- unsigned char index_pt, log_pt;
- for (index_pt = 0; index_pt < pos_num; index_pt++)
- chosen[index_pt] = 0;
- randomSeed(analogRead(A0));
- for (index_pt = 0; index_pt < pos_num; index_pt++) {
- int r = random(pos_num - index_pt);
- for (log_pt = 0; log_pt <= r; log_pt++) {
- r += chosen[log_pt];
- }
- chosen[r] = 1;
- pos_array[index_pt] = r;
- }
- }
- void showStrip() {
- #ifdef ADAFRUIT_NEOPIXEL_H
- // NeoPixel
- strip.show();
- #endif
- #ifndef ADAFRUIT_NEOPIXEL_H
- // FastLED
- FastLED.show();
- #endif
- }
- void setPixel(int Pixel, byte red, byte green, byte blue) {
- #ifdef ADAFRUIT_NEOPIXEL_H
- // NeoPixel
- strip.setPixelColor(Pixel, strip.Color(red, green, blue));
- #endif
- #ifndef ADAFRUIT_NEOPIXEL_H
- // FastLED
- leds[Pixel].r = red;
- leds[Pixel].g = green;
- leds[Pixel].b = blue;
- #endif
- }
- void setAll(byte red, byte green, byte blue) {
- for (int i = 0; i < NUM_LEDS; i++ ) {
- setPixel(i, red, green, blue);
- }
- showStrip();
- }
Lightning Code
- // Lighting Cloud
- // James Bruce
- #include <FastLED.h>
- #define NUM_LEDS 6
- #define DATA_PIN 9
- enum Mode {CLOUD, ACID, OFF, ON, RED, GREEN, BLUE, FADE};
- Mode mode = CLOUD;
- Mode lastMode = CLOUD;
- int fade_h;
- int fade_direction = 1;
- CRGB leds[NUM_LEDS];
- void setup() {
- FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
- Serial.begin(9600);
- }
- void loop() {
- constant_lightning();
- }
- void single_colour(int H) {
- for (int i = 0; i < NUM_LEDS; i++) {
- leds[i] = CHSV( H, 255, 255);
- }
- if (lastMode != mode) {
- FastLED.show();
- lastMode = mode;
- }
- delay(50);
- }
- void colour_fade() {
- for (int i = 0; i < NUM_LEDS; i++) {
- leds[i] = CHSV( fade_h, 255, 255);
- }
- if (fade_h > 254) {
- fade_direction = -1;
- }
- else if (fade_h < 0) {
- fade_direction = 1;
- }
- fade_h += fade_direction;
- FastLED.show();
- delay(100);
- }
- void reset() {
- for (int i = 0; i < NUM_LEDS; i++) {
- leds[i] = CHSV( 0, 0, 0);
- }
- FastLED.show();
- }
- void acid_cloud() {
- for (int i = 0; i < NUM_LEDS; i++) {
- if (random(0, 100) > 90) {
- leds[i] = CHSV( random(0, 255), 255, 255);
- }
- else {
- leds[i] = CHSV(0, 0, 0);
- }
- }
- FastLED.show();
- delay(random(5, 100));
- reset();
- //}
- }
- void rolling() {
- for (int r = 0; r < random(2, 10); r++) {
- for (int i = 0; i < NUM_LEDS; i++) {
- if (random(0, 100) > 90) {
- leds[i] = CHSV( 0, 0, 255);
- }
- else {
- leds[i] = CHSV(0, 0, 0);
- }
- }
- FastLED.show();
- delay(random(5, 100));
- reset();
- }
- }
- void crack() {
- for (int i = 0; i < NUM_LEDS; i++) {
- leds[i] = CHSV( 0, 0, 255);
- }
- FastLED.show();
- delay(random(10, 100));
- reset();
- }
- void thunderburst() {
- int rs1 = random(0, NUM_LEDS / 2);
- int rl1 = random(10, 20);
- int rs2 = random(rs1 + rl1, NUM_LEDS);
- int rl2 = random(10, 20);
- for (int r = 0; r < random(3, 6); r++) {
- for (int i = 0; i < rl1; i++) {
- leds[i + rs1] = CHSV( 0, 0, 255);
- }
- if (rs2 + rl2 < NUM_LEDS) {
- for (int i = 0; i < rl2; i++) {
- leds[i + rs2] = CHSV( 0, 0, 255);
- }
- }
- FastLED.show();
- delay(random(10, 50));
- reset();
- delay(random(10, 50));
- }
- }
- void constant_lightning() {
- switch (random(1, 4)) {
- case 1:
- thunderburst();
- delay(random(10, 500));
- break;
- case 2:
- rolling();
- break;
- case 3:
- crack();
- delay(random(50, 250));
- break;
- case 4:
- acid_cloud();
- break;
- }
- }