Why is this line being skipped?

I am prototyping in tinkercad trying to get the consol to say "Sensor Left High" when an optical sensor detects something, and turn on a light. Im using a switch to simulate this sensor and the light turns on but it skips over the Serial.println command?

The right sensor(switch) code works 100% as intended but the left doesn't? I originally had it plugged into pin 2 on the Arduino but then the light and the consol wouldn't work. So I swapped it to pin 0 and now the light works? It's just skipping over line 71?

// [C++ code]
// [Made By Nicky K.]

  // [Setting Up Names for pins for easier Coding]
  int const OpticalSR = 1; //Optical Sensor Right
  int const OpticalSL = 0; //Optical Sensor Left
  int const LFS = 3; //Left Foward Switch
  int const LRS = 4; //Left Rear Switch
  int const RFS = 6; //Right Foward Switch
  int const RRS = 5; //Right Rear Switch

  int const OpticalSRL = 7; //Optical Light Right
  int const OpticalSLL = 8; //Optical Light Left
  int const LMFL = 13; //Left Motor Foward Light
  int const LMRL = 12; //Left Motor Reverse Light
  int const StopL = 11; //Stop Light
  int const RMFL = 10; //Right Motor Foward Light
  int const RMRL = 9; //Right Motor Reverse Light

void setup(){
  
  // [Turns on Serial Monitor & sets pins as Inputs and Outputs]
  Serial.begin(9600);
  pinMode(OpticalSR, INPUT);
  pinMode(OpticalSL, INPUT);
  pinMode(LFS, OUTPUT);
  pinMode(LRS, OUTPUT);
  pinMode(RFS, OUTPUT);
  pinMode(RRS, OUTPUT);
  
  pinMode(OpticalSRL, OUTPUT);
  pinMode(OpticalSLL, OUTPUT);
  pinMode(LMFL, OUTPUT);
  pinMode(LMRL, OUTPUT);
  pinMode(StopL, OUTPUT);
  pinMode(RMFL, OUTPUT);
  pinMode(RMRL, OUTPUT);
  
  // [Turning On then Off the Lights to make sure they are Working]
  digitalWrite(OpticalSRL, HIGH);
  digitalWrite(OpticalSLL, HIGH);
  digitalWrite(LMFL, HIGH);
  digitalWrite(LMRL, HIGH);
  digitalWrite(StopL, HIGH);
  digitalWrite(RMFL, HIGH);
  digitalWrite(RMRL, HIGH);
  delay(50);
  digitalWrite(OpticalSRL, LOW);
  digitalWrite(OpticalSLL, LOW);
  digitalWrite(LMFL, LOW);
  digitalWrite(LMRL, LOW);
  digitalWrite(StopL, LOW);
  digitalWrite(RMFL, LOW);
  digitalWrite(RMRL, LOW);
  
}

void loop(){
  
  // [Turns on Sensor Lights if Sensors Detect Something]
  if (digitalRead(OpticalSR) == HIGH) {
  Serial.println("Sensor Right High");
  digitalWrite(OpticalSRL, HIGH);
  } 
  else {
  digitalWrite(OpticalSRL, LOW);
  }
  
  // [??????Why this not working?]
  if (digitalRead(OpticalSL) == HIGH) {
  Serial.println("Sensor Left High");
  digitalWrite(OpticalSLL, HIGH);
  } 
  else {
  digitalWrite(OpticalSLL, LOW);
  }
  
}

Oops

The optical sensors are wired to pins 0 and 1, so you should probably elaborate on where the "oops" is.

Sorry uploaded the wrong image before, now it should be fixed

Yes, that's where the "oops" is.

The state of the optical sensors im using are either high or low and that's why I'm using a switch to simulate the high or low state manually in tinkercad

Sure, but for the sake of our new user, you should probably explain that pins 0 and 1 shouldn't be used as input / output because they interfere with the serial port, instead of just quoting the pin assignments and saying "oops". That's not going to be particularly helpful.

@OP: You shouldn't use pins 0 and 1 as input / output because they interfere with the serial port.

1 Like

I thought the "Tx" and "Rx" next those pins would be sufficient.

Thanks for the explanation, is there a way I could hook them to analog pins and have the analog pins read as high and low?

Yes, just use analogue pins as digital.

