Serial monitor stops after 1 second

I tried using serial monitor today, re-using a code I did a few days ago, and the serial monitor does one line, then it stops, I tried using another code, and this time, the serial monitor works, but stop after one irl second, anybody could help me?

Welcome to the forum

Please post your full sketches, using code tags when you do. This prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

Which Arduino board are you using and what, if anything, is connected to it ?

I am using an arduino Uno, and it is only connected to my chromebook, which is where I am putting the code in, so far, this is the only code that works:

  Serial.begin(9600);
}


void loop() {
      Serial.println("on");
  delay(100);
}

But my real code is this:

const int buttonPinUp = 2;     // Button to increment the counter
const int buttonPinDown = 3;   // Button to decrement the counter
const int ledPin = 9;          // LED pin

int buttonPushCounter = 0;      // Allows counting the number of button presses
int buttonStateUp = 0;          // Current state of the up button (0 or 1)
int buttonStateDown = 0;        // Current state of the down button (0 or 1)
int lastButtonStateUp = 0;      // Previous state of the up button (0 or 1)
int lastButtonStateDown = 0;    // Previous state of the down button (0 or 1)

void setup() {
  pinMode(buttonPinUp, INPUT_PULLUP);   // Set up the increment button
  pinMode(buttonPinDown, INPUT_PULLUP); // Set up the decrement button
  pinMode(ledPin, OUTPUT);               // Set up the LED
  Serial.begin(9600);
}

