// For: https://forum.arduino.cc/t/multiplexing-led-3x3-arduino-learning/1052704
//
// This Wokwi project: https://wokwi.com/projects/348142596017619538
//
// Bad example !
// I had this multiplexing thing all wrong.
// All three anodes should be set at the same time,
// and the brightness will only be 1/3. It is now 1/9.
int a = 5; //delay time
void setup() 
{
  pinMode(2, OUTPUT);    // 2, 3, 4 is cathode
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(8, OUTPUT);    // 8, 9, 10 is anode
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  // Turn off the rows (cathodes)
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
}
void loop() 
{
  // Scan all the leds, one by one.
  for( int row = 2; row <= 4; row++)
  {
    // Turn the active row (three cathodes) low
    digitalWrite(row, LOW);
    for( int column = 8; column <= 10; column++)
    {
      // Turn the middle led on.
      if( row == 3 and column == 9)
      {
        digitalWrite(column, HIGH);
      }
      else
      {
        // Randomly turn an other led on and off
        // Just for fun.
        if( random(100) < 2)
        {
          digitalWrite( column, HIGH);
        }
      }
      // Keep the led on some time, to make it visible
      delay(a);
      // Turn the column off (anode of led)
      digitalWrite(column,LOW);
    }
    // Ready with this row, turn it off
    digitalWrite(row,HIGH);
  } 
}
// this is for scanning first row
void scanFirstRow() 
{ 
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
}
// this is for scanning second row
void scanSecondRow() 
{ 
  digitalWrite(8, LOW);
  digitalWrite(9, HIGH);
  digitalWrite(10, LOW);
}
// this is for scanning third row
void scanThirdRow() 
{ 
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, HIGH);
}
// this is for turn on first light on first row
void turnon1() 
{ 
  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  delay(a);
  digitalWrite(2, HIGH);
}