See the code in action on Tinkercad
//https://www.best-microcontroller-projects.com/arduino-digitalwrite.html #if defined(ARDUINO_AVR_UNO) #define setPin(b) ( (b)<8 ? PORTD |=(1<<(b)) : PORTB |=(1<<(b-8)) ) #define clrPin(b) ( (b)<8 ? PORTD &=~(1<<(b)) : PORTB &=~(1<<(b-8)) ) #define tstPin(b) ( (b)<8 ? (PORTD &(1<<(b)))!=0 : (PORTB &(1<<(b-8)))!=0 ) #elif defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) #define setPin(b) ( (b)<8 ? PORTH |=(1<<(b)) : PORTB |=(1<<(b-8)) ) #define clrPin(b) ( (b)<8 ? PORTH &=~(1<<(b)) : PORTB &=~(1<<(b-8)) ) #define tstPin(b) ( (b)<8 ? (PORTH &(1<<(b)))!=0 : (PORTB &(1<<(b-8)))!=0 ) #else #error Unsupported board selection. #endif //#define PIN_TST 13 const uint8_t PinOn13 = 13; static uint8_t FRED[3] = {1, 0, 0}; static uint8_t FGREEN[3] = {0, 1, 0}; static uint8_t FBLUE[3] = {0, 0, 1}; static uint8_t RED[3] = {255, 0, 0}; static uint8_t GREEN[3] = {0, 255, 0}; static uint8_t BLUE[3] = {0, 0, 255}; static uint8_t MAGENTA[3] = {255, 0, 255}; static uint8_t CYAN[3] = {0, 255, 255}; static uint8_t YELLOW[3] = {255, 255, 0}; static uint8_t WHITE[3] = {255, 255, 255}; static uint8_t OFF[3] = {0, 0, 0}; const uint8_t REDLED = 10; //180 Ohm Resistor RGB LED (higher resistance to compensate for Red LED being brighter) const uint8_t GREENLED = 9; //100 Ohm Resistor RGB LED const uint8_t BLUELED = 8; //100 Ohm Resistor RGB LED const uint8_t RDBTNLED = 6; //220 Ohm Resistor Red Button LED const uint8_t GRBTNLED = 5; //220 Ohm Resistor Green Button LED static uint8_t BTNRED[2] = {1, 0}; //Only RED Button ON static uint8_t BTNGREEN[2] = {0, 1}; //Only GREEN Button ON static uint8_t BTNON[2] = {1, 1}; //Both Buttons ON static uint8_t BTNOFF[2] = {0, 0}; //Both Buttons OFF /* static uint8_t BTNRED[2] = {255, 0}; //Only RED Button ON static uint8_t BTNGREEN[2] = {0, 255}; //Only GREEN Button ON static uint8_t BTNON[2] = {255, 255}; //Both Buttons ON static uint8_t BTNOFF[2] = {0, 0}; //Both Buttons OFF */ void setup () { pinMode(REDLED, OUTPUT); pinMode(GREENLED, OUTPUT); pinMode(BLUELED, OUTPUT); pinMode(RDBTNLED, OUTPUT); pinMode(GRBTNLED, OUTPUT); pinMode(13, OUTPUT); } void loop () { setPin(PinOn13); //Turn ON pin 13 Built-in LED // Set color to red setColour(RED); setBtnRed(); delay(2000); // Set color to green setColour(GREEN); setBtnGreen(); delay(2000); // Set color to blue setColour(BLUE); setBtnBoth(); delay(2000); // Set color to Off setColour(OFF); clrBtnBoth(); clrPin(PinOn13); //Turn OFF pin 13 Built-in LED delay(2000); } void setColour(uint8_t rgb[3]) { colour(rgb[0], rgb[1], rgb[2]); } void colour(uint8_t red, uint8_t green, uint8_t blue) { analogWrite(REDLED, red); analogWrite(GREENLED, green); analogWrite(BLUELED, blue); } void setBtnRed() { setPin(RDBTNLED); clrPin(GRBTNLED); } void setBtnGreen() { setPin(GRBTNLED); clrPin(RDBTNLED); } void setBtnBoth() { setPin(GRBTNLED); setPin(RDBTNLED); } void clrBtnBoth() { clrPin(GRBTNLED); clrPin(RDBTNLED); } /* void setBtnColour(uint8_t rgb[2]) { btnColour(rgb[0], rgb[1]); } void btnColour(uint8_t red, uint8_t green) { analogWrite(RDBTNLED, red); analogWrite(GRBTNLED, green); } */