Project Guidance

Hello All,
I am very new to arduino and its programming.
I have to create the following:

  1. There are 5 inputs as switch and 5 inputs as led.
  2. There are 1 input as a push button and on input as a buzzer.
  3. Total 6 inputs and 6 outputs naming from I1 to I6 resp and O1 to O6.
  4. Now when I1 is High O1 and O6 should be high.
  5. When I6(push button) is pressed O6 should be Low.
  6. O1 will become low when I1 will become Low.
  7. This should happen with all the inputs.
  8. Now if O1 is high and O6 is low and if I2 becomes high than again O6 should become high and O2 should become High.
    8 ) O6 again becomes low when I6(Pushbutton) is pressed.

Tried this thing with nested if but not possible from my side please help.

You need to post your code and say exactly what's (not) happening.....

Can Someone help me with the code. It will be of great help.

akshay_90:
Can Someone help me with the code. It will be of great help.

Helping means helping, not supplying.

Post what you have, say where it goes right / wrong, explain where you're stuck, then you'll get help.

Right now, you're asking for a ready-made solution, that's not likely to happen.

const int sd = 2;
const int acc = 3;
const int sd2 = 4;
const int led = 13;
const int buzzer = 12;
const int led1 = 11;
int b = 0;
int acc1 =0;
int bu1;

void setup() {
// initialize digital pin 13 as an output.
pinMode(led, OUTPUT);
pinMode(led1,OUTPUT);
pinMode(buzzer,OUTPUT);
pinMode(sd, INPUT);
pinMode(acc,INPUT);
pinMode(sd2,INPUT);
}

// the loop function runs over and over again forever
void loop() {
b = digitalRead(sd);
acc1 = digitalRead(acc);
bu1 = digitalRead(sd2);
if(b == HIGH && acc1 == LOW){
digitalWrite(led, HIGH);
digitalWrite(buzzer,HIGH);
}

else if(b == LOW && acc1 == HIGH){
digitalWrite(led, LOW);
digitalWrite(buzzer,LOW);

}
else if(b == LOW && acc1 == LOW){
digitalWrite(buzzer, LOW);
digitalWrite(led, LOW);

}
else if(b==HIGH && acc1==HIGH){
digitalWrite(buzzer, LOW);
digitalWrite(led, HIGH);

}
if(bu1 == HIGH && acc1 == LOW){
digitalWrite(led1, HIGH);
digitalWrite(buzzer,HIGH);
}

else if(bu1 == LOW && acc1 == HIGH){
digitalWrite(led1, LOW);
digitalWrite(buzzer,LOW);

}
else if(bu1 == LOW && acc1 == LOW){
digitalWrite(buzzer, LOW);
digitalWrite(led1, LOW);

}
else if(bu1==HIGH && acc1==HIGH){
digitalWrite(buzzer, LOW);
digitalWrite(led1, HIGH);

}
}

I have the following code for 3 inputs and 3 outputs. Now my trouble is when i download this on arduino uno board when i give 5 v to pin 2 the led on pins 13 and 12 glow. but when i give 5 v to pin 4 led doesnt glow.

akshay_90:
Tried this thing with nested if but not possible from my side please help.

You are thinking about your program the wrong way.

A program is based on data structures and algorithms.

It’s up to you providing these two things, the program will then do the rest.

A good data structure for a whole bunch of mechanical switches and buttons is an “array”.

A good algorithm to work over an arrray is a “for-loop”.

A good algorithm for the loop() function with Arduino sketches is the IPO programming model:

  • input
  • programming
  • output

You just need to write three functions of your own, the loop then always looks like this:

void loop() {
  input();
  processing();
  output();
}

And following the three steps over and over again

  • obtain input from the outer world outside the Arduino
  • do internal processing within the Arduino RAM
  • send output to the world outside the Arduino
    you can write any program you want.

To give you a starting point, I’ll provide you my default sketch that can read the input of as many buttons and switches as you like to have. Debouncing of mechanical switches may be tricky, so the input function is tricky, too. But you just need copy-and-paste, so the “input()” function for your program is ready without any work for you by just providing the correct pin numbers for all the buttons and switches:

#define INPUTMODE INPUT_PULLUP    // INPUT or INPUT_PULLUP
#define BOUNCETIME 5              // bouncing time in milliseconds
byte buttonPins[]={2, 3, 4, 5, 6, 7};// pin numbers of all buttons
#define NUMBUTTONS sizeof(buttonPins) // number of buttons (automatically calculated)
byte buttonState[NUMBUTTONS];  // array holds the actual HIGH/LOW states
byte buttonChange[NUMBUTTONS]; // array holds the state changes when button is pressed or released
enum{UNCHANGED,BUTTONUP,BUTTONDOWN};

void input(){
// read the input state and state changes of all buttons
  static unsigned long lastButtonTime; // time stamp for remembering the time when the button states were last read
  memset(buttonChange,0,sizeof(buttonChange)); // reset all old state changes
  if (millis()-lastButtonTime<BOUNCETIME) return;  // within bounce time: leave the function
  lastButtonTime=millis(); // remember the current time
  for (int i=0;i<NUMBUTTONS;i++) 
  {
    byte curState=digitalRead(buttonPins[i]);        // current button state
    if (INPUTMODE==INPUT_PULLUP) curState=!curState; // logic is inverted with INPUT_PULLUP
    if (curState!=buttonState[i])                    // state change detected
    {
      if (curState==HIGH) buttonChange[i]=BUTTONDOWN;
      else buttonChange[i]=BUTTONUP;
    }
    buttonState[i]=curState;  // save the current button state
  }
}


void output(){
// send a message to Serial if a button state (pressed/released) has changed
// button pressed: Send button pin number with minus sign
// button released: Send button pin number
  byte action;
  for (int i=0;i<NUMBUTTONS;i++)
  {
    switch (buttonChange[i])  
    {
      case BUTTONUP: Serial.println(buttonPins[i]);break;
      case BUTTONDOWN: Serial.println(-buttonPins[i]);break;
    }
  }
}


void setup() {
  Serial.begin(9600); // initialize Serial at 9600 baud
  // then initialize all buttons
  for (int i=0;i<NUMBUTTONS;i++) pinMode(buttonPins[i],INPUTMODE);
}

void loop() {
  input();
  // processing(); // if you need any processing, write a function for it!  
  output();
}

The output() function currently just shows what’s going on and sends messages to Serial when you press or release a button or a switch changes its state.

The INPUTMODE must be defined accordingly to your circuit:
If you should connect the buttons/switches just from GND to the input pin, use INPUT_PULLUP.
If you use additional “pull-down” resistors in your circuit, use INPUT.

So perhaps this might be a starting point for you, to work out the “processing()” and “output()” logic that you need for your sketch.

You need to use serial to put some prints in appropriate places so you can see what the values of all those variables are, and to see there the logic is actually leading you.

Do you have any pullups or pulldowns on the inputs? Easiest is to use pinMode(x, INPUT_PULLUP) and not have to worry with external components.

Perhaps the Thread planning and implementing a program will give you some ideas.

...R