Problems selecting and toggling LED status with potentiometer

I’ve been working on my grad project, for which I’ve decided to use an arduino. My goal is to make tic-tac-toe with LEDs. The way I have it set up is so each space in the game consists of 2 LEDs, one green and the other red. I’m using a potentiometer to select a pair and two buttons to select the color in each pair. I have what would be two rows currently wired, just to see if it all works. The circuit did function as planned before, but now none of the LEDs turn on at all when turning the potentiometer. I was hoping someone could see the problem that I can’t seem to find. Here is a sample of the code for the first three pairs of LEDs:

const int analogPin = A0;      //poteniometer, to choose pairs of leds
const int ledG1 = 13;          //green led in first pair
const int ledR1 = 12;          //red led in first pair
const int ledG2 = 11;          //green led in second pair
const int ledR2 = 10;          //red led in second pair
const int ledG3 = 9;           //green led in third pair
const int ledR3 = 8;           //red led in third pair
const int threshold1 = 112;    //max analog value for first pair
const int threshold2 = 225;    //max analog value for second pair
const int threshold3 = 338;   //max analog value for third pair
const int buttonG = 51;         //button to choose green led in pairs
const int buttonR = 53;         //button to choose red led in pairs
int buttonGstate = 0;
int buttonRstate = 0;
int ledtoggle[6];   //if true (1), led should always be on. if false (0), led should be off

void setup() {                 
  pinMode(ledG1, OUTPUT);      //set leds as outputs
  pinMode(ledR1, OUTPUT);
  pinMode(ledG2, OUTPUT);
  pinMode(ledR2, OUTPUT);
  pinMode(ledG3, OUTPUT);
  pinMode(ledR3, OUTPUT);
  pinMode(ledG4, OUTPUT);
  pinMode(ledR4, OUTPUT);
  pinMode(ledG5, OUTPUT);
  pinMode(ledR5, OUTPUT);
  pinMode(ledG6, OUTPUT);
  pinMode(buttonG, INPUT);    //set buttons as inputs
  pinMode(buttonR, INPUT);
  Serial.begin(9600);         //serial monitor
}

For the sake of length, I’ll just put the part of loop that includes up to the first pair.

void loop() {
  int i;
  int analogValue = analogRead(analogPin);
  buttonGstate = digitalRead(buttonG);
  buttonRstate = digitalRead(buttonR);
  for (i = 0; i < 6; i++) {
    if (ledtoggle[i]) {
      digitalWrite(ledtoggle[i], HIGH);
    }
  }
if (ledtoggle[0] == 0 && ledtoggle[1] == 0) {
  if ((analogValue <= threshold1) && (analogValue > 0)) 
  {
    if (buttonGstate == HIGH && buttonRstate == LOW) {
      digitalWrite(ledR1, LOW);
      ledtoggle[0] = 1; }
    else if(buttonGstate == LOW && buttonRstate == HIGH) {
      digitalWrite(ledG1, LOW);
      ledtoggle[1] = 1; }
    else if(buttonRstate == LOW && buttonGstate == LOW) {
      digitalWrite(ledG1, HIGH);
      digitalWrite(ledR1, HIGH); }                                                                                                                                                                                                                                                                                           
    else {
      digitalWrite(ledG1, LOW);
      digitalWrite(ledR1, LOW); }
    }
 }
Serial.println(analogValue);
}

Thanks again.

I don't know what your game is intended to do, but I suggest you write a sketch that does nothing but read the potentiometer and confirm you're getting sensible values from that, and another one that does nothing but turn on each LED in turn and make sure they all work. I don't know how many LEDs you plan to leave turned on at once but make sure you don't exceed the current limits for the chip as a whole.

I'm using a potentiometer to select a pair and two buttons to select the color in each pair.

If the top of the linear pot is +5 and the bottom is Gnd, then the wiper will vary between +5 and 0. The ADC (a 10-bit #) will vary from 1023 to 0. There are 9 squares in "naught and crosses", so simply use map() to transform 1023-0 to 1-9. Then use a switch/case statement to manage the selection.

Personally... If you use RGB LEDs, then you could use Red for player #1, green for #2, and turning the pot would illuminate BLUE as a selection LED which would only stop on a non-selected (available) cell; that is, on play 1 of the game, any 9 blue LEDs could be illuminated, but once Red or Green are lit, Blue in that cell will not illuminate, so button selection would not work... Forcing a bypass until an available cell, blue illuminated, is selected.... Player would push their button, Red or Blue.

More color ---> more fun! ---> higher grade?

Ray

The map and switch case work, so selection works now. Thanks for the tip. On another note, how/where could I incorporate an if statement in a case?

Use if() just like normally

    case 12:    // SIN
            b = Serial.parseFloat();
            if (Degrees){b = b * Pi / 180.0; }
            LastX = sin(b);
            Serial << _FLOAT(LastX, decimals);
            break;

Ok, I think the only problem I have now is keeping the results of an if statement to remain constant. In this case, I'm trying to keep one LED in the pair on and the other off, but at the moment it only works while the button is pressed. Is there a way to make the case "stop" after producing a certain output?

I'm trying to keep one LED in the pair on and the other off, but at the moment it only works while the button is pressed

A Boolean array which is 9 deep and 2-dimensional (Red/Green) would seem to be the approach that I would use.

Array representation of tic-tac-toe:
Board representation by element:
0          1           2
3          4           5
6          7           8

LED representation
[0, 0] == Red
[0,  1] == Greeen

But you could do this in many, many different ways.

I've been working on my grad project

Surely you know by now how to use arrays?

Ray