-3

This is the code that I started with for blinking an LED.

int redPin = 12;                 // Red LED connected to digital pin 12
int greenPin = 11;               // Green LED connected to digital pin 11

void setup ()                    // run once, when the sketch starts
{
  pinMode (redPin, OUTPUT);      // sets digital pin as output to provide current to LED
  pinMode (greenPin, OUTPUT);    // sets digital pin as output to provide current to LED
}

void loop ()                     // run over and over again, called repeatedly
{
  digitalWrite (redPin, HIGH);   // sets the Red LED on
  digitalWrite (greenPin, HIGH); // sets the Green LED on
  delay (500);                   // waits for 500 milliseconds, half a second
  digitalWrite (redPin, LOW);    // sets the Red LED off
  digitalWrite (greenPin, LOW);  // sets the Green LED off
  delay (500);                   // waits for 500 milliseconds, half a second
}

This is my modified code for blinking an LED.

int redPin = 12;                 // Red LED connected to digital pin 12
int greenPin = 11;               // Green LED connected to digital pin 11

void setup ()                    // run once, when the sketch starts
{
  pinMode (redPin, OUTPUT);      // sets digital pin as output to provide current to LED
  pinMode (greenPin, OUTPUT);    // sets digital pin as output to provide current to LED
}

void loop ()                     // run over and over again, called repeatedly
{
  digitalWrite (redPin, HIGH);   // sets the Red LED on
  digitalWrite (greenPin, HIGH); // sets the Green LED on
  delay (500);                   // waits for 500 milliseconds, half a second
  digitalWrite (greenPin, LOW);  // sets the Green LED off
  delay (500);                   // waits for 500 milliseconds, half a second
  digitalWrite (redPin, LOW);    // sets the Red LED off
  delay (500);                   // waits for 500 milliseconds, half a second
  digitalWrite (greenPin, HIGH);  // sets the Green LED on
  delay (500);                   // waits for 500 milliseconds, half a second
}

I want to change the loop() procedure code so that both LEDs are on for 500 ms, then only the red LED is on for 500 ms, then both LEDs are off, and finally only the green LED is on for 500 ms.

Is this code correct?

  • 3
    Please, don't post images of code; post the code itself so others can copy it to work in your problem. – user31481 Feb 14 '18 at 13:42
  • If I can solve this question, I would delete this post – kj8 Feb 14 '18 at 13:45
  • 1
    It can be useful for someone else; don't delete it. – user31481 Feb 14 '18 at 13:46
  • 2
    @LookAlterno Now that you've answered the question anyway, I bet the OP won't have the motivation to improve it. Just saying. – Dmitry Grigoryev Feb 14 '18 at 15:03
  • 1
    @DmitryGrigoryev. Well, OP keep asking. That's a good sign. – user31481 Feb 14 '18 at 15:22
0

As I understand, you want one led ON while the other is OFF.

Then, you only need to make sure that the state of one pin is the opposite of the other pin, like here:

int redPin=12;
int greenPin=11;

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
}

void loop() {
  digitalWrite(redPin, HIGH);
  digitalWrite(greenPin, LOW);
  delay(500);
  digitalWrite(redPin, LOW);
  digitalWrite(greenPin,HIGH);
  delay(500);
}
  • Thanks.I have one more question.How can I change the loop() procedure code so that both LEDs are on for 500 ms, then only the red LED is on for 500 ms, then both LEDs are off, and finally only the green LED is on for 500 ms. – kj8 Feb 14 '18 at 13:51
  • @kj8. From here is up to you. Think about, write it down as you wrote it, but now using digitalWrite and delay instead of "both led on" and "on for 500ms". – user31481 Feb 14 '18 at 13:57
  • int redPin = 12; int greenPin = 11; void setup() { pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); } void loop() { digitalWrite(redPin, HIGH); digitalWrite(greenPin, HIGH); delay(500); digitalWrite(greenPin, LOW); delay(500); digitalWrite(redPin, LOW); delay(500); digitalWrite(greenPin,HIGH) delay(500); } – kj8 Feb 14 '18 at 15:01
  • Is this code correct? – kj8 Feb 14 '18 at 15:02
  • 1
    @kj8. Well, test it! You have your Arduino and LED. Run your code, make changes, do experiments! – user31481 Feb 14 '18 at 15:54
0

How can I change the loop() procedure code so that both LEDs are on for 500 ms, then only the red LED is on for 500 ms, then both LEDs are off, and finally only the green LED is on for 500 ms?

That is pretty self-explanatory. Just break your requirement down into steps;

  1. Turn on both LEDs
  2. Wait 500ms
  3. Turn off green LED
  4. Wait 500ms
  5. Turn off red LED
  6. Wait 500ms
  7. Turn on green LED
  8. Wait 500ms

You already know how you perform all those steps, so now it's over to you to turn it into Arduino code.

  • int redPin = 12; int greenPin = 11; void setup() { pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); } void loop() { digitalWrite(redPin, HIGH); digitalWrite(greenPin, HIGH); delay(500); digitalWrite(greenPin, LOW); delay(500); digitalWrite(redPin, LOW); delay(500); digitalWrite(greenPin,HIGH) delay(500); } – kj8 Feb 14 '18 at 15:02
  • Is this code correct? – kj8 Feb 14 '18 at 15:02
  • Does it do what you want it to do? – Majenko Feb 14 '18 at 15:33
  • Yes. I want to do it. – kj8 Feb 14 '18 at 15:35
0

The best answer is here: http://www.gammon.com.au/blink, go read that post as it explains this very well.

Basically, don't use delay but instead keep track of the status and time and when you reach a threshold toggle it.

  • 1
    Answers shouldn't be behind a link. You should add relevant info from that link directly into the answer. – gre_gor Feb 14 '18 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.