Hello
I'm new here, but not entirely new to Arduino, the last 2 days I tried working on a code to operate 4 buttons (Normally Opened) as 4 individual latches.
1 push switches output x on, another push switches it off, and the beginning state always needs to be low.
These 4 need to operate independently of each other, I wanted to use this as a basic control panel
to operate 4 output, 2 for power sockets 1 for lights and 1 for a compressor in my workspace.
The buttons are 1 NO with TRI color LED Ring light (I'm using red and blue) (red = off blue = on)
The state of the LED's is controlled by a simple relay, the relay is controlled by the MCU and the small relay in turn provides power to a contactor.
I build an electrical panel for this and used a small PCB with 2 transistors for each button as a bistable latch circuit, but it is glitching like hell any interference makes that circuit unstable, hence my idea of using an MCU.
Apart for those 4 tiny PCB's the electrical panel works as it should. I used the Latch example code found on this forum for 1 latch, and altered it to 4 latches.
Here is why my problems come in.
For some reason, I cannot select a board in Arduino IDE, and without a board selected I cannot verify the code, I tried reinstalling, I tried new drivers, I even tried on alder version of windows, but I'm probably missing something.
What I need a bit of help with is not the programming part itself, but if that code is right, maybe missed something, and if someone is willing to verify the code.
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;
const int ledPin1 = 13;
const int ledPin2 = 14;
const int ledPin3 = 15;
const int ledPin4 = 16;
const int latchPin1 = 9;
const int latchPin2 = 10;
const int latchPin3 = 11;
const int latchPin4 = 12;
boolean thisState1 = 0;
boolean thisState2 = 0;
boolean thisState3 = 0;
boolean thisState4 = 0;
boolean mySwitch1 = 0;
boolean mySwitch2 = 0;
boolean mySwitch3 = 0;
boolean mySwitch4 = 0;
boolean lastMySwitchState1 = 0;
boolean lastMySwitchState2 = 0;
boolean lastMySwitchState3 = 0;
boolean lastMySwitchState4 = 0;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(latchPin1, OUTPUT);
pinMode(latchPin2, OUTPUT);
pinMode(latchPin3, OUTPUT);
pinMode(latchPin4, OUTPUT);
Serial.begin(19200);
}
void loop() {
buttonState1 = digitalRead(buttonPin1);
if (buttonState1 == HIGH) {
digitalWrite(ledPin1, HIGH);
} else {
digitalWrite(ledPin1, LOW);
}
thisState1 = digitalRead(mySwitch1);
if (thisState1 != lastMySwitchState1)
{
lastMySwitchState1 = thisState1;
if(thisState1 == HIGH)
{
digitalWrite(latchPin1,!digitalRead(latchPin1));
}
{
buttonState2 = digitalRead(buttonPin2);
if (buttonState2 == HIGH) {
digitalWrite(ledPin2, HIGH);
} else {
digitalWrite(ledPin2, LOW);
}
thisState2 = digitalRead(mySwitch2);
if (thisState2 != lastMySwitchState2)
{
lastMySwitchState2 = thisState2;
if(thisState2 == HIGH)
{
digitalWrite(latchPin2,!digitalRead(latchPin2));
}
{
buttonState3 = digitalRead(buttonPin3);
if (buttonState3 == HIGH) {
digitalWrite(ledPin3, HIGH);
} else {
digitalWrite(ledPin3, LOW);
}
thisState3 = digitalRead(mySwitch3);
if (thisState3 != lastMySwitchState3)
{
lastMySwitchState3 = thisState3;
if(thisState3 == HIGH)
{
digitalWrite(latchPin3,!digitalRead(latchPin3));
}
{
buttonState4 = digitalRead(buttonPin4);
if (buttonState4 == HIGH) {
digitalWrite(ledPin4, HIGH);
} else {
digitalWrite(ledPin4, LOW);
}
thisState4 = digitalRead(mySwitch4);
if (thisState4 != lastMySwitchState4)
{
lastMySwitchState4 = thisState4;
if(thisState4 == HIGH)
{
digitalWrite(latchPin4,!digitalRead(latchPin4));
}
}
Serial.print("mySwitch1 State ");
Serial.print("mySwitch2 State ");
Serial.print("mySwitch3 State ");
Serial.print("mySwitch4 State ");
Serial.print(mySwitch1);
Serial.print(mySwitch2);
Serial.print(mySwitch3);
Serial.print(mySwitch4);
Serial.print(" switch State ");
Serial.println(latchPin1);
Serial.println(latchPin2);
Serial.println(latchPin3);
Serial.println(latchPin4);
}
I hope someone here is willing to guide me into the right direction.