Stepper motor with buttons and seven segment. (it was cool)
This commit is contained in:
+2
-1
@@ -10,6 +10,7 @@
|
||||
|
||||
[env:uno]
|
||||
platform = atmelavr
|
||||
board = uno
|
||||
# board = uno
|
||||
board = sparkfun_promicro16
|
||||
framework = arduino
|
||||
lib_deps = https://github.com/DFRobot/DFRobotDFPlayerMini
|
||||
|
||||
+97
-31
@@ -1,42 +1,108 @@
|
||||
#include <Arduino.h>
|
||||
#include <SoftwareSerial.h>
|
||||
#include "DFRobotDFPlayerMini.h"
|
||||
|
||||
int music_num;
|
||||
SoftwareSerial mySoftwareSerial(10, 11); // #1
|
||||
DFRobotDFPlayerMini myDFPlayer; // #2
|
||||
// Buttons
|
||||
#define LEFT_PIN20 20
|
||||
#define RIGHT_PIN19 19
|
||||
#define SPEED_PIN18 18
|
||||
|
||||
// Steper motor
|
||||
const int STEP_PIN5 = 5;
|
||||
const int DIR_PIN7 = 7;
|
||||
int SPEED_INDEX = 2; // by defult is 2
|
||||
|
||||
// LEDs
|
||||
const short int LA = 2;
|
||||
const short int LB = 3;
|
||||
const short int LC = 10;
|
||||
const short int LD = 14;
|
||||
const short int LE = 15;
|
||||
const short int LF = 4;
|
||||
const short int LG = 8;
|
||||
const short int LDOT = 16;
|
||||
|
||||
// Setup Section
|
||||
void setup() {
|
||||
mySoftwareSerial.begin(9600);
|
||||
Serial.begin(115200);
|
||||
myDFPlayer.begin(mySoftwareSerial); // #3
|
||||
myDFPlayer.volume(20);
|
||||
// Stepper motor setup
|
||||
pinMode(STEP_PIN5, OUTPUT);
|
||||
pinMode(DIR_PIN7, OUTPUT);
|
||||
|
||||
// Seven sgement setup
|
||||
pinMode(LA, OUTPUT);
|
||||
pinMode(LB, OUTPUT);
|
||||
pinMode(LC, OUTPUT);
|
||||
pinMode(LD, OUTPUT);
|
||||
pinMode(LE, OUTPUT);
|
||||
pinMode(LF, OUTPUT);
|
||||
pinMode(LG, OUTPUT);
|
||||
pinMode(LDOT, OUTPUT);
|
||||
|
||||
// Buttons setup
|
||||
pinMode(LEFT_PIN20, INPUT);
|
||||
pinMode(RIGHT_PIN19, INPUT);
|
||||
pinMode(SPEED_PIN18, INPUT);
|
||||
}
|
||||
|
||||
// Loop Section
|
||||
void loop() {
|
||||
if (Serial.available() > 0) {
|
||||
music_num = Serial.readString().toInt(); // #4
|
||||
if (music_num < 5 && music_num > 0) {
|
||||
switch (music_num) {
|
||||
case 1:
|
||||
Serial.println("1");
|
||||
myDFPlayer.play(1);
|
||||
break;
|
||||
case 2:
|
||||
Serial.println("2");
|
||||
myDFPlayer.play(2);
|
||||
break;
|
||||
case 3:
|
||||
Serial.println("3");
|
||||
myDFPlayer.play(3);
|
||||
break;
|
||||
case 4:
|
||||
Serial.println("4");
|
||||
myDFPlayer.play(4);
|
||||
break;
|
||||
}
|
||||
}
|
||||
long int micro_seconds[3] = {600, 1000, 5000};
|
||||
|
||||
// Speed section
|
||||
if (digitalRead(SPEED_PIN18) == LOW) {
|
||||
SPEED_INDEX--;
|
||||
if (SPEED_INDEX < 0)
|
||||
SPEED_INDEX = 2;
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
// Display
|
||||
switch (SPEED_INDEX) {
|
||||
case 0:
|
||||
digitalWrite(LA, LOW);
|
||||
digitalWrite(LB, HIGH);
|
||||
digitalWrite(LC, HIGH);
|
||||
digitalWrite(LD, LOW);
|
||||
digitalWrite(LE, LOW);
|
||||
digitalWrite(LF, LOW);
|
||||
digitalWrite(LG, LOW);
|
||||
digitalWrite(LDOT, LOW);
|
||||
break;
|
||||
case 1:
|
||||
digitalWrite(LA, HIGH);
|
||||
digitalWrite(LB, HIGH);
|
||||
digitalWrite(LC, LOW);
|
||||
digitalWrite(LD, HIGH);
|
||||
digitalWrite(LE, HIGH);
|
||||
digitalWrite(LF, LOW);
|
||||
digitalWrite(LG, HIGH);
|
||||
digitalWrite(LDOT, LOW);
|
||||
break;
|
||||
case 2:
|
||||
digitalWrite(LA, HIGH);
|
||||
digitalWrite(LB, HIGH);
|
||||
digitalWrite(LC, HIGH);
|
||||
digitalWrite(LD, HIGH);
|
||||
digitalWrite(LE, LOW);
|
||||
digitalWrite(LF, LOW);
|
||||
digitalWrite(LG, HIGH);
|
||||
digitalWrite(LDOT, LOW);
|
||||
break;
|
||||
}
|
||||
|
||||
if (digitalRead(LEFT_PIN20) == LOW) {
|
||||
digitalWrite(DIR_PIN7, HIGH);
|
||||
|
||||
digitalWrite(STEP_PIN5,HIGH);
|
||||
delayMicroseconds(micro_seconds[SPEED_INDEX]);
|
||||
digitalWrite(STEP_PIN5,LOW);
|
||||
delayMicroseconds(micro_seconds[SPEED_INDEX]);
|
||||
}
|
||||
|
||||
if (digitalRead(RIGHT_PIN19) == LOW) {
|
||||
digitalWrite(DIR_PIN7, LOW);
|
||||
|
||||
digitalWrite(STEP_PIN5,HIGH);
|
||||
delayMicroseconds(micro_seconds[SPEED_INDEX]);
|
||||
digitalWrite(STEP_PIN5,LOW);
|
||||
delayMicroseconds(micro_seconds[SPEED_INDEX]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user