Having trouble turning two simultaneous LED's on with DIP switch

Very new to c and having trouble with this project. Using Romeo Development Board w/ atmega328. Am able to bring on the appropriate LEDs on with individual DIP switches are engaged. But simultaneously together, I cannot get the required outcome. Any help would be very much appreciated. The 3rd else if statement is where I’m stumped.

Thanks in advance

Atmega328p Romeo board IO Board Jumper Component
PD2 D2 JP3_3 D1A
PD3 D3 JP3_4 D1B
PD4 D4 JP3_5 D1C
PD5 D5 JP3_6 D1D
PD6 D6 JP3_7 D1E
PD7 D7 JP3_8 D1F
PB0 D8 JP3_9 D1G
PB1 D9 JP3_10 D1H

PB2 D10 JP_2 SW_3 
PB3 D11 JP_2 SW_2
PB4 D12 JP_2 SW_1

#include <avr/io.h>
#define F_CPU 16000000UL
#include <util/delay.h>

void delay_ms (uint16_t ms);
void delay_us (uint16_t us);

int main(void) 

DDRD = 0b11111100; //Set PD2..PD7 for output

DDRB = 0b00000011; //Set PB0 & PB1 for output & Simultaneously set PB2..PB4
//for input


if ((PINB & 0x04)==0) // When switch for SW3 is engaged
PORTB = 0b00000110; // 1 LED turned on
else if ((PINB & 0x08)==0) // When switch for SW2 is engaged
PORTB = 0b00001011; // 2 LEDs turned on
else if ((PINB & 0x10)==0) // When switch for SW1 is engaged
PORTB = 0b00010011; // 4 LEDs turned on
PORTD = 0b11000000;
else if ((PINB & 0x0c)==0) // When switch for SW3 & SW2 is engaged
PORTB = 0b00001111; // Can't get 3 LEDs to come on simultaneously
PORTD = 0b10000000;
PORTB = 0b00011100; // LEDs off
PORTD = 0b00000000; // LEDs off
return 0;

void delay_ms (uint16_t ms) {
uint16_t i;
for (i = 0; i < ms; i++)

void delay_us (uint16_t us) {
uint16_t i;

for (i = 0; i < us; i++)

JulyLab2c.c (1.45 KB)

Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you post a link to “Romeo Development Board w/ atmega328” that you are using please?

Thanks… Tom… :slight_smile:

Hi Tom. Thanks for the reply and info on posting here. I will try to proceed accordingly :). I’m gonna post a picture of the circuit I’ve drawn(very 2nd grader-esque). The jumpers to LEDs & DIP switches belong to an I/O Trainer. I’m also attaching a .pdf of the Romeo Board with links embedded within for pinouts and schematics…etc.

Thanks again.

DownloadAttachment.pdf (48 KB)

Sorry. This attachment didn’t go through the first time. It’s the Romeo Board .pdf with specs and pinout links.

Romeo - The First Arduino Robot Control Board - DFRobot.pdf (308 KB)

Thanks for the info.

When you turn your dip switches OFF, have you got pull-down or pull-up resistors on the controller inputs?
You cannot leave an input that you are using open circuit.

Tom... :slight_smile:

Put a Serial.print statement in at that point to check if that condition is ever true, might shed some light on things

Thanks Tom. The controller has pull up resistors which have been set up in the code for the pins (PB2 to PB4) that are connected to all three DIP switches.

are your comments back the front? Is it 3 LEDs or 4 that will not light simultaneously?

Have you checked to see if the condition becomes true?

What is confusing is your controller is basically a UNO with C++ with a robot motor controller board on top, yet you are programming like it is a C machine.

Are you using the Arduino IDE? main() loop is not used in Arduino.

I notice that the delay functions you have coded are blocking code, while the delay loop is executed the rest of your code is not running.

Can you please tell us your electronics, programming, Arduino, hardware experience?

Tom.. :slight_smile:

I have tried to write some code that I hope will do what you want, it is written in the IDE and uses Arduino instructions.
When you load it, if you open the IDE Monitor window and select 9600baud, you will see the operation of your buttons.

byte sw1Pin = 12;
byte sw2Pin = 11;
byte sw3Pin  = 10;

byte ledAPin = 2;
byte ledBPin = 3;
byte ledCPin = 4;
byte ledDPin = 5;
byte ledEPin = 6;
byte ledFPin = 7;
byte ledGPin = 8;
byte ledHPin = 9;

//Assign some variables to use for button condition
bool sw1ButtonState = 0;
bool sw2ButtonState = 0;
bool sw3ButtonState = 0;

void setup() {
  // Configure input pins, with pullups.
  pinMode(sw1Pin, INPUT_PULLUP);
  pinMode(sw2Pin, INPUT_PULLUP);
  pinMode(sw3Pin, INPUT_PULLUP);
  // Configure output pins.
  pinMode(ledAPin, OUTPUT);
  pinMode(ledBPin, OUTPUT);
  pinMode(ledCPin, OUTPUT);
  pinMode(ledDPin, OUTPUT);
  pinMode(ledEPin, OUTPUT);
  pinMode(ledFPin, OUTPUT);
  pinMode(ledGPin, OUTPUT);
  pinMode(ledHPin, OUTPUT);


void loop() {
  // Read the buttons and invert state due to switch ON to GND.
  sw1ButtonState = !digitalRead(sw1Pin);
  sw2ButtonState = !digitalRead(sw2Pin);
  sw3ButtonState = !digitalRead(sw3Pin);
  // Serial Monitor Button states
  Serial.print("SW1 \t");
  Serial.print("\t SW2 \t");
  Serial.print("\t SW3 \t");
  // IF statements for each button
  if (sw1ButtonState == true)
    digitalWrite(ledAPin, HIGH);
    digitalWrite(ledAPin, LOW);

  if (sw2ButtonState == true)
    digitalWrite(ledBPin, HIGH);
    digitalWrite(ledBPin, LOW);

  if (sw3ButtonState == true)
    digitalWrite(ledCPin, HIGH);
    digitalWrite(ledCPin, LOW);


See if that does what you need at this stage.
Tom... :slight_smile:

Hi Tom. That's awesome of you to write that code for me. I didn't expect you too, but you're a good man for doing so :). I will look through it and try it as soon as I finish this reply.

This is basically a lab for a course I'm taking using the Romeo Development Board. The course is an embedded c course so all instruction so far has been in C as provided by the prof. No Arduino instruction has been discussed at all thus far. The IDE we I use is geany and X loader is used to upload the hex file to the controller.

Here's a link as to what the prof. expects the switches to do. Hopefully this clears things up a bit.

Thanks tim77777,

I'll check into the Serial.print statement and see. Don't know exactly how to use it yet. I'm very green at all this.


Your instructor wants you to make a BCD converter, each dip switch represents a BCD value, the number of LEDs that light up equal the total BCD value.

Tom.... :slight_smile: