I have the following code that opens and closes a solenoid valve. However I would like to press O for Open Or C for Close on the keyboard and this should send digitalWrite(pin, LOW); / digitalWrite(pin, HIGH); to my arduino.
#define VALVE_PIN 2
class valveControl {
//defining attributes
private:
byte pin;
public:
valveControl(byte pin) {
// Use 'this->' to make the difference between the
// 'pin' attribute of the class and the
// local variable 'pin' created from the parameter.
this->pin = pin;
init();
}
void init() {
pinMode(pin, OUTPUT);
// Always try to avoid duplicate code.
// Instead of writing digitalWrite(pin, LOW) here,
// call the function off() which already does that
off();
}
void on() {
digitalWrite(pin, HIGH);
}
void off() {
digitalWrite(pin, LOW);
}
}; // don't forget the semicolon at the end of the class
I study Computer Science and we are thought to code in OOP rather than procedural programming. Also there are other components/ objects that I would like to add. Still a newbie.