Created
July 15, 2017 11:26
-
-
Save ArduinoBasics/d6a449912684e8f4c96d3920fbc30e0a to your computer and use it in GitHub Desktop.
Using a Stepper motor as a musical instrument
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//================================================================================================ | |
// Title: Nextion Enhanced Stepper Motor Piano Project | |
// Author: ScottC, http://arduinobasics.blogspot.com | |
// Version: 1.0 | |
// Date: 15th July 2017 | |
// Arduino IDE: 1.8.1 | |
// Arduino Board: Arduino Uno | |
// Nextion Board: Nextion Enhanced NX4827K043 | |
// Description: The Arduino receives Serial messages from the Nextion board, and based on those messages, the Arduino will | |
// make the stepper motor move: | |
// a) Faster or Slower | |
// b) Left or Right | |
// c) at a specific speed such that it sounds like a Note from a keyboard. | |
// | |
//Libraries Required: | |
// AccelStepper Library: for easy Stepper Motor control | |
// SoftwareSerial Library: to enable Serial communication between the Arduino UNO and the Nextion board | |
//================================================================================================ | |
//Libraries | |
#include <AccelStepper.h> | |
#include <SoftwareSerial.h> | |
//Global variables | |
SoftwareSerial nSerial(10, 11); // Arduino Pins (10=RX, 11=TX) connected to Nextion wires (TX=Blue, RX=Yellow)respectively | |
AccelStepper stepperMotor; // Arduino Digital Pins 2,3,4,5 connected to Duinotech Pins IN1,IN2,IN3,IN4 respectively | |
#define EnablePin 7 // Arduino Digital Pin 7 connected to both Duinotech Pins ENA and ENB | |
int stepperSpeed = 415; // Default stepper speed on start up | |
int stepperMaxSpeed = 1000; // Maximum stepper speed | |
int stepperMinSpeed = 375; // Minimum stepper speed (can be set to zero if required) | |
int stepperDirection = 1; // 1 = Left, -1= Right | |
byte readMsg = 0; // Used to store the message sent by the Nextion board | |
//The following notes[] array, holds the motor speeds required to produce each note in key: | |
// A Bb B C C# D Eb E F F# G Ab A Bb B (C) | |
int notes[] = {0, 443, 470, 495, 525, 557, 590, 625, 667, 710, 757, 800, 845, 890, 940, 1000, 1050}; | |
//===================================================================================================== | |
// setup(): | |
// This runs once only | |
// Setup Serial communication between the Arduino and Computer, as well as SoftwareSerial for | |
// communication between the Arduino and the Nextion Enhanced board. | |
// Setup the stepper motor speeds (default speed and Maximum speed) | |
// Configure the "Enable Pin" and set it to LOW to turn motor off for now (reduces power consumption) | |
//----------------------------------------------------------------------------------------------------- | |
void setup() { | |
Serial.begin(9600); //Arduino <--> Computer | |
nSerial.begin(9600); //Arduino <--> Nextion Enhanced | |
stepperMotor.setMaxSpeed(stepperMaxSpeed); //Set the Maximum speed of the stepper motor | |
stepperMotor.setSpeed(stepperSpeed); //Set the default speed of the stepper motor | |
pinMode(EnablePin, OUTPUT); //Set digital pin 7 (EnablePin) as an OUTPUT | |
digitalWrite(EnablePin, LOW); //Set the Enable Pin LOW to turn motor OFF | |
} | |
//===================================================================================================== | |
// loop(): | |
// This code runs in an endless loop | |
// Read any Serial messages from the Nextion board, and control the stepper motor accordingly | |
//----------------------------------------------------------------------------------------------------- | |
void loop() { | |
if (nSerial.available()) { //If there are any Serial messages available from the Nextion board | |
readMsg = nSerial.read(); //Read the message and store it in the variable called "readMsg" | |
if(readMsg>0&&readMsg<17){ //If the message is a number between 1 and 16 (inclusive) - e.g. Messages from Interface2 (Piano screen) | |
digitalWrite(EnablePin, HIGH); //Turn on the stepper motor, by setting the EnablePin to HIGH | |
stepperMotor.setSpeed(notes[readMsg]);//and run the motor at a speed defined within the notes[] array. | |
} else { //Otherwise | |
digitalWrite(EnablePin, LOW); //Turn off the stepper motor by setting the EnablePin to LOW | |
switch(readMsg){ //Use switch-case to perform specific tasks based on the message received by the Nextion - these are the messages from Interface1 (Stepper Motor Controller Screen). | |
case 0x00: | |
//do nothing | |
digitalWrite(EnablePin, HIGH); //We receive three 0x00 messages after each number sent from the piano, so we want the motor to stay on, but that is all. | |
break; | |
case 76: //LEFT // "L" = 76, so we want to to turn the motor LEFT | |
digitalWrite(EnablePin, HIGH); // So enable the motor by setting the EnablePin HIGH | |
stepperDirection = 1; // Set the stepperDirection to 1 (LEFT) | |
stepperMotor.setSpeed(stepperSpeed * stepperDirection); //Set the speed of the motor in the LEFT direction | |
break; | |
case 82: //RIGHT // "R" = 82, so we want to to turn the motor RIGHT | |
digitalWrite(EnablePin, HIGH); // So enable the motor by setting the EnablePin HIGH | |
stepperDirection = -1; // Set the stepperDirection to -1 (RIGHT) | |
stepperMotor.setSpeed(stepperSpeed * stepperDirection); //Set the speed of the motor in the RIGHT direction | |
break; | |
case 70: //FASTER // "F" = 70, so we want to to turn the motor FASTER | |
digitalWrite(EnablePin, HIGH); // So enable the motor by setting the EnablePin HIGH | |
stepperSpeed = stepperSpeed + 5; // Increase the speed of the motor by 5 | |
if(stepperSpeed>stepperMaxSpeed){ // If the stepper motor speed exceeds the maximum speed limit, | |
stepperSpeed = stepperMaxSpeed; // then keep it at the Maximum speed. | |
} | |
stepperMotor.setSpeed(stepperSpeed * stepperDirection); //Set the motor to the new speed (same direction) | |
Serial.print("Stepper Motor Speed changed to "); //Print the new stepper motor speed to the Serial monitor window. | |
Serial.println(stepperSpeed); | |
break; | |
case 83: //SLOWER // "S" = 83, so we want to to turn the motor SLOWER | |
digitalWrite(EnablePin, HIGH); // So enable the motor by setting the EnablePin HIGH | |
stepperSpeed = stepperSpeed - 5; // Reduce the speed of the motor by 5 | |
if(stepperSpeed<stepperMinSpeed){ // If the stepper motor speed is lower than the MINIMUM limit, | |
digitalWrite(7, LOW); // then STOP the motor. | |
} | |
stepperMotor.setSpeed(stepperSpeed * stepperDirection); //Set the motor to the new speed (same direction) | |
Serial.print("Stepper Motor Speed changed to "); //Print the new stepper motor speed to the Serial monitor window. | |
Serial.println(stepperSpeed); | |
break; | |
default: | |
//Serial.println(readMsg); //Used for debugging messages from Nextion | |
break; | |
} | |
} | |
} | |
stepperMotor.runSpeed(); //This line must be called frequently for the motor to spin at the pre-defined speed | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment