How to make arduino recognize a pair of numbers and turn on an led.

Hello all, the title may be a bit confusing but all I really have is 2 numbers. A rowState which ranges from 1-5 and a columnState which ranges from 1-5. So what my program does is that it first detects if a "row button" is hit. If one of the 5 row buttons are hit then the state of a variable "rowState" changes to either 1-5 depedning on which button you hit. Then it does the same with the columns. Once i hit a row button and a column button i tell my program to print both of the states to see if it works and it does. Now the question is how would I light up a certain led from a 5 by 5 grid (max7219) using those coordinates so to speak. So if rowState = 1 and columnState = 1 and I ultimately want to have the led (1,1) turn on. Any ideas, Im still kind of a beginner in arduino and I'm surprised I got this far with the project so any help is thankfull.

My code is below if anyone was wondering.

#define referenceVoltage A0
#define buttonRow A1
#define buttonColumn A2

#define ledB 2
#define ledC1 3


  int voltage = analogRead(referenceVoltage);
  int whichRow = analogRead(buttonRow);
  
  int rowState = 0;
  int columnState = 0;

void setup()
{
    Serial.begin(9600);

}

void loop() {
  
  
  while(rowState == 0){
rowcheck();

}

while(columnState == 0) {

    columnCheck();
}

 Serial.print("row =");
 Serial.println(rowState);
 Serial.print("column =");
 Serial.println(columnState);
 delay(1000);

}

void rowcheck() {

    int voltage = analogRead(referenceVoltage);
  int whichRow = analogRead(buttonRow);

    if (whichRow > .9*voltage) {
    Serial.println("B");
    rowState = 1;
    delay(250);
  
  } 
  else {
    if (whichRow > .7*voltage) {
Serial.println("I"); 
rowState = 2;
delay(250);
    } 
  else {
    if (whichRow > .5*voltage) {
    Serial.println("N");
    rowState = 3;
    delay(250);
  } 
  else {
    if (whichRow > .3*voltage) {
    Serial.println("G");
    rowState = 4;
    delay(250);
  } 
  else {
    if (whichRow > .1*voltage) {
    Serial.println("O");
    rowState = 5;
    delay(250);
  } 
  else {
    Serial.println("nothin hit");
    delay(250);}
  }
  }
  }
  }
}


void columnCheck() {

    int voltage = analogRead(referenceVoltage);
  int whichColumn = analogRead(buttonColumn);

    if (whichColumn > .9*voltage) {
    Serial.println("1");
    columnState = 1;
    delay(250);
  
  } 
  else {
    if (whichColumn > .7*voltage) {
Serial.println("2");  
columnState = 2;
delay(250);
    } 
  else {
    if (whichColumn > .5*voltage) {
    Serial.println("3");
    columnState = 3;
    delay(250);
  } 
  else {
    if (whichColumn > .3*voltage) {
    Serial.println("4");
    columnState = 4;
    delay(250);
  } 
  else {
    if (whichColumn > .1*voltage) {
    Serial.println("5");
    columnState = 5;
    delay(250);
  } 
  else {
    Serial.println("nothin hit");
    delay(250);}
  }
  }
  }
  }

}

Ok, that’s easy enough and there are a few ways to do it, but the most common way it to use a container “Packet”. You want to send the data as < value1 , value2 >. If the first char is ‘<’, then collect the rest until the serial monitor receives ‘>’. A comma is a good way to split the data into multiple parts, so you will need that too.

So it would be something like this.

if(Serial.available() > 0){
char temp = Serial.read(); // example incomming data <1,23,456,7890>
if( temp == ‘<’ ) // find this char in the buffer, and start collecting the rest after it.
{
while( true ) // just a loop to continuously gather the rest of the chars
// another Serial.available and Serial.read() here.

//look at the incoming char and see if it is a comma or the end of the container ‘>’
// if it in not a comma or >, then store the char(s) into a temporary array
// if the char IS a comma then increment the arrays index by 1 to store the next char(s)

// keep doing this until the incoming char is > then break out of the while loop and you should have all your data

I have a working example if you want to take a look at it.

So I sort of understand it haha. So the data I'm recieving is rowState and columnState. How would i apply your program to mine. I'm assuming its gonna go after the column check.

If you could send me the sample code that would be nice. I'm confused on how all this would work and whats happening where.

It doesn’t get much more simpler than this.

int buffer[8];
int counter = 0;

void setup()
{
  Serial.begin(115200);
}

void loop() 
{
  if(Serial.available() > 0)
  {
    char temp = Serial.read(); // example incomming data <1,23,456,7890>
    if( temp == '<' ) // is incoming char equal to <, if so gather the rest
    { 
      counter = 0; // make sure counter is set to zero
      while(1)
      {
        if(Serial.available() > 0)
        {
          char temp = Serial.read(); // read in incoming char
          
          if(temp != ',' && temp != '>') 
          {
            buffer[counter] = (buffer[counter]*10) + (temp - '0');// converts temp into an int then stores it
          }

          if(temp == ',') 
          {
            counter++; // increment buffer index
          }

          if(temp == '>') // if incoming char is >, then break out of loop
          {  
            break; // breaks out of the while loop
          }
        }
      }
    }
    for(int NUM = 0; NUM <= counter; NUM++) //will output "1,23,456,7890"
     {
       Serial.print(buffer[NUM]);
       if(NUM != counter) Serial.print(",");
     }
     Serial.println();
  }

  while(counter != -1) // clear the buffer
  {
    buffer[counter] = 0; 
    --counter;
  }
}

Simple serial LED on/off code.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

//A very simple example of sending a string of characters 
//from the serial monitor, capturing the individual 
//characters into a String, then evaluating the contents 
//of the String to possibly perform an action (on/off board LED).

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }

  if (readString.length() >0) {
    Serial.println(readString);

    if(readString.indexOf("on") >=0)
    {
      digitalWrite(ledPin, HIGH);
    }

    if(readString.indexOf("off") >=0)
    {
      digitalWrite(ledPin, LOW);
    }

    readString="";
  } 
}