Given the amount of new users who run into this problem, they clearly aren't.

"The analog input pins can be used as digital pins, referred to as A0, A1, etc. The exception is the Arduino Nano, Pro Mini, and Mini’s A6 and A7 pins, which can only be used as analog inputs."

(You could also save some digital inputs by hooking your seven LEDs up to a shift register instead of hooking each one directly to an Arduino pin.)

Thank you so much, have a nice day

So I took your guy's advice and am not using pins 1 and 0, and now have the lights on the analog pins using digital write commands that work perfectly, but now moving the sensor(switches) to pins 9 and 10 yields no consol text and no lights turning on? What is my major malfunction? It seems as if the digitalRead command isn't working?

// [C++ code]
// [Made By Nicky K.]

  // [Setting Up Names for pins for easier Coding]
  int const OpticalSR = 9; //Optical Sensor Right
  int const OpticalSL = 10; //Optical Sensor Left
  int const LFS = 3; //Left Foward Switch
  int const LRS = 4; //Left Rear Switch
  int const RFS = 6; //Right Foward Switch
  int const RRS = 5; //Right Rear Switch

  int const OpticalSRL = A4; //Optical Light Right
  int const OpticalSLL = A5; //Optical Light Left
  int const LMFL = A0; //Left Motor Foward Light
  int const LMRL = A1; //Left Motor Reverse Light
  int const StopL = 13; //Stop Light
  int const RMFL = A2; //Right Motor Foward Light
  int const RMRL = A3; //Right Motor Reverse Light

void setup(){
  
  // [Turns on Serial Monitor & sets pins as Inputs and Outputs]
  Serial.begin(9600);
  pinMode(OpticalSR, INPUT);
  pinMode(OpticalSL, INPUT);
  pinMode(LFS, OUTPUT);
  pinMode(LRS, OUTPUT);
  pinMode(RFS, OUTPUT);
  pinMode(RRS, OUTPUT);
  
  pinMode(OpticalSRL, OUTPUT);
  pinMode(OpticalSLL, OUTPUT);
  pinMode(LMFL, OUTPUT);
  pinMode(LMRL, OUTPUT);
  pinMode(StopL, OUTPUT);
  pinMode(RMFL, OUTPUT);
  pinMode(RMRL, OUTPUT);
  
  // [Turning On then Off the Lights to make sure they are Working]
  digitalWrite(OpticalSRL, HIGH);
  digitalWrite(OpticalSLL, HIGH);
  digitalWrite(LMFL, HIGH);
  digitalWrite(LMRL, HIGH);
  digitalWrite(StopL, HIGH);
  digitalWrite(RMFL, HIGH);
  digitalWrite(RMRL, HIGH);
  delay(50);
  digitalWrite(OpticalSRL, LOW);
  digitalWrite(OpticalSLL, LOW);
  digitalWrite(LMFL, LOW);
  digitalWrite(LMRL, LOW);
  digitalWrite(StopL, LOW);
  digitalWrite(RMFL, LOW);
  digitalWrite(RMRL, LOW);
  
}

void loop(){
  
  // [Turns on Sensor Lights if Sensors Detect Something]
  if (digitalRead(OpticalSR) == HIGH) {
  Serial.println("Sensor Right High");
  digitalWrite(OpticalSRL, HIGH);
  } 
  else {
  digitalWrite(OpticalSRL, LOW);
  }
  
  if (digitalRead(OpticalSL) == HIGH) {
  Serial.println("Sensor Left High");
  digitalWrite(OpticalSLL, HIGH);
  } 
  else {
  digitalWrite(OpticalSLL, LOW);
  }
  
}

I'm actually a bit sceptical about the wiring on the optical sensors, could you please post a model number, datasheet or similar?

These should be INPUT_PULLUP to enable the built-in pull-up resistors. Without that the pin could read HIGH or LOW when the button isn't pressed.

1 Like

Pololu Carrier with Sharp GP2Y0D810Z0F Digital Distance Sensor 10cm

A cryptic two-letter code? Sufficient? For a beginner?
Are you serious, or trolling?

And how am I supposed to know they're a beginner?
The order they put "int const"?
Their use of code tags in a first post?

In the most recent sketch, you've got no VCC going to the two sensors. Is this just an incomplete drawing, or did you forget to wire that>
C