Simple Sound Sensor and Stepper Motor Code

My goal is to try to turn the sound sensor on and off using the code below. Seems such a simple task but still can't do it.

Here is my code:

#include <Stepper.h>

#include "Stepper.h"
#define STEPS 32 // Number of steps for one revolution of Internal shaft
// 2048 steps for one revolution of External shaft

volatile boolean TurnDetected; // need volatile for Interrupts
volatile boolean rotationdirection; // CW or CCW rotation

;int val;

const int PinCLK=2; // Generating interrupts using CLK signal
const int PinDT=3; // Reading DT signal

int StepsToTake; // How much to move Stepper

// Setup of proper sequencing for Motor Driver Pins
// In1, In2, In3, In4 in the sequence 1-3-2-4
Stepper small_stepper(STEPS, 8, 10, 9, 11);

// Interrupt routine runs if CLK goes from HIGH to LOW
void isr () {
delay(4); // delay for Debouncing
if (digitalRead(PinCLK))
rotationdirection= digitalRead(PinDT);
else
rotationdirection= !digitalRead(PinDT);
TurnDetected = true;
}

int SoundSensor = 7;
void setup()
{
pinMode(PinCLK,INPUT);
pinMode(PinDT,INPUT);
attachInterrupt (0,isr,FALLING); // interrupt 0 always connected to pin 2 on Arduino UNO

pinMode(SoundSensor, INPUT);
Serial.begin (9600);
}

void loop()
{
val = digitalRead(SoundSensor);
small_stepper.setSpeed(700); //Max seems to be 700
if (val = HIGH)
{ StepsToTake=-50;
small_stepper.step(StepsToTake);}
else
{
StepsToTake=0;
small_stepper.step(StepsToTake);}

digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
}

What does "turn the sound sensor on and off" actually mean? And what part of your code is trying to do it?

Steve

void isr ()  {
  delay(4);  // delay for Debouncing

You can't use delay in an ISR.

 if (val = HIGH)

Should this be == and not =

I can't understand what the interrupt is supposed to do or where these signals come from.

duplicate post
https://forum.arduino.cc/index.php?topic=695501.0

actually triplicate post
https://forum.arduino.cc/index.php?topic=695495.0

caleb_mathew7:
yea haha they're similiar code but could you help me out and take a look at my code?

Not unless you read the sticky posts at the top of the forum, regarding forum rules and etiquette.

caleb_mathew7:
could you tell me how to delete my other posts?

Forget about it. Just fix this one. Read the sticky posts, and edit your post to use code tags also.

I think when people read this, they will wonder why you didn't take the hint and edit the post to add code tags. It would demonstrate that you're sincere about following the rules.

Have you run any stepper library example sketches to verify that the stepper hardware works and is powered and connected properly?

There are no comments in the code (well, 2 lines... not enough). You haven't explained how it is supposed to work either.

caleb_mathew7:
Yes the Stepper motor works just fine, I used a rotary encoder with it and it worked perfectly. That's how I know the problem isn't with the motor it's with the code. Also I've tried a program with the sound sensor to tune it to the right frequency so that works too.

So look at the stepper code that works, to see how it's done. Obviously, you have done something fatally different.

Oh. Wait. I think you're actually telling me that you haven't run example sketches. Just used hardware connections to test the motor. Bad.

Okay, how many times do you want me to ask you to use code tags?

The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The "Code: [Select]" feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing or review.

If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower right corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

A good place to start would be to put in some Serial.print() statements to help you debug your program. How do you know if your sound sensor is giving you a HIGH or LOW reading and hence stepping your motor or not.

You also do not need to put all those pins low at the end of loop()

What type of sound sensor do you have? You have declared it as an input but some sensors float when not activated which means you need an external pull-up resistor or use the internal one by declaring the pin INPUT_PULLUP.

triple post
https://forum.arduino.cc/index.php?topic=695502.0

Thank you! much better.

The program you say doesn't operate the stepper lacks these calls:

   small_stepper.step(StepsToTake);

... which look to me like the method used to move the stepper, so...

3 threads on the same subject merged

Why did you post the same thing 3 times ?

Multiple posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated multiple posting will result in a timeout from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Is this a continuation of the other post like this one.

Do you think that the answers will be different from the many that you got already in the other thread(s)?

Yes, this would be the fourth thread. The first 3 got the OP a moderator warning.

Yet another duplicate

@caleb_mathew7 - be very careful. You are skating on thin ice posting starting duplicate threads