Serial + led interference

Hi

i have a big problem, i am using a mega which has a input from a external device it has a common GND. the problem i am having is that the code works when i dont request anything to be printed to the serial, however when i do the led starts to flash. so i decided to use a photocell however i am still getting the issue

int ledPin = 50; // LED connected to digital pin 13
int inPin = 32;   // pushbutton connected to digital pin 7
int val = 0;     // variable to store the read value
int photocellPin = 0;     // the cell and 10K pulldown are connected to a0
int photocellReading;  

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);      // sets the digital pin 13 as output
  pinMode(inPin, INPUT);      // sets the digital pin 7 as input
}

void loop()
{
   photocellReading = analogRead(photocellPin);  

  val = digitalRead(inPin);   // read the input pin
  if (val == HIGH){
    digitalWrite(ledPin, HIGH);
  }else {
digitalWrite(ledPin, LOW);

  }


if (photocellReading > 800) {
    
  } else {


  }
}

my code is simple but i cant seem to solve the issue the led has no resistor i attached one and it still was flashing

if anyone can help me that would be great

thank you

Are you saying that the LED flashes when you run the code you posted ? What photocell is it and how is it wired ? How is the inPin wired ? Any pullup or pulldown resistor in the circuit ? If not, then why not, or use pinMode(inPin, INPUT_PULLUP); to turn on the internal pullup resistor.

int ledPin = 50; // LED connected to digital pin 13
int inPin = 32;   // pushbutton connected to digital pin 7

Which pins are the input and output actually connected to ?

int inPin = 32;   // pushbutton connected to digital pin 7

Is the pushbutton the “external device”?

What IS the external device? How is it connected to the Arduino?

UKHeliBob:
Are you saying that the LED flashes when you run the code you posted ? What photocell is it and how is it wired ? How is the inPin wired ? Any pullup or pulldown resistor in the circuit ? If not, then why not, or use pinMode(inPin, INPUT_PULLUP); to turn on the internal pullup resistor.

int ledPin = 50; // LED connected to digital pin 13

int inPin = 32;  // pushbutton connected to digital pin 7


Which pins are the input and output actually connected to ?

Hi inPin, INPUT_PULLUP worked like a charm thank you so much