// Program done on 2/02/2024 by Amaljith A
// Implement an SR flip-flop with inputs as S and R, outputs as Q and Q_Not.
const int switchSPin = 2; // Slide switch for Set (S)
const int switchRPin = 3; // Slide switch for Reset (R)
const int ledQPin = 4; // LED for Q output
const int ledQNotPin = 5; // LED for Q' (complement of Q) output
// Variables to store current and next state
bool Q = 0; // Current state of Q = Qn
bool Q_Not = 0; // Current state of Q_Not = (Qn+1)
bool S, R; //variables to store Inputs S and R
void setup() {
// Set pins of switch and LED's as inputs or outputs
pinMode(switchSPin, INPUT);
pinMode(switchRPin, INPUT);
pinMode(ledQPin, OUTPUT);
pinMode(ledQNotPin, OUTPUT);
}
void loop() {
// Read the state of the switches
S = digitalRead(switchSPin);
R = digitalRead(switchRPin);
// output based on the characteristic equation for SR flipflop
Q = S + (Q * !R);
Q_Not = R + (!Q * !S);
// Update the LEDs
digitalWrite(ledQPin, Q);
digitalWrite(ledQNotPin, Q_Not);
// Add a small delay for stability
delay(100);
}