Tic Tac Toe (Last led not working)

What the circuit and code does:
-Allows the user to choose an led
-Allows the user to conform the led they want, and light it up permanently (will never turn off)

Notes:
-When the user is choosing an led, the previous one will turn off to show which led you can choose.

Problem: When choosing an LED (with the 1st button, on the far left), the LED turns on, with the previous one turning off, which is what is supposed to happen. However, when SELECTING an Led and then continuing to choose them, the last led on pin 10 does not function properly.

  • Pin 10 Led (bottom right), when pressed to confirm, it will go on, but then go off right after even when it is selected to be permanent

Below is the image, and code:

int led = 2; //led / digital pin that is active
bool permanentState[12]; //array to store permanent led's / digital pins



void pinset() { //setting each digital pin as an output
  for (int b = 2; b !=11; b++) {
    pinMode(b, OUTPUT);}
  }

void clickled(){ //stores the current led / pin permanently
  if (analogRead(A5) == 500) { 
        permanentState[led] = true;}
}


void choose_select_pin() {
  clickled();
  if (analogRead(A5) == 755) { //value to do the folling commands underneath
    while (true) { 
      int j = led - 1;

      if (!permanentState[led]) {//does not turn off the permanent leds
        digitalWrite(j, LOW); //turns off the previous led (not permanent ones)
      }
      
      
      digitalWrite(led, HIGH); //turns on the LED based on the led variable
      Serial.println(led);
      Serial.print("Pin Active:");
      

      if (led == 11) {
        // Turn off the LED at pin 10 if it's not permanent
        if (!permanentState[10]) {
          digitalWrite(10, LOW);
        }
        led = 1;  // goes back to the 1st digital pin
      }
      delay(400);

      if (led != 11) {
        led++; //increases the led variable if the digital pin is from 1 - 10
      }

      if (analogRead(A5) != 755) { 
        break; //breaks the while loop if the left button is not being pressed
      }
    }}}









void setup() {
  Serial.begin(9600); // Initialize serial communication
  pinset();          
}

void loop() {  
  Serial.print("voltage signal: ");
  Serial.println(analogRead(A5));
  choose_select_pin();  
  delay(2000);
}


Image to show circuit

It sounds like a loose wire, if it is just one LED.

But, since it is the last LED in a sequence, software might be the cause...

Try this to set each pin as output:

void pinset() { //setting each digital pin as an output
  for (int b = 2; b < 11; b++)
    pinMode(b, OUTPUT);
}

pinset() puts pins 2 to 10 in output mode, where do you define pins 12 and 13 as outputs?
Why don't you use the digital mode of pins A0 to A5 to connect the push buttons?
I'm not saying that it's wrong how you do it, but if it's not to save pins, the other thing is simpler.

1 Like

im not doing the 2 leds at pin 12 and 13 yet. Also I am using the A5 anolog with the push button and voltage divider

there is a black wire connected to A5, and it comes from the three push buttons which all have different values due to the voltage divider

But I cant figure out where in the code portion I just sent

Each pin, excluding the last one does not turn off when it is set as permanent, however, the last led/pin has a different code because it has to reset at pin 2 again

Code is fixed, and thank you for the new code update

Do not post pictures of code (post #6). Post code in a code block.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.