# Will this code work propertly?

#### Beginner0001

Joined Sep 29, 2017
18
I am working with ATmega16. I have 4 switches and 4 LEDs. I want to turn off the first LED with the first switch, the second one with the second switch, and so on. When I turn off the switch, LED should turn on. All switches connected to PORTA, all LEDs to PORTB. Initially, all LEDs are on. Will this code do the job?

Code:
PORTB=0b00001111;

if (PINA & (1<<n))
{
PORTB = PORTB & ~(1 << n)
}
else
{
PORTB = PORTB | (1 << n);
}
}

#### ApacheKid

Joined Jan 12, 2015
385
I am working with ATmega16. I have 4 switches and 4 LEDs. I want to turn off the first LED with the first switch, the second one with the second switch, and so on. When I turn off the switch, LED should turn on. All switches connected to PORTA, all LEDs to PORTB. Initially, all LEDs are on. Will this code do the job?

Code:
PORTB=0b00001111;

if (PINA & (1<<n))
{
PORTB = PORTB & ~(1 << n)
}
else
{
PORTB = PORTB | (1 << n);
}
}
If that's the code in its entirety then I'd be surprised if it "works" at all.

Unless that some kind of interrupt handler it will not run at all - except when the app starts perhaps.

Surely you need a loop to poll the input port? unless you repeatedly examine the input port the code will have no way to react to any changes arising from the switch press.

#### KeithWalker

Joined Jul 10, 2017
1,780
Can you show us a diagram of how the switches and LEDs are wired to the Arduino please. I cannot comment on the function software until I know that the hardware is correct.

#### BobaMosfet

Joined Jul 1, 2009
1,773
I am working with ATmega16. I have 4 switches and 4 LEDs. I want to turn off the first LED with the first switch, the second one with the second switch, and so on. When I turn off the switch, LED should turn on. All switches connected to PORTA, all LEDs to PORTB. Initially, all LEDs are on. Will this code do the job?

Code:
PORTB=0b00001111;

if (PINA & (1<<n))
{
PORTB = PORTB & ~(1 << n)
}
else
{
PORTB = PORTB | (1 << n);
}
}
This is a fine example where anyone serious at programming will learn how to flow-chart. Flow-charting was specifically intended to help people document processes and prove logic.

Here is free, online flow-charting software- you can save your flowcharts to your own hard-drive, too.

https://app.diagrams.net/

Lastly, as others have requested, please show how you are wiring (a schematic) your circuit so we know how the switches are connected, and the LEDs.

#### Beginner0001

Joined Sep 29, 2017
18
Can you show us a diagram of how the switches and LEDs are wired to the Arduino please. I cannot comment on the function software until I know that the hardware is correct.

That's how all four switches and LEDs are connected.

If that's the code in its entirety then I'd be surprised if it "works" at all.
That's not the whole code, just function, I forgot to add that part.

Code:
void LED (int n)
{
PORTB=0b00001111;

if (PINA & (1<<n))
{
PORTB = PORTB & ~(1 << n)
}
else
{
PORTB = PORTB | (1 << n);
}
}
}

#### MrChips

Joined Oct 2, 2009
23,519
If I were to do this I would declare definitions for SW1 and LED1, etc.
Then the code would look something like:
C:
void main(void)
{
Init();
while (1)
{
LED1 = SW1;
LED2 = SW2;
LED3 = SW3;
LED4 = SW4;
}
}