How to stop Serial.println ?

Hi there... I'm trying to use the serial monitor to gather data from a photocell connected to my arduino uno, and have the arduino stop sending data when I activate a pushbutton. It isn't working, and I can't figure out why. Help will be greatly appreciated!

So... what I've got so far does give me information in the serial monitor, BUT it doesn't stop when I push the button. Why?

the photocell is on A0, and the button is on D4.

int photoPin = A0;
int inPin = 4;
int val = 0;
void setup() {
Serial.begin(1200);
pinMode (photoPin, INPUT);
pinMode (inPin, INPUT);

}

void loop() {
Serial.println(analogRead(A0));
delay(2);
val == digitalRead(4);
if (val == HIGH) {Serial.end();}
}

val == digitalRead(4);

is a comparison, it should be an assignment

val = digitalRead(4);

(deleted)

Thanks, spycatcher...that worked just fine!

However, I want it to completely stop when I press the button, so that I don't have to hold the button down forever. I do realize that a slide switch would work, but I don't have any at the moment... Any other ideas?

Did you try the fix in reply #1?

Yes, I did. However, it simply caused the serialprint to stop on its own, without waiting for the pushbutton's signal, a few milliseconds after starting.

How is the pushbutton connected to pin4? Are you using any external pullups or pull down resistors?

No, I'm not... am I supposed to? right now it's connected directly to pin 4 via a short wire and breadboard.

Here's a photo!

???

See anything I did wrong?

Where do you start? Examples, tutorials and guides.

Looks like you are wired like S3 in the schematic, you will need to turn on the internal pull-up.

You have:
pinMode (inPin, INPUT);
Try:
pinMode (inPin, INPUT_PULLUP);

EDIT
On a second look, looks like you are wired as S1, you need a 10k resistor from the input pin to GND.

OK... it's late. I'll give it a shot tomorrow and let y'all know how it goes. Thanks!

Wire diagonally across the button switch to ensure that you pick up switched legs. Some adjacent pairs are always connected.

The OP should straddle the center trough.

OK! Thank you all for the help! I've finally got it doing what I wanted, and I've learned a lot in the process!

I did change my circuit a bit, using a 10k resistor pulldown.

I also discovered that my pushbutton wasn't seated properly in the breadboard, causing it to frequently fail to signal the arduino.

someone else also recommended that I use state-change-detection to determine the state of the button... that helped a lot!

Here is my current code, if anyone is interested:

const int photoPin = A0;
const int buttonPin = 2;
int buttonState = 0;
int buttonPushCounter = 0;
int lastButtonState = 0;

void setup() {
Serial.begin(1200);
pinMode (photoPin, INPUT);
pinMode (buttonPin, INPUT);

}

void loop() {

buttonState = digitalRead(buttonPin);

if (buttonState != lastButtonState) {
if (buttonState == HIGH){
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
Serial.println("off");
}
delay(10);
}
lastButtonState = buttonState;

if (buttonPushCounter % 2 ==0) {
Serial.println(analogRead(A0));
}
}