IDE not meeting expected outcome

Hi all, after reading the Forum guidelines, and spending much time trying to solve the issue myself, I have now given up and seek the wise heads of this forum. I make no apologies - I have worked and re-worked the sketch, but still cannot get the desired result and have obviously missed the salient art of C++.
Brief outline: nano board (clone) operating in WIN10 Pro 64, Com 3-no problems being recognised. IDE compiles OK, uploads OK, but outcome only partial i.e., rapid activation with one led 'flashing' once, the other staying on? Circuit is one double pole switch to pin 2 (input) grounded via a 10K pull-down resistor, other contact to 5vdc.
Two LED's - one on pin 3, the other on pin 6 as outputs, to ground via 220 ohm resistors.
(Other IDE's e.g., examples work OK, and I have two identical nano's both of which work the same way.)

So, here is my code, which is quite simple really. and especially kept that way for me to gain a better understanding of Arduino/C++ etc: So, over to you, and I shall look forward with anticipation to an obvious answer that I have missed (not seeing the wood for the trees, etc.) Kind regards,

const int Switch = 2;   // 10k pull down resistor to Gnd, switch to 5vdc
const int RedLed = 3;       // pin that Red led attached to via 220ohm resistor
const int GreenLed = 6;    // pin that Green led attached to via 220ohm resistor
int SwitchState = 0;


void setup()
{

  pinMode(2, INPUT);

  pinMode(3, OUTPUT);//Red led's
  pinMode(6, OUTPUT);//Green led's

}

//read the switch position value into a variable


void loop()
{

  int (SwitchState) = digitalRead(SwitchState);

  //switch Down

  { if (SwitchState == LOW)

      delay(50);

    digitalWrite(6, LOW);//Green off
    digitalWrite(3, HIGH);//Red on(Gear in transit)
    delay(8000);
    digitalWrite(6, HIGH); //Green on
    digitalWrite(3, LOW);//Red off
  }
  { if (SwitchState == HIGH)

      delay(50);

    digitalWrite(6, LOW);  //Green Led off}
    digitalWrite(3, HIGH);   //Red Led on
    delay(8000);
    digitalWrite(3, LOW);//Red Led off

  }
}

Thank you for posting you code according to the guidelines.

You are basically saying, "it doesn't work", without describing what happens. ("rapid activation with one led 'flashing' once, the other staying on?" Which LED?

Post a schematic - not a useless Fritzing picture. Also, describe, in detail, what it is doing.

You create names for your Led pins but then you don't use them.

const int RedLed = 3;       // pin that Red led attached to via 220ohm resistor
const int GreenLed = 6;    // pin that Green led attached to via 220ohm resistor

digitalWrite(6, LOW);//Green off
digitalWrite(3, HIGH);//Red on(Gear in transit)

But that isn't really the problem.

 int (SwitchState) = digitalRead(SwitchState);

Oops
I'll give you a tip that's also a hint.
Use the word Pin in your pin names. i.e. redLedPin, switchPin.
it helps to avoid these types of issues.

Thank you for your tips guys, I shall try them out. I will also put up a sketch. (Thought I did, but forgot during the :frowning: post - sorry!)

{ if (SwitchState == LOW)

The opening brace should be AFTER the closing paren.

Your title "IDE not meeting expected outcome" sounds strange to me.
This sounds like the IDE would have an "opinion" The outcome will allways be what you have programmed.

The IDE will try to compile the sourcecode-text you have typed it.
If there are formal syntactical mistakes you will get a compiler-error.

If there are no formal syntactical mistakes it will compile.

If the compiled code will do what you have programmed.

There is a very old programmers wisdom:

Your program always does what you have programmed. If your program does something different than you expected, then you don't understand (yet) what you have programmed.

Another way to say this is: "The bug has his hands on the keyboard and is staring at the screen."

If you use self-explaining names most comments get obsolet because the name does explain it already

The main purpose of a programming-language is using names instead of numbers.

You coded

const int RedLed = 3;       // pin that Red led attached to via 220ohm resistor[color=#222222][/color]
const int GreenLed = 6;    // pin that Green led attached to via 220ohm resistor[color=#222222][/color]
[color=#222222][/color]
digitalWrite(6, LOW);//Green off[color=#222222][/color]
digitalWrite(3, HIGH);//Red on(Gear in transit)

if you code

const int RedLed_Pin   = 3;   // pin that Red led attached to via 220ohm resistor
const int GreenLed_Pin = 6;   // pin that Green led attached to via 220ohm resistor

the names "RedLed_Pin" itself explains it
then your code should use this name all the time

  pinMode(RedLed_Pin, OUTPUT);//Red led's
  pinMode(GreenLed_Pin, OUTPUT);//Green led's

and

    digitalWrite(GreenLed_Pin, LOW);  // comment no longer needed Green off
    digitalWrite(RedLed_Pin, HIGH);   // comment no longer needed Red on(Gear in transit)
    delay(8000);
    digitalWrite(GreenLed_Pin, HIGH); // comment no longer needed Green on
    digitalWrite(RedLed_Pin, LOW);    // comment no longer needed  Red off

If you ever would like to change to another IO-pin of the Arduino you change the number
in one single place and you are done
because all other places related to the IO-pin use the name. No possability to forget one.

Here is a modifyed version of your code.

const int Switch_Pin   = 2;   // 10k pull down resistor to Gnd, switch to 5vdc
const int RedLed_Pin   = 3;   // pin that Red led attached to via 220ohm resistor
const int GreenLed_Pin = 6;   // pin that Green led attached to via 220ohm resistor


int SwitchState = 0;


void setup() {
  pinMode(Switch_Pin, INPUT);
  pinMode(RedLed_Pin, OUTPUT);//Red led's
  pinMode(GreenLed_Pin, OUTPUT);//Green led's
}




//read the switch position value into a variable
void loop() {


  int (SwitchState) = digitalRead(Switch_Pin);


  //switch Down
  if (SwitchState == LOW) {


    delay(50);


    digitalWrite(GreenLed_Pin, LOW);  // comment no longer needed Green off
    digitalWrite(RedLed_Pin, HIGH);   // comment no longer needed Red on(Gear in transit)
    delay(8000);
    digitalWrite(GreenLed_Pin, HIGH); // comment no longer needed Green on
    digitalWrite(RedLed_Pin, LOW);    // comment no longer needed  Red off
  }
  
   if (SwitchState == HIGH) {


    delay(50);


    digitalWrite(GreenLed_Pin, LOW);  // comment no longer needed Green Led off}
    digitalWrite(RedLed_Pin, HIGH);   // comment no longer needed Red Led on
    delay(8000);
    digitalWrite(GreenLed_Pin, LOW);  // comment no longer needed Red Led off
  }
}

best regards Stefan

Thank you Stefan, and others. As I indicated, sometimes the answer is before you and invisible! I guess sometimes we do miss the seeing the 'Wood for the trees'! Thank you again. And kind regards to all who honestly assist. :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.