Anybody willing to help me?

Hello there people anybody willing to help me with my project?
It involves some buttons buzzer and a stepper motor.
Discord: Alexnahui
Sincerely Alex.

A bit more detail would help. What exactly do you want to do?

Alexisnhi, welcome to the forum.

We would need to know exactly what you need help with. To help you get the best out of the forum, the mods here usually post the following link for newcomers:

It explains what information would be useful and indeed essential for us in order to be able to provide further help. Please review it and then post some more information about your project.

This is not discord.

That sounds like it could be an interesting project that might be a lot of fun! However, please keep in mind that we are not a free design or code-writing service. We’re more than happy to help with your design or code, but we need you to make an initial attempt. Please design and write your code, then post it along with an explanation of what’s not working properly.

  1. Show Your Work First: Before asking for assistance, make an attempt to design or write the code yourself. Share your work along with details about what isn’t working. Only you know what you want!
  2. Provide Clear Documentation: Since we can’t see your project, share an annotated schematic (best) or a clear drawing of your setup. Pictures are welcome, but avoid using Fritzing diagrams as they are wiring diagrams, not schematics, and are not ideal for troubleshooting.
  3. Include Technical Details: If there is specific hardware involved, include links to technical information. There are often many versions of similar components, so precise details are essential. What version of buttons buzzer and a stepper motor are we looking at.
  4. Reference Resources: For additional help, check out useful links and tutorials: Useful Links on Arduino Forum. Post links to your buttons buzzer and a stepper motor technical information.
  5. Obtain a copy: of the Arduino Cookbook and complete several of the projects. Many of them will help you achieve parts of your goals.
  6. Course Electronics For Beginners (eBook)
  7. Electronics For Beginners (eBook)
  8. Arduino Step-by-step Projects Course | Build 25 Projects

I came up with this, did sum myself, AI helped me aswell, i know it is messy. Any ideas how to improve this and get rid of junk code?

#include "pitches.h"


#define BUTTON_PIN1  12 // GPIO pin connected to button 1

#define LED_PIN1    15  // GPIO pin connected to LED 1

#define LEDWHITE    2   // GPIO pin connected to white LED

#define LEDYELLOW   0   // GPIO pin connected to yellow LED

#define LEDGREEN    4   // GPIO pin connected to green LED

#define BUTTON_PIN2 14  // GPIO pin connected to button 2


const int BUZZZER_PIN = 27;
const unsigned long beepInterval = 10000; // 10 seconds
unsigned long previousBeepTime = 0; // To keep track of the last beep time


// Melody for button 1 toggled

int toggleMelody[] = {

 NOTE_C5, NOTE_E5, NOTE_G5, NOTE_C5

};

int toggleDurations[] = {

 150, 150, 150, 150

};


// Melody for button 1 disabled

int disabledMelody[] = {

 NOTE_A4, NOTE_F4, NOTE_E4

};

int disabledDurations[] = {

 150, 150, 150

};


// variables will change:

int led_state1 = LOW;    // the current state of LED 1

int button_state1;       // the current state of button 1

int last_button_state1;  // the previous state of button 1

int button_state2 = HIGH; // Set initial button 2 state to HIGH (OFF)

int last_button_state2;  // the previous state of button 2

bool loopActive = false;  // Flag to track if the loop is active


void setup() {

 Serial.begin(9600);                // initialize serial communication

 pinMode(BUTTON_PIN1, INPUT_PULLUP); // set button 1 pin to input pull-up mode

 pinMode(LED_PIN1, OUTPUT);          // set LED 1 pin to output mode

 pinMode(BUTTON_PIN2, INPUT_PULLUP); // set button 2 pin to input pull-up mode

 pinMode(LEDWHITE, OUTPUT);          // set white LED pin to output mode

 pinMode(LEDYELLOW, OUTPUT);         // set yellow LED pin to output mode

 pinMode(LEDGREEN, OUTPUT);          // set green LED pin to output mode

 digitalWrite(LEDGREEN, HIGH);       // turn on green LED


 // Initialize button states

 button_state1 = digitalRead(BUTTON_PIN1);

 last_button_state2 = button_state2; 
 
   tone(27, NOTE_E5, 200);
 noTone(27);
 delay(200);
 tone(27, NOTE_G5, 200);
 noTone(27);
 delay(200);
 tone(27, NOTE_A5, 200);
 noTone(27);
 delay(200);
 tone(27, NOTE_E5, 200);
 noTone(27);

}


