Code for switch pin and 2 leds (beginner needing help)

Hi I'm new to this and a company I am joining have asked to see what I could do on Arduino..

Please if you have time could you help me write a code for this as I'm so stuck and have tried many different ideas but failing due to being so new.

This is the task:

Green led output is on digital pin 9
Orange led output is on digital pin 10
Switch input is on digital pin 8

Flash the green led when switch is in position '0', flash the orange led when switch is in position '1'

Hello. How much help can we give you before this becomes dishonest? Don’t you think they will check this forum to see how much help you get? I would.

Did they say how quickly the flashing should be and how quickly the circuit must react to the switch?

Why don’t you post your best attempt so far and we can give you some pointers. Use code tags. It’s the </> icon

Paul

Hi Paul,

Thank you for your reply I see what you mean about being dishonest but I can guarantee its fine… I will be taken on as an apprentice at this company so will get taught it anyways (I’m not trying to steal a code and claim its mine I just want some help) the company even said I should ask people in the forums for help if I’m stuck.

They have not stated how quick the flashing should be and how quick to react they just want to see if I can get 2 different coloured leds flashing when the switch is in 2 different positions.

I will upload my code and any pointers would be deeply appreciated.

This code worked to flash the orange led in position ‘1’ … I added to it thinking the green would flash in position ‘0’ but its not that simple it seems.

Kind regards,

Sam

orange_green_position_0___1.ino (383 Bytes)

okay so I've been messing around and so close to doing it but my lights are flashing in the wrong position, what am I doing wrong? green is flashing in position '1' instead of '0'

int val = 1;

void setup() {
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(8, INPUT);
}

void loop() {
val = digitalRead(8);

if (val == 0){
  digitalWrite(10, HIGH);
} else {
  digitalWrite(10, LOW);
}

if (val == 0){
  digitalWrite(9, HIGH);
} else {
  digitalWrite(9, LOW);
}
}

Quite a few things wrong. We'll take them a few at a time.

#define LED 10
#define LED 9

Here, you define a symbol "LED" and give it the value 10. But then you immediately re-define it and give it the value 9. So when the sketch runs it will only ever have the second value.

Oh wait... You changed it while I was writing...

Have you finished changing it yet?

Hi Paul,

sorry I changed the code cos I thought it looked messy!

This code has worked I found that if I swapped the 'HIGH' & 'LOW' around the lights flash in the correct positions.

would you say this is a good enough code or is there a better more simpler way?

kind regards

int val = 0;

void setup() {
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(8, INPUT);
}

void loop() {
val = digitalRead(8);

if (val == 0){
  digitalWrite(10, LOW);
} else {
  digitalWrite(10, HIGH);
}

if (val == 0){
  digitalWrite(9, LOW);
} else {
  digitalWrite(9, HIGH);
}

}

It's still messy. Click Tools->Auto Format before you post it next time.

The LEDs don't flash as such unless you use the switch do they? I doubt that's what your employers are looking for. I think they will expect the leds to continue to flash even when you are not using the switch.

Still, you are learning fast.

Ah I have now done that thankyou for the tip!

They didn't specify they wanted the lights to continue to flash; just to flash once and stay on in that certain position until the switch changes position again.

Thankyou Paul I appreciate you giving up your time to help a newbie out :slight_smile:

Next challenge .. Pulse width modulation :smiley:

Sedgell95:
They didn't specify they wanted the lights to continue to flash; just to flash once and stay on in that certain position until the switch changes position again.

Are you sure? Because maybe it is a trick question. You don't even need an Arduino to do what you just described/what your code is doing. All you need is the switch, leds and resistor(s).

If it is a trick question and they do want them flashing for a second would I just need to add a delay (1000) in the code for example?

No. Simply using delay() does not cause flashing. You have to switch the leds on and then off! And not when a switch changes.