void loop() {
  buttonStateUp = digitalRead(buttonPinUp);
  buttonStateDown = digitalRead(buttonPinDown);

  // Check the up button
  if (buttonStateUp != lastButtonStateUp) {
    if (buttonStateUp == LOW) {
      buttonPushCounter++;
      Serial.println("Incremented");
      Serial.print("Counter value: ");
      Serial.println(buttonPushCounter);
    }
    delay(50);
  }
  lastButtonStateUp = buttonStateUp;

  // Check the down button
  if (buttonStateDown != lastButtonStateDown) {
    if (buttonStateDown == LOW) {
      buttonPushCounter--;
      Serial.println("Decremented");
      Serial.print("Counter value: ");
      Serial.println(buttonPushCounter);
    }
    delay(50);
  }
  lastButtonStateDown = buttonStateDown;

  // Control the LED
  if (buttonPushCounter % 4 == 0 && buttonPushCounter != 0) { // Turn ON the LED if the counter is a multiple of 4 and not 0
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}

Please post a hand-drawn wiring diagram showing how you have the buttons wired (pencil and paper is fine).

Please explain what you expect the "real code" to do, and what it does instead.

That won't work. It won't upload to the Arduino.

so far, the real code should make the serial monitor go up when I press the up button (2), and go down when I press the down button (3), heres the current wiring

1 Like

what do you mean? It does work, I am able to send it to my arduino

  • In the real world, those leds need a series dropping resistor, about 220R.
    Looks like the LED is backwards.

  • Your switches need pull down resistors. About 10k.

FYI

I mean what you posted, and told us works, will not work.

When using INPUT_PULLUP, the buttons should be wired from the pins to GND. The wiring diagram is wrong in several respects.

1 Like

Its just that last time I did it, at school, everything worked

Do "it" correctly, and it should work.

ok, give me a few minutes and I will do a good plan, I will send the pictures and I will send the code that I used last friday

here is the picture I took from real life:

const int buttonPinUp = 2;     // Button to increment the counter
const int buttonPinDown = 3;   // Button to decrement the counter
const int ledPin = 9;          // LED pin

int buttonPushCounter = 0;      // Allows counting the number of button presses
int buttonStateUp = 0;          // Current state of the up button (0 or 1)
int buttonStateDown = 0;        // Current state of the down button (0 or 1)
int lastButtonStateUp = 0;      // Previous state of the up button (0 or 1)
int lastButtonStateDown = 0;    // Previous state of the down button (0 or 1)

void setup() {
  pinMode(buttonPinUp, INPUT_PULLUP);   // Set up the increment button
  pinMode(buttonPinDown, INPUT_PULLUP); // Set up the decrement button
  pinMode(ledPin, OUTPUT);               // Set up the LED
  Serial.begin(9600);
}

void loop() {
  buttonStateUp = digitalRead(buttonPinUp);
  buttonStateDown = digitalRead(buttonPinDown);

  // Check the up button
  if (buttonStateUp != lastButtonStateUp) {
    if (buttonStateUp == LOW) {
      buttonPushCounter++;
      Serial.println("Incremented");
      Serial.print("Counter value: ");
      Serial.println(buttonPushCounter);
    }
    delay(50);
  }
  lastButtonStateUp = buttonStateUp;

  // Check the down button
  if (buttonStateDown != lastButtonStateDown) {
    if (buttonStateDown == LOW) {
      buttonPushCounter--;
      Serial.println("Decremented");
      Serial.print("Counter value: ");
      Serial.println(buttonPushCounter);
    }
    delay(50);
  }
  lastButtonStateDown = buttonStateDown;

  // Control the LED
  if (buttonPushCounter % 4 == 0 && buttonPushCounter != 0) { // Turn ON the LED if the counter is a multiple of 4 and not 0
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}

I asked an ai to change this code:

const int buttonPin = 2;  
const int ledPin = 9;   

int buttonPushCounter = 0;  // permet de compter le nombre de fois que le bouton est pesé
int buttonState = 0;        // état actuel du bouton (0 ou 1)
int lastButtonState = 0;    // état précédent du bouton (0 ou 1)

void setup() {
  pinMode(buttonPin, INPUT_PULLUP); // PULLUP inverse le signal, mais permet de simplifier les branchements
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}


void loop() {
  buttonState = digitalRead(buttonPin);

  if (buttonState != lastButtonState) {
    if (buttonState == LOW) {
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("nombre de fois : ");
      Serial.println(buttonPushCounter);
    } else {
      Serial.println("off");
    }
    delay(50);
  }
  lastButtonState = buttonState;

  if (buttonPushCounter % 4 == 0) { // si le bouton est pesé 4 fois, on allume la DEL
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}

and it gave me this:

const int buttonPinUp = 2;     // Button to increment the counter
const int buttonPinDown = 3;   // Button to decrement the counter
const int ledPin = 9;          // LED pin

int buttonPushCounter = 0;      // Allows counting the number of button presses
int buttonStateUp = 0;          // Current state of the up button (0 or 1)
int buttonStateDown = 0;        // Current state of the down button (0 or 1)
int lastButtonStateUp = 0;      // Previous state of the up button (0 or 1)
int lastButtonStateDown = 0;    // Previous state of the down button (0 or 1)

void setup() {
  pinMode(buttonPinUp, INPUT_PULLUP);   // Set up the increment button
  pinMode(buttonPinDown, INPUT_PULLUP); // Set up the decrement button
  pinMode(ledPin, OUTPUT);               // Set up the LED
  Serial.begin(9600);
}

void loop() {
  buttonStateUp = digitalRead(buttonPinUp);
  buttonStateDown = digitalRead(buttonPinDown);

  // Check the up button
  if (buttonStateUp != lastButtonStateUp) {
    if (buttonStateUp == LOW) {
      buttonPushCounter++;
      Serial.println("Incremented");
      Serial.print("Counter value: ");
      Serial.println(buttonPushCounter);
    }
    delay(50);
  }
  lastButtonStateUp = buttonStateUp;

  // Check the down button
  if (buttonStateDown != lastButtonStateDown) {
    if (buttonStateDown == LOW) {
      buttonPushCounter--;
      Serial.println("Decremented");
      Serial.print("Counter value: ");
      Serial.println(buttonPushCounter);
    }
    delay(50);
  }
  lastButtonStateDown = buttonStateDown;

  // Control the LED
  if (buttonPushCounter % 4 == 0 && buttonPushCounter != 0) { // Turn ON the LED if the counter is a multiple of 4 and not 0
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}

yeah :sweat_smile: I just realized I put my wires wrong, let me test after re-doing correctly

  • Ask AI to fix your problem then.

ai has limits, otherwise, I wouldn't be talking here right now, don't you think?

  • Let’s see you are getting Ai to write your code and you are getting others to fix what problems there may be.
    What is your contribution ? :thinking:

  • We help people to write their code not fix AI stuff.

1 Like