Arduino sees input but nothing goes to serial

I have a sketch that has 3 laser detectors and on pins 6,7,8 and it is supposed to tell me which one was hit by the laser. I had it working with one, but I can't figure out why it's not working with 3. What am I missing?

int Laser1 = 6;
int Laser2 = 7;
int Laser3 = 8;
char one = "LaserOne";
char two = "LaserTwo";
char three = "LaserThree";

void setup()
{
Serial.begin(9600);
pinMode(Laser1, INPUT);
pinMode(Laser2, INPUT);
pinMode(Laser3, INPUT);
}

void loop()
{

boolean val1 = digitalRead(Laser1);
Serial.print(one);

boolean val2 = digitalRead(Laser2);
Serial.print(two);

boolean val3 = digitalRead(Laser3);
Serial.print(three);

}

All that code does is read the pin, but does abosultely nothing with the value in val1 or val2 or val3. And the serial prints aren't inside any if statements or anything so they just happen in between the reads. If you want it to tell you if the pin was high or low then you need an if statement.

if(val1 == HIGH){
    Serial.print(one);
}

What am I missing?

  1. you missed out reading the how to use this forum sticky post
  2. so you missed out saying what your code actually does - not working does not cut it round here.
  3. you seem to have missed out using any if statements.

You're absolutely right. That's what I get for trying to "simplify" my code before posting it. Even if my if statements were missing, shouldn't I have seen variable one, two and three flow across my serial data?

So answer point 2 please.

You should see a whole bunch of them in a row even. Where are you looking for your output? DO you have the serial monitor open or are you expecting to see it at the bottom of the IDE window?

Your code happily displays LaserOneLaserTwoLaserThree forever in the serial monitor.

Laser 1,2,3 are laser receivers connected to an arduino uno on pins 6,7,8 respectively. The arduino is plugged into a raspberry pi running node red. The idea is that when I point a laser at any of these 3 sensors, I want to know which one was hit. Pointing a laser at any of the 3 lights up the input light on the uno but no sensor data flows to the pi. When I drop a different sketch in there, such as my ultrasonic range sensor, its readings like 24cm flow through the debugging node. So I feel like I've tested:

  1. That the uno is getting data.
  2. That the uno can send data.
  3. That the pi can receive data.
  4. That I can read uno serial data streaming across the pi.

What I don't know is why this particular sketch doesn't seem to be sending any serial data.

I haven't programmed C++ since the 1980s on my WWIV bulletin board service, so I could be missing something. I'm a PHP guy so my concept of what makes something fire on the arduino could be missing a piece of the puzzle. Any help would be greatly appreciated.

By the way, I run a site that attracts a lot of PHP Noobs, so I know what it's like to answer a bunch of super basic questions, so I appreciate your time on mine.

What I don't know is why this particular sketch doesn't seem to be sending any serial data.

The code in the opening post does send data.

If you have a problem with another code, please post that code (and use code tags please).

I don't get it. All of my other code works perfectly piping data to the pi. Even if I do this really basic sketch...
int Laser = 6;
int Detector = 7;
char pi = "Laser";

void setup()
{
Serial.begin(9600);
pinMode(Laser, OUTPUT);
pinMode(Detector, INPUT);
}

void loop()
{
digitalWrite(Laser, HIGH);
boolean val = digitalRead(Detector);
if (val > 0) {
Serial.print(pi);
}
}

I see laserlaserlaser in the serial monitor, but nothing comes across on the pi? Any idea what could be going wrong?

If I hook an analog mic up, I see my volts going in the pi's serial. If I hook a laser rangefinder, the cm happily flow across the screen, but this one...nothing.

I see laserlaserlaser in the serial monitor, but nothing comes across on the pi?

Is that a question or a statement? If it is a question, send me your Arduino and your pi, and I’ll answer the question.

If it is a statement, that’s the wrong punctuation.

Wow. Ok.

Yes. It is a statement.

I see serial data in my serial monitor for this sketch. I see the light come on saying that data is received. However I do not see serial data flowing across the serial receiver on my pi in node-red.

I do see serial data flowing on my pi with other sketches.

Add a statement to the code in reply #9, in setup():

   Serial.println("Good morning, Mr. Pi. Did you have a nice pi day yesterday?");

Do you see that message on the pi?

If so, then I think that the device that you are reading from is not pulling the pin HIGH or LOW all the time.

I appreciate the response...I'm not at home at the moment, but I think I can see what's going on right away. I'm guessing the pi doesn't get the data until it has a Serial.println

I think node red needs that line return to fire off the "msg" that makes this all work. THANK YOU! I was pulling my hair out on that one. I'll report back.

I don't think that the print() vs. println() makes any difference. It wouldn't to the serial monitor.

What I'm suggesting is that you add an unconditional print. The only print statement you have now depends on the switch (or whatever is connected to the pin) being wired correctly.

Adding an unconditional print would tell you whether the failure to see data was due to bad wiring.

PaulS. Thank you for your help. the print vs println actually does make a difference to the pi (or I guess more specifically node-red).

Nodes in node red don't "fire" unless something triggers them, so they were just sitting there in a big buffer. Once I did your ln command I got a giant buffer of all the data I had been sending to the pi debugger. Weird but true.

Again. Thanks so much for your help!

Weird but true.

OK. Glad to have helped, even if unintentionally.

Just to put this one to rest....this code works...
int Laser1 = 6;
int Laser2 = 7;
int Laser3 = 8;
char one = "LaserOne";
char two = "LaserTwo";
char three = "LaserThree";

void setup()
{
Serial.begin(9600);
pinMode(Laser1, INPUT);
pinMode(Laser2, INPUT);
pinMode(Laser3, INPUT);
}

void loop()
{

boolean val1 = digitalRead(Laser1);
if (val1 > 0) {
Serial.println(one);
}
boolean val2 = digitalRead(Laser2);
if (val2 > 0) {
Serial.println(two);
}
boolean val3 = digitalRead(Laser3);
if (val3 > 0) {
Serial.println(three);
}
}

Note that, if you change Serial.println to Serial.print nothing will come through on node-red. And when I'm doing concatenation, if I want that to come through as one message, I can do Serial.print() on the first line and Serialprintln() on the second line and the pi will send that as one block, which is really handy for the things I'm doing.

and the pi will send that as one block,

No
and the pi will send see that as one block,

And SEND that as one block to the next node.