Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Motor and RGB-LED control by GeekGarage.dk
- // Published on October 20 2019
- //////////////////////////////////////////
- // YOUR CONFIGURATION - EDIT BELOW THIS //
- //////////////////////////////////////////
- //Defines PIN Reference (To save memory instead of int) - Change PIN NUMBER according to your wirering!
- #define MOTOR_PIN_1 5
- #define MOTOR_PIN_2 6
- #define MOTOR_PIN_3 7
- #define MOTOR_PIN_4 8
- #define PIN_RED 9
- #define PIN_GREEN 10
- #define PIN_BLUE 11
- //Motor
- int Motor_Direction = 1; // 1 = clockwise, -1 = counter clockwise - Initial direction or if change is disabled
- bool Motor_Change_Direction = true; //Should motor change direction based on the min and max timers?
- unsigned long Min_Motor_Time_Direction = 5000; //How long should the motor minimum move in one direction in milliseconds
- unsigned long Max_Motor_Time_Direction = 30000; //How long should the motor maximum move in one direction in milliseconds
- unsigned short Motor_Speed = 1; //Initial motor speed or if speed change is disabled this is the speed used
- bool Motor_Change_Speed = true; //Should the motor change speed?
- unsigned short Min_Motor_Speed = 1; //The fastest speed the motor can run.
- unsigned short Max_Motor_Speed = 5; //The slowest speed the motor can run
- unsigned long Min_Motor_Speed_Change_Time = 5000; //The minimum time before motor change speed in milliseconds
- unsigned long Max_Motor_Speed_Change_Time = 30000; //The maximum time before motor change speed in milliseconds
- //LED
- unsigned short RGB_Value[3] = {44,104,168}; //These are the Red, Green and Blue values, in that order, and value from 0-255.
- bool Should_Flicker = true;
- unsigned long Min_Time_Between_Flicker = 6000; //Minimum time between flickers in milliseconds
- unsigned long Max_Time_Between_Flicker = 30000; //Maximum time between flickers in milliseconds
- unsigned long Min_Flicker_Time = 3000; //Minimum flicker time in milliseconds
- unsigned long Max_Flicker_Time = 7000; //Maximum flicker time in milliseconds
- //BREATH DOES NOTHING FOR NOW!!!
- bool Should_Breath = true;
- unsigned long Min_Time_Between_Breath = 6000; //Minimum time between breath in milliseconds
- unsigned long Max_Time_Between_Breath = 12000; //Maximum time between breath in milliseconds
- unsigned long Min_Breath_Time = 3000; //Minimum flicker time in milliseconds
- unsigned long Max_Breath_Time = 7000; //Maximum flicker time in milliseconds
- ///////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////
- // DO NOT EDIT BELOW THIS UNLESS YOU KNOW WHAT YOU ARE DOING //
- ///////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////
- //Initialize base values
- int MotorStep = 7;
- unsigned long Motor_Time_Direction = random(Min_Motor_Time_Direction,Max_Motor_Time_Direction);
- unsigned long Motor_Speed_Change_Time = random(Min_Motor_Speed_Change_Time,Max_Motor_Speed_Change_Time);
- unsigned long Time_Between_Flicker = random(Min_Time_Between_Flicker,Max_Time_Between_Flicker);
- unsigned long Flicker_Time = random(Min_Flicker_Time,Max_Flicker_Time);
- unsigned long Time_Between_Breath = random(Min_Time_Between_Breath,Max_Time_Between_Breath);
- unsigned long Breath_Time = random(Min_Breath_Time,Max_Breath_Time);
- unsigned long LEDStartMillis_Flick = millis();
- unsigned long LEDStartMillis_Breath = millis();
- unsigned long LEDCurrentMillis_Flick = millis();
- unsigned long LEDCurrentMillis_Breath = millis();
- unsigned short FlickState = 0;
- unsigned short BreathState = 0;
- const unsigned short Org_RGB_Value[3] = {RGB_Value[0],RGB_Value[1],RGB_Value[2]};
- // Timers setup for "multitasking"
- unsigned long currentMillis = millis();
- unsigned long prevMotor_Time_Direction = 0;
- unsigned long prevMotor_Speed_Change_Time = 0;
- unsigned long prevMotor_Speed = 0;
- unsigned long prevTime_Between_Flicker = 0;
- unsigned long prevTime_Between_Breath = 0;
- unsigned long prevLEDMillis_Flick = 0;
- unsigned long prevLEDMillis_Breath = 0;
- void setup()
- {
- //Setup Motor Pins
- pinMode(MOTOR_PIN_1, OUTPUT);
- pinMode(MOTOR_PIN_2, OUTPUT);
- pinMode(MOTOR_PIN_3, OUTPUT);
- pinMode(MOTOR_PIN_4, OUTPUT);
- //Setup LED Pins
- pinMode(PIN_RED, OUTPUT);
- pinMode(PIN_GREEN, OUTPUT);
- pinMode(PIN_BLUE, OUTPUT);
- analogWrite(PIN_RED, RGB_Value[0]);
- analogWrite(PIN_GREEN, RGB_Value[1]);
- analogWrite(PIN_BLUE, RGB_Value[2]);
- }
- void loop() {
- currentMillis = millis();
- //Random pick based on config
- if (Motor_Change_Direction && (currentMillis - prevMotor_Time_Direction >= Motor_Time_Direction)) {
- Motor_Direction = randomDirection();
- Motor_Time_Direction = random(Min_Motor_Time_Direction,Max_Motor_Time_Direction);
- prevMotor_Time_Direction = currentMillis;
- }
- if (Motor_Change_Speed && (currentMillis - prevMotor_Speed_Change_Time >= Motor_Speed_Change_Time)) {
- Motor_Speed = random(Min_Motor_Speed,Max_Motor_Speed);
- Motor_Speed_Change_Time = random(Min_Motor_Speed_Change_Time,Max_Motor_Speed_Change_Time);
- prevMotor_Speed_Change_Time = currentMillis;
- }
- if (Should_Flicker && (currentMillis - prevTime_Between_Flicker >= Time_Between_Flicker)) { //Run if flick enabled and >= timer
- if(LEDStartMillis_Flick == 0){ //Get start millis for flick to keep track of run time
- LEDStartMillis_Flick = currentMillis;
- }
- LEDCurrentMillis_Flick = millis(); //millis used to keep track of last state change
- if(LEDCurrentMillis_Flick-LEDStartMillis_Flick <= Flicker_Time){
- if(LEDCurrentMillis_Flick-prevLEDMillis_Flick >= random(30,120)){ //Change state every 30 to 120 ms at random
- if (FlickState == 1){
- analogWrite(PIN_RED, RGB_Value[0]);
- analogWrite(PIN_GREEN, RGB_Value[1]);
- analogWrite(PIN_BLUE, RGB_Value[2]);
- FlickState = 0;
- } else {
- analogWrite(PIN_RED, random(0,(RGB_Value[0])));
- analogWrite(PIN_GREEN, random(0,(RGB_Value[1])));
- analogWrite(PIN_BLUE, random(0,(RGB_Value[2])));
- FlickState = 1;
- }
- prevLEDMillis_Flick = LEDCurrentMillis_Flick;
- }
- }
- if(LEDCurrentMillis_Flick-LEDStartMillis_Flick >= Flicker_Time){
- LEDStartMillis_Flick = 0;
- Time_Between_Flicker = random(Min_Time_Between_Flicker,Max_Time_Between_Flicker);
- Flicker_Time = random(Min_Flicker_Time,Max_Flicker_Time);
- prevTime_Between_Flicker = currentMillis;
- analogWrite(PIN_RED, Org_RGB_Value[0]);
- analogWrite(PIN_GREEN, Org_RGB_Value[1]);
- analogWrite(PIN_BLUE, Org_RGB_Value[2]);
- FlickState = 0;
- }
- }
- if (Should_Breath && (currentMillis - prevTime_Between_Breath >= Time_Between_Breath)) {
- if(LEDStartMillis_Breath == 0){ //Get start millis for flick to keep track of run time
- LEDStartMillis_Breath = currentMillis;
- }
- LEDCurrentMillis_Breath = millis(); //millis used to keep track of last state change
- if(LEDCurrentMillis_Breath-LEDStartMillis_Breath <= Breath_Time){
- if(LEDCurrentMillis_Breath-prevLEDMillis_Breath >= random(30,120)){ //Change state every 30 to 120 ms at random
- if (BreathState == 1){
- //Breath one direction
- BreathState = 0;
- } else {
- //Breath other direction
- BreathState = 1;
- }
- prevLEDMillis_Breath = LEDCurrentMillis_Breath;
- }
- }
- if(LEDCurrentMillis_Breath-LEDStartMillis_Breath >= Breath_Time){
- LEDStartMillis_Breath = 0;
- Time_Between_Breath = random(Min_Time_Between_Breath,Max_Time_Between_Breath);
- Breath_Time = random(Min_Breath_Time,Max_Breath_Time);
- prevTime_Between_Breath = currentMillis;
- analogWrite(PIN_RED, Org_RGB_Value[0]);
- analogWrite(PIN_GREEN, Org_RGB_Value[1]);
- analogWrite(PIN_BLUE, Org_RGB_Value[2]);
- BreathState = 0;
- }
- }
- //Start of motor movement
- if (currentMillis - prevMotor_Speed >= Motor_Speed) {
- if (Motor_Direction == 1) {
- MotorStep++;
- } else {
- MotorStep--;
- }
- if (MotorStep > 7) {
- MotorStep = 0;
- MotorAdvance(MotorStep);
- } else if (MotorStep < 0) {
- MotorStep = 7;
- MotorAdvance(MotorStep);
- } else {
- MotorAdvance(MotorStep);
- }
- prevMotor_Speed = currentMillis;
- }
- //End of motor movement
- }
- void MotorAdvance(int MotorStep) {
- switch (MotorStep) {
- case 0:
- digitalWrite(MOTOR_PIN_1, LOW);
- digitalWrite(MOTOR_PIN_2, LOW);
- digitalWrite(MOTOR_PIN_3, LOW);
- digitalWrite(MOTOR_PIN_4, HIGH);
- break;
- case 1:
- digitalWrite(MOTOR_PIN_1, LOW);
- digitalWrite(MOTOR_PIN_2, LOW);
- digitalWrite(MOTOR_PIN_3, HIGH);
- digitalWrite(MOTOR_PIN_4, HIGH);
- break;
- case 2:
- digitalWrite(MOTOR_PIN_1, LOW);
- digitalWrite(MOTOR_PIN_2, LOW);
- digitalWrite(MOTOR_PIN_3, HIGH);
- digitalWrite(MOTOR_PIN_4, LOW);
- break;
- case 3:
- digitalWrite(MOTOR_PIN_1, LOW);
- digitalWrite(MOTOR_PIN_2, HIGH);
- digitalWrite(MOTOR_PIN_3, HIGH);
- digitalWrite(MOTOR_PIN_4, LOW);
- break;
- case 4:
- digitalWrite(MOTOR_PIN_1, LOW);
- digitalWrite(MOTOR_PIN_2, HIGH);
- digitalWrite(MOTOR_PIN_3, LOW);
- digitalWrite(MOTOR_PIN_4, LOW);
- break;
- case 5:
- digitalWrite(MOTOR_PIN_1, HIGH);
- digitalWrite(MOTOR_PIN_2, HIGH);
- digitalWrite(MOTOR_PIN_3, LOW);
- digitalWrite(MOTOR_PIN_4, LOW);
- break;
- case 6:
- digitalWrite(MOTOR_PIN_1, HIGH);
- digitalWrite(MOTOR_PIN_2, LOW);
- digitalWrite(MOTOR_PIN_3, LOW);
- digitalWrite(MOTOR_PIN_4, LOW);
- break;
- case 7:
- digitalWrite(MOTOR_PIN_1, HIGH);
- digitalWrite(MOTOR_PIN_2, LOW);
- digitalWrite(MOTOR_PIN_3, LOW);
- digitalWrite(MOTOR_PIN_4, HIGH);
- break;
- default:
- digitalWrite(MOTOR_PIN_1, LOW);
- digitalWrite(MOTOR_PIN_2, LOW);
- digitalWrite(MOTOR_PIN_3, LOW);
- digitalWrite(MOTOR_PIN_4, LOW);
- break;
- }
- }
- int randomDirection() {
- return (int) random(2) * 2 - 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement