Problem with my first program, please help.

Hi

Thanks for taking a look.

I have an Arduino 2560, two L298N boards and two momentary switches all connected up, I think I have just made some kind of simple error in the code.

Basically when I push the first button, I want all four motor connected to the two L298N boards to move the motors in the open louvres direction for 15 seconds. When I push the second button close the louvres.

When I push the buttons nothing happens.

Buttons are connected to 5v and one to digital pin 50 the other digital pin 51, no external resistor is being used just INPUT_PULLUP.

5v-----switch-----switch
\ --------pin 51
-------pin 50

I wired both switches from a single 5v pin because I have run out of 5v pins.

I did basic testing before adding in the buttons and the Arduino was sending the right signals to the L298N boards, as I could see the LEDs lighting properly and measure the voltage on the motor outputs.

The sketch verifies and uploads.

Thanks again.

Richard

// motor A
int AIN1=30;
int AIN2=31;
int AENA=42;
// motor B
int BIN1=32;
int BIN2=33;
int BENA=43;
// motor C
int CIN1=34;
int CIN2=35;
int CENA=44;
// motor D
int DIN1=36;
int DIN2=37;
int DENA=45;
// switches
int OPEN=50;
int CLOSE=51;
void setup()
{
// set all pins 
 pinMode(AIN1,OUTPUT);
 pinMode(AIN2,OUTPUT); 
 pinMode(AENA,OUTPUT);
 pinMode(BIN1,OUTPUT);
 pinMode(BIN2,OUTPUT); 
 pinMode(BENA,OUTPUT);
 pinMode(CIN1,OUTPUT);
 pinMode(CIN2,OUTPUT); 
 pinMode(CENA,OUTPUT);
 pinMode(DIN1,OUTPUT);
 pinMode(DIN2,OUTPUT); 
 pinMode(DENA,OUTPUT); 
 pinMode(OPEN,INPUT_PULLUP);
 pinMode(CLOSE,INPUT_PULLUP); 
}

void louvre_command(int mode) {
	switch(mode) {

		case 1:
			// close louvre
			digitalWrite(AENA,HIGH);// motor A enable 
			digitalWrite(AIN1,LOW);// rotate back
			digitalWrite(AIN2,HIGH);
			digitalWrite(BENA,HIGH);// motor B enable 
			digitalWrite(BIN1,LOW);// rotate back
			digitalWrite(BIN2,HIGH);
			digitalWrite(CENA,HIGH);// motor C enable 
			digitalWrite(CIN1,LOW);// rotate back
			digitalWrite(CIN2,HIGH);
			digitalWrite(DENA,HIGH);// motor D enable 
			digitalWrite(DIN1,LOW);// rotate back
			digitalWrite(DIN2,HIGH);
			delay(15000); 

			break;
		case 2:
			// open louvre
			digitalWrite(AENA,HIGH);// motor A enable 
 			digitalWrite(AIN1,HIGH);// rotate forward
 			digitalWrite(AIN2,LOW);
			digitalWrite(BENA,HIGH);// motor B enable 
 			digitalWrite(BIN1,HIGH);// rotate forward
			digitalWrite(BIN2,LOW);
			digitalWrite(CENA,HIGH);// motor C enable 
			digitalWrite(CIN1,HIGH);// rotate forward			
			digitalWrite(CIN2,LOW);
			digitalWrite(DENA,HIGH);// motor D enable 
			digitalWrite(DIN1,HIGH);// rotate forward
			digitalWrite(DIN2,LOW);
			delay(15000); 

			break;


}
}

void loop() {
	// Getting data from the buttons
	int open_press = digitalRead(OPEN);
	int close_press = digitalRead(CLOSE);

// the logic


	if(open_press == LOW && close_press == HIGH) {
		louvre_command(2);
	}

	if(open_press == HIGH && close_press == LOW) {
		louvre_command(1);
	}

	if(open_press == LOW && close_press == LOW) {
                // Do nothing....
		// louvre_command(0);
	}

	if(open_press == HIGH && close_press == HIGH) {
		// Do nothing....
		// louvre_command(0);
	}

	delay(500);
}

What do your debug prints tell you is happening?

Please use code tags when posting code.

Thanks AWOL, sorry I didn't know about code tags, that should be fixed.

I am not sure I understand about debug prints, if you mean that I should have bits of redundant code that spit the contents of variables out to the serial console, then my program doesn't have them, as the title says its it's my first sketch and I should add I am not a programmer and my programming skills are poor.

Thanks again.

Richard

You're switches are wired up wrong. You need them connected between input pin and ground and then enable the internal pull ups.

if you mean that I should have bits of redundant code that spit the contents of variables out to the serial console, then my program doesn't have them,

Quite obviously, they are not redundant, or you wouldn't be posting here.

Grumpy_Mike many thanks, that worked.

AWOL, sorry that came out wrong, the debug lines are not redundant for debugging.

Basically what happens now is the delay isn't working, I press either button and it works properly but doesn't stop after 15 seconds, my use of delay and break is perhaps wrong?

Thanks again guys!

Cheers
Richard

The output pins stay in the commanded state until you command them to another state.They don't turn off by themselves.

Thanks Herbie56!