void playMelody(int *notes, int *durations, int size) {

 for (int i = 0; i < size; i++) {

   tone(BUZZZER_PIN, notes[i], durations[i]);

   delay(durations[i]);

   noTone(BUZZZER_PIN);

 }

}


void functionA() {

 digitalWrite(LEDWHITE, HIGH); // Turn on white LED

 delay(200);                   // Wait for 200 ms

 digitalWrite(LEDWHITE, LOW);  // Turn off white LED

 Serial.println("func called"); // Print message to serial

 loopActive = false;

 disableLED1(); // Call the function to disable LED1

}


void disableLED1() {

 led_state1 = LOW; 

 digitalWrite(LED_PIN1, led_state1);

}


void loop() {

 // Read button states

 last_button_state1 = button_state1;      // save the last state of button 1

 button_state1 = digitalRead(BUTTON_PIN1); // read new state of button 1


 // Button 1 pressed (Always check this)

 if (last_button_state1 == HIGH && button_state1 == LOW) {

   Serial.println("Button 1 is pressed");

   led_state1 = !led_state1 ; // Toggle LED state

   digitalWrite(LED_PIN1, led_state1); // Update LED state

   button_state2 = LOW; // Turn off button 2

   digitalWrite(BUTTON_PIN2, button_state2); // Update button 2 state

   last_button_state2 = button_state2;


   // Start the loop

   loopActive = true; 

   Serial.println("Loop is active");

   

   // Play toggled melody

   if (led_state1 == HIGH) {

     playMelody(toggleMelody, toggleDurations, sizeof(toggleMelody) / sizeof(int));

   }

 }


 // Check button 2 state ONLY if LED1 is ON

 if (led_state1 == HIGH) { 

   last_button_state2 = button_state2;      // save the last state of button 2

   button_state2 = digitalRead(BUTTON_PIN2);

   delay(100); // read new state of button 2


   // Button 2 pressed

   if (last_button_state2 == HIGH && button_state2 == LOW && loopActive) {  // Check loopActive

     Serial.println("Button 2 is pressed");

     functionA(); // Call functionA when button 2 is pressed

     button_state2 = HIGH; // Turn ON button 2 after functionA is called

     digitalWrite(BUTTON_PIN2, button_state2); // Update button 2 state

     last_button_state2 = button_state2;

   }

 }


 // Execute loop if it's active and LED1 is ON

 if (loopActive && led_state1 == HIGH) {

   Serial.println("Loop running...");
     digitalWrite(LEDWHITE, HIGH); // Turn on white LED

      delay(500);                   // Wait for 200 ms

      digitalWrite(LEDWHITE, LOW);  // Turn off white LED

   delay(500); 
        unsigned long currentMillis = millis();
   if (currentMillis - previousBeepTime >= beepInterval) {
     previousBeepTime = currentMillis; // Update the last beep time
     tone(BUZZZER_PIN, 1000, 200); // Beep sound (1000 Hz for 200 ms)
     noTone(BUZZZER_PIN);
   }

 } else if (loopActive) {

   // If the loop is active, but LED1 is OFF or functionA is called, stop the loop

   Serial.println("Loop is inactive");
   digitalWrite(LEDWHITE, LOW);  // Turn off white LED
   loopActive = false; 


 }


 // Play disabled melody only when transitioning from ON to OFF

 if (led_state1 == LOW && last_button_state1 == LOW) {

   playMelody(disabledMelody, disabledDurations, sizeof(disabledMelody) / sizeof(int));

 }

}

Not until you tell us what the code is supposed to be doing!

Hi, @alexisnhi
Welcome to the forum

What Arduino controller are you using/

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

not exactly arduino controller but close enough :slight_smile: esp32 devkit v1

HI,
Have you actually built your project or is this all speculation at the moment?

What is your project?
What is your code supposed to do?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

  • AI should be able to weed the garden. :thinking: