Hi all I have a project that I was doing where when the 7 segment digit display shows a 3, a buzzer and stepper motor activate and do their thing. I have most to all of the code written but I cannot seem to get the motor and buzzer to activate when I need to. Thank you in advance for help.
#include "pitches.h"
const int a = 8; //For displaying segment "a"
const int b = 9; //For displaying segment "b"
const int c = 4; //For displaying segment "c"
const int d = 5; //For displaying segment "d"
const int g = 3; //For displaying segment "g"
bool bPress = false;
const int buttonPin = 10;
// Variables will change:
int buttonPushCounter = 1; // counter for the number of button presses
int buttonState = 1; // current state of the button
int lastButtonState = 0;
#define echoPin 12
#define trigPin 11
long duration;
int distance;
#define IN1 6
#define IN2 2
#define IN3 1
#define IN4 0
int Steps = 0;
boolean Direction = true;// gre
unsigned long last_time;
unsigned long currentMillis ;
int steps_left=4095;
long time;
int melody[]= {NOTE_E5, NOTE_G5, NOTE_E6, NOTE_C6, NOTE_D6, NOTE_G6
};
int noteDurations []={ 8, 8, 8, 8, 8, 8
};
void setup() {
// put your setup code here, to run once:
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
Serial.println("with Arduino UNO R3");
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
// delay(1000);
pinMode(a, OUTPUT); //A
pinMode(b, OUTPUT); //B
pinMode(c, OUTPUT); //C
pinMode(d, OUTPUT); //D
pinMode(g, OUTPUT); //G
pinMode( buttonPin , INPUT_PULLUP );
Serial.begin(9600);
displayDigit(buttonPushCounter);
}
void displayDigit(int digit)
{
//Conditions for displaying segment a
if(digit!=1 && digit != 4)
digitalWrite(a,HIGH);
//Conditions for displaying segment b
if(digit != 5 && digit != 6)
digitalWrite(b,HIGH);
//Conditions for displaying segment c
if(digit !=2)
digitalWrite(c,HIGH);
//Conditions for displaying segment d
if(digit != 1 && digit !=4 && digit !=7)
digitalWrite(d,HIGH);
//Conditions for displaying segment g
if (digit!=0 && digit!=1 && digit !=7)
digitalWrite(g,HIGH);
}
void stepper(int xw){
for (int x=0;x<xw;x++){
switch(Steps){
case 0:
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
break;
case 1:
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, HIGH);
break;
case 2:
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
break;
case 3:
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
break;
case 4:
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
case 5:
digitalWrite(IN1, HIGH);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
case 6:
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
case 7:
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
break;
default:
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
}
SetDirection();
}
}
void SetDirection(){
if(Direction==1){
Steps++;
}
if(Direction==0){
Steps--;
}
if(Steps>7){
Steps=0;
}
if(Steps<0){
Steps=7;
}
}
void turnOff()
{
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(g,LOW);
}
void loop() {
buttonState = digitalRead(buttonPin);
// step one revolution in one direction:
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
// Displays the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// put your main code here, to run repeatedly:
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == LOW) {
// if the current state is HIGH then the button went from off to on:
bPress = true;
buttonPushCounter++;
if( buttonPushCounter > 4) buttonPushCounter =0 ;
Serial.println("on");
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
if( bPress ){
turnOff();
displayDigit(buttonPushCounter);
}
if (buttonPushCounter == 3); {
if(buttonPushCounter == 3);
{
for (int thisNote = 0; thisNote < 6; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(13, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(13); }
while(steps_left>0){
currentMillis = micros();
if(currentMillis-last_time>=1000){
stepper(1);
time=time+micros()-last_time;
last_time=micros();
steps_left--;
}
}
}
}
}