1

I have project by Arduino mega which have 22 inputs and 22 outputs and buzzer reset and output reset pushbuttons , it have two status

  1. if the input is high(not continuous ) the output will on and the buzzer will be on, if output reset pushbutton is pressed the output will off , if buzzer reset pushbutton is pressed the buzzer will off.
  2. if the input is always high (continuous high signal on input ) the output will on and the buzzer will be on, in this case we will not reset the output even if the output reset pushbutton is pressed , and we will be able to reset the(turn off) buzzer by the buzzer reset pushbutton, and wait until input state to change to high again to repeat procedure

help me with editing my code


    //int input[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,23,24}; //input pins (23 for reset ,24 for buzzer reset)
//int output[] = {26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 24, 34, 44, 45, 46, 47, 48,49}; //output pins (49 for buzzer)

int in1 =     2;  

int in25 =   26;// for output reset
int in26 =   27;// for buzzer reset

///////////////

int out1 = 28   ;

int out24= 51   ;
int out25= 52   ; // output for buzzer

////////////////////////////////////

int buttonState1 =  0;    

int buttonState24 =  0;    
int buttonState25 =  0; //for  reset
int buttonState26 =  0; //for buzzer reset

//unsigned long previousMillis = 0;        // will store last time buzzer was updated
//const long interval = 10000;           // interval at which to buzzer (milliseconds)
//int buzzerState = LOW;             // buzzerState used to set the buzzer


void setup() {


  pinMode(in1 , INPUT);
 
  pinMode(in24 , INPUT);
  pinMode(in25 , INPUT);
  pinMode(in26 , INPUT);

///////////////////////////

pinMode(out1, OUTPUT);
pinMode(out2, OUTPUT);

pinMode(out24, OUTPUT);
pinMode(out25, OUTPUT);

digitalWrite(out25, LOW); // buzzer LOW

}

void loop() {
  buttonState1 = digitalRead(in1);
  if (buttonState1 == HIGH) {
    digitalWrite(out1, LOW);
    //digitalWrite(out25, LOW);
    //delay(10000);
    //digitalWrite(buzzer, LOW);
  }

  buttonState25 = digitalRead(in25); //read reset button state from input (25) pin 26
  if (buttonState25 == HIGH )

  {
    digitalWrite(out1, HIGH); //reset out1
    //digitalWrite(buzzer, LOW);
  }
  
  //else {
   // buttonState26 = digitalRead(in26); //read the buzzer reset button state from input (26)pin 27
   // if (buttonState26 == HIGH) {
    //  digitalWrite(out25, HIGH);

   // }
  //}

    //   while (digitalRead(input1) == HIGH);
    //digitalWrite(buzzerrelay, LOW);




    ////////////////////////////////////////////////////////////

 buttonState2 = digitalRead(in2);
  if (buttonState2 == HIGH) {
    digitalWrite(out2, LOW);
   // digitalWrite(out25, LOW);
    //delay(10000);
    //digitalWrite(buzzer, LOW);
  }

  buttonState25 = digitalRead(in25); //read reset button state
  if (buttonState25 == HIGH )

  {
    digitalWrite(out2, HIGH); //reset the output
    //digitalWrite(buzzer, LOW);
  }
  
 // else {
  //  buttonState26 = digitalRead(in26); //read the buzzer reset button state
    //if (buttonState26 == HIGH) {
    //  digitalWrite(out25, HIGH);

    //}
 // }

    
   //////////////////////////////////////////////////////////////
     
 
  //  }
 // }


  
  }

7
  • your code is definitely overly repetitive ... why did you opt to not use arrays, such as the first two lines of your code, which you commented out? .... it is definitely simpler to use in[1], in[2], etc. because you can use a for loop to access the variables... you cannot use a loop for in1, in2, etc. so you are forced to repeat the same code multiple times – jsotola Jan 23 at 20:08
  • What is the problem with your code? Is it not working? If yes, please explain, what you expected and what it actually does – chrisl Jan 23 at 20:23
  • i have tried to use arrays put they do not worked properly because i am new to Arduino and arrays , and now I'm focusing on solving buzzer problem – eng.hamza Jan 23 at 20:24
  • it is working put the buzzer part is not working ,,the problem that if the input is contentious the buzzer could not be reset and still ON until the input goes low after that i can reset the buzzer – eng.hamza Jan 23 at 20:30
  • 1
    You posted over 850 lines of code. It's unrealistic to expect your readers to review and post suggestions on that much code. You need to simplify and narrow your question, and provide a smaller example project that illustrates what you are struggling with. – Duncan C Jan 23 at 20:31
2

Any time you have the same sequence of code repeated over and over with slight variations, that is a "code smell" (an indication that you may be using the wrong approach.)

You should use arrays of pin numbers as you have in the first couple of commented-out lines, and write code that loops through those arrays, as well as functions that take parameters and can do the same thing using different values that are passed in.

Don't try to tackle a problem like this without learning the skills and concepts you need. You said "...I am new to Arduino and arrays , and now I'm focusing on solving buzzer problem." That is a mistake. Code like you have posted is hard to write, hard to read, and much harder to maintain.

1

Here is your code simplified. (untested)

Since the pins are sequential, you could calculate pin numbers, instead of using an array for pin number lookup.

For example, you could use a loop for (int i = 1; i < 25; i++) and first input pin would be referenced as i + 1 and first output pin would be referenced as i + 27.

// https://arduino.stackexchange.com/questions/80972/arduino-mega-buzzer-output

//  int in25 = 26;                           // for output reset
//  int in26 = 27;                           // for buzzer reset

//  int out25 = 52   ;                       // output for buzzer

//  int buttonState25 =  0;                  // for  reset
//  int buttonState26 =  0;                  // for buzzer reset

//  arrays are zero indexed (first element is at index zero)
//  array index      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26

    int in [27] = {255,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27};   // input  pin numbers (26 for reset, 27 for buzzer reset)
    int out[26] = {255, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 24, 34, 44, 45, 46, 47, 48, 49, 50, 51, 52    };   // output pin numbers (52 for buzzer)

    int buttonState[27];
    
//  unsigned long previousMillis = 0;        // will store last time buzzer was updated
//  const long interval          = 10000;    // interval at which to buzzer (milliseconds)
//  int buzzerState              = LOW;      // buzzerState used to set the buzzer

    
    void setup() {

      for (int i = 1; i < 27; i++) { buttonState[i] = 0;      }       // clear states of buttons 1 to 26
      for (int i = 1; i < 26; i++) { pinMode(out[i], OUTPUT); }       // configure outputs 1 to 25

      digitalWrite(out[25], LOW);                                     // buzzer LOW

    }

    void loop() {

      for (int i = 1; i < 25; i++) {                                  // loop buttons 1 to 24
        
        buttonState[i] = digitalRead(in[i]);

        if (buttonState[i] == HIGH) {
          digitalWrite(out[i], LOW);
//        digitalWrite(out[25], LOW);
//        delay(10000);
//        digitalWrite(buzzer, LOW);
        }
     
        buttonState[25] = digitalRead(in[25]);       // read reset button state

        if (buttonState[25] == HIGH ) {
          digitalWrite(out[i], HIGH);                // reset the output
//        digitalWrite(buzzer, LOW);
        }
     
//      else {
//        buttonState[26] = digitalRead(in[26]);     // read the buzzer reset button state
//        if (buttonState[26] == HIGH) {
//          digitalWrite(out[25], HIGH);
//        }
//      }
      }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.