// This program turns an LED off when the switch is down, and on when the switch is up. #include #include using namespace std; int main() { char switch_state; ifstream button; ofstream led; led.open("/sys/class/gpio/gpio18/value"); //Infinite loop while(1) { /* Although this appears to be bad practice the file needs to be reopened so that the state of the pin is re-checked. */ button.open("/sys/class/gpio/gpio23/value"); if (button.is_open()) { button >> switch_state; button.close(); } else { cout << "Error: could not open file" << endl; } // Debug info cout << switch_state << endl; // Controlling the LED if(led.is_open()) { led << switch_state; led.flush(); } else { cout << "Error: could not open file" << endl; } } return 0; }