Hello , I am on a journey to ensemble a laser harp with the help of a A4988 driver and a nema 17 stepper motor
Since the code operates 4 output pins instead of only step and sirection I was wondering how can it be modified in order for it to work with the driver
int delaylaser = 4;
int delay_motor = 6;
int LaserState = LOW;
int LaserPin = 7;
int threshold=220; // Threshold for the light readings
int sensor=0;
int note5 = 0x37;
int note4 = 0x34;
int note3 = 0x32;
int note2 = 0x30;
int note1 = 0x2F;
int state=1; // Motor status
int a = 0, b = 0, c = 0, d = 0, e = 0; // Note status
void forward()
{
switch(state){
case 1:
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
delay(delay_motor);
state=2;
break;
case 2:
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(delay_motor);
state=3;
break;
case 3:
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
delay(delay_motor);
state=4;
break;
case 4:
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
delay(delay_motor);
state=1;
break;
}
}
void backward()
{
switch(state){
case 3:
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
delay(delay_motor);
state=2;
break;
case 4:
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(delay_motor);
state=3;
break;
case 1:
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
delay(delay_motor);
state=4;
break;
case 2:
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
delay(delay_motor);
state=1;
break;
}
}
void setup()
{
Serial.begin(9600);
pinMode(2, OUTPUT); // Motor pins
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(LaserPin, OUTPUT); // Laser pin
pinMode(13, OUTPUT); // Status led.
// Position calibration
digitalWrite(LaserPin, HIGH);
// The motor goes forward until the sensor receives light
// In that moment, the mirror will be perpendicular to the beam
while(sensor<threshold)
{
forward();
sensor=analogRead(0);
}
// Once calibrated, the motor goes backward to its initial position
int i;
for(i = 0; i < 8; i++) {
backward();
}
}
void noteOn(int cmd, int pitch, int velocity) // Function to play the notes
{
Serial.write(cmd);
Serial.write(pitch);
Serial.write(velocity);
}
void loop()
{
digitalWrite(LaserPin, HIGH);
delay(delaylaser);
if(analogRead(0) > threshold) // If the sensor gets a signal
{
if(b+c+d+e == 0) { // If there are not any notes playing
if(a == 0) { // If this note is not being played
digitalWrite(13, HIGH); // Switch on status led
noteOn(0x90, note1, 0x7F); // Play note 1
}
a = 1;
}
}
else if(analogRead(0) < threshold) // If the sensor does not get a signal:
{
if(a >= 1) { // If this note is being played
if(++a == 3) { // If we have not had any readings for 3 cycles
digitalWrite(13, LOW); // Switch off the status led.
noteOn(0x90, note1, 0x00); // Stop playing note 1.
a = 0;
}
}
}
digitalWrite(LaserPin, LOW);
forward();
// The next steps are similar to the previous one. Each one of them corresponds
// to a different note
digitalWrite(LaserPin, HIGH);
delay(delaylaser);
if(analogRead(0) > threshold)
{
if(a+c+d+e == 0) {
if(b == 0) {
digitalWrite(13, HIGH);
noteOn(0x90, note2, 0x7F);
}
b = 1;
}
}
else if(analogRead(0) < threshold)
{
if(b >= 1) {
if(++b == 3) {
digitalWrite(13, LOW);
noteOn(0x90, note2, 0x00);
b = 0;
}
}
}
digitalWrite(LaserPin, LOW);
forward();
digitalWrite(LaserPin, HIGH);
delay(delaylaser);
if(analogRead(0) > threshold)
{
if(a+b+d+e == 0) {
if(c == 0) {
digitalWrite(13, HIGH);
noteOn(0x90, note3, 0x7F);
}
c = 1;
}
}
else if(analogRead(0) < threshold)
{
if(c >= 1) {
if(++c == 3) {
digitalWrite(13, LOW);
noteOn(0x90, note3, 0x00);
c = 0;
}
}
}
digitalWrite(LaserPin, LOW);
forward();
digitalWrite(LaserPin, HIGH);
delay(delaylaser);
if(analogRead(0) > threshold)
{
if(a+b+c+e == 0) {
if(d == 0) {
digitalWrite(13, HIGH);
noteOn(0x90, note4, 0x7F);
}
d = 1;
}
}
else if(analogRead(0) < threshold)
{
if(d >= 1) {
if(++d == 3) {
digitalWrite(13, LOW);
noteOn(0x90, note4, 0x00);
d = 0;
}
}
}
digitalWrite(LaserPin, LOW);
forward();
digitalWrite(LaserPin, HIGH);
delay(delaylaser);
if(analogRead(0) > threshold)
{
if(a+b+c+d == 0) {
if(e == 0) {
digitalWrite(13, HIGH);
noteOn(0x90, note5, 0x7F);
}
e = 1;
}
}
else if(analogRead(0) < threshold)
{
if(e >= 1) {
if(++e == 3) {
digitalWrite(13, LOW);
noteOn(0x90, note5, 0x00);
e = 0;
}
}
}
digitalWrite(LaserPin, LOW);
// Once it reaches the last note, the motor moves backwards
// until it reaches the first note again
backward();
digitalWrite(LaserPin, HIGH);
delay(delaylaser);
if(analogRead(0) > threshold)
{
if(a+b+c+e == 0) {
if(d == 0) {
digitalWrite(13, HIGH);
noteOn(0x90, note4, 0x7F);
}
d = 1;
}
}
else if(analogRead(0) < threshold)
{
if(d >= 1) {
if(++d == 3) {
digitalWrite(13, LOW);
noteOn(0x90, note4, 0x00);
d = 0;
}
}
}
digitalWrite(LaserPin, LOW);
backward();
digitalWrite(LaserPin, HIGH);
delay(delaylaser);
if(analogRead(0) > threshold)
{
if(a+b+d+e == 0) {
if(c == 0) {
digitalWrite(13, HIGH);
noteOn(0x90, note3, 0x7F);
}
c = 1;
}
}
else if(analogRead(0) < threshold)
{
if(c >= 1) {
if(++c == 3) {
digitalWrite(13, LOW);
noteOn(0x90, note3, 0x00);
c = 0;
}
}
}
digitalWrite(LaserPin, LOW);
backward();
digitalWrite(LaserPin, HIGH);
delay(delaylaser);
if(analogRead(0) > threshold)
{
if(a+c+d+e == 0) {
if(b == 0) {
digitalWrite(13, HIGH);
noteOn(0x90, note2, 0x7F);
}
b = 1;
}
}
else if(analogRead(0) < threshold)
{
if(b >= 1) {
if(++b == 3) {
digitalWrite(13, LOW);
noteOn(0x90, note2, 0x00);
b = 0;
}
}
}
digitalWrite(LaserPin, LOW);
backward();
}