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);
}