Basic problem

I'm trying to do some basic programming but Arduino doesn't get what I'm trying to communicate to it. I'm going slightly mad >:(

This is a light version of what I'm trying to accomplish but even this basic code won't result in anything.

int IndicatorsSwitchLeft=6;
int IndicatorsSwitchRight=7;

int IndicatorsLeftOutput=9;
int IndicatorsRightOutput=10;

void setup() {
  pinMode(IndicatorsSwitchLeft, INPUT);
  pinMode(IndicatorsSwitchRight, INPUT);

  pinMode(IndicatorsLeftOutput, OUTPUT);
  pinMode(IndicatorsRightOutput, OUTPUT);
}

void loop() {

  if(IndicatorsSwitchLeft==LOW){

    digitalWrite(IndicatorsLeftOutput, HIGH);
    delay(500);
    digitalWrite(IndicatorsLeftOutput, LOW);
    delay(500);
    
    digitalWrite(IndicatorsRightOutput, LOW);

  }else if(IndicatorsSwitchRight==LOW){
      
    digitalWrite(IndicatorsRightOutput, HIGH);
    delay(500);
    digitalWrite(IndicatorsRightOutput, LOW);
    delay(500);
   
    digitalWrite(IndicatorsLeftOutput, LOW); 
      
  }else{
  
    digitalWrite(IndicatorsLeftOutput, LOW);
    digitalWrite(IndicatorsRightOutput, LOW);
      
  }
}

I've checked pins 6,7,9 and 10 to see if there's something wrong with them by setting them as outputs and just send digitalWrite(pinName, HIGH), to them and let them light up some led's They all work perfectly. What am I doing wrong here?

Hi AndreasC

You need to read the value of the input pin:

if(digitalRead(IndicatorsSwitchLeft)==LOW){

Your code was comparing the pin number (6) with LOW (0) so they could never equal each other.

How are your switches wired? I assume they connect the pin to GND when closed? If so, make sure you have external pullup resistors, or use the internal pullups:

pinMode(IndicatorsSwitchLeft, INPUT_PULLUP);

Regards

Ray

See http://arduino.cc/en/Tutorial/DigitalPins with regard to external pull ups vs internal pullups.

Thanks

Have one more problem though. I write this:

int ignition0 = 2;
int ignition1 = 3;
int ignition2 = 4;


void setup() {
  
  pinMode(ignition0, INPUT_PULLUP);
  pinMode(ignition1, INPUT_PULLUP);
  pinMode(ignition2, INPUT_PULLUP);
}


void loop() {

if (digitalRead(ignition0) == HIGH && digitalRead(ignition1) == HIGH && digitalRead(ignition2) == LOW) {
}

}

Problem is that the code in the if-statement runs even when the conditions aren't met which I find really strange and misbehaving. Is it just me or is Arduino extremely hard to program for? It doesn't seem to understand correct code.

I've tried to complement with an else{} without success:

  }else if (digitalRead(ignition0) == HIGH && digitalRead(ignition1) == HIGH && digitalRead(ignition2) == HIGH) {
    digitalWrite(BrakeLightsOutput, LOW);
    digitalWrite(BrakeLightsOutput2, LOW);
  }

AndreasC: Have one more problem though. I write this:

I don't see any code following your IF statement.

It will be very difficult to debug your code if you have the digitalRead()s within the IF test. Much better to read all the values first because then you can use Serial.println() to check them if you are having a problem. Something like this

ign0State = digitalRead(ignition0);
ign1State = digitalRead(ignition1);
ign2State = digitalRead(ignition2);
if (ign0State == HIGH && ign1State == HIGH && ign2State == LOW) {
  // stuff that should happen
}

Another potential problem with your ELSE IF line is that it is reading the values again and it may get a different result. However if you have saved the values you can use them in the ELSE part to get consistent logic.

...R

Start simple and check that your switches are giving you the input signals you expect.

Try a program like this. You should see 1 for switch open, 0 for switch closed.

int ignition0 = 2;
int ignition1 = 3;
int ignition2 = 4;


void setup() 
{  
  Serial.begin(115200);
  pinMode(ignition0, INPUT_PULLUP);
  pinMode(ignition1, INPUT_PULLUP);
  pinMode(ignition2, INPUT_PULLUP);
}


void loop()
{
  Serial.print("ignition0: ");
  Serial.print(digitalRead(ignition0));
  Serial.print("   ignition1: ");
  Serial.print(digitalRead(ignition1));
  Serial.print("   ignition2: ");
  Serial.println(digitalRead(ignition2));
  
  delay(1000);  // wait 1 second
}

AndreasC: Problem is that the code in the if-statement runs even when the conditions aren't met which I find really strange and misbehaving.

I'll guarantee 100% that this is not true. The if works correctly. The conditions must be met, despite appearances. It's very common to think the problem is the computer, but it rarely is, and 99% of the time we realise our mistake.

Is it just me or is Arduino extremely hard to program for? It doesn't seem to understand correct code.

Arduino is no harder to program for than any other microcontroller. It does understand correct code, so you need to look at your code again. As mentioned above, the best way to debug these things is to print out text to the console. This will help you find out where you are going wrong.

The problem wasn't me, and by that I mean it was not my fault, but I should have looked over the pins. I found that the pin nr 3 doesn't respond to input by measuring the voltage on it. It read 0 V on it on both input and no input. I changed it to pin 11 instead and now it works. Thanks :)

AndreasC: I found that the pin nr 3 doesn't respond to input by measuring the voltage on it.

Sounds like it has been damaged.

...R