Can't Verify to check a altered code

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.

you'll have to fix that. which IDE version and which board do you use?

how do you define right?

using arrays and a button library will make you life easier. Remember most buttons are bouncing.

I'm using version 2.3.2

with right, I mean if that code would work, uploading it in to pro micro or nano

As for bouncing, can u use a debounce?

one thing you can do is press ctrl-T (PC) or cmd-T (Mac) in the IDE to indent your code properly. You'll see it's definitely not correct (you made a mess with the matching {})

you could check if it compiles and even simulate your code using wokwi

you can add a circuit to your button to remove bouncing (a capacitor) or deal with it in software - which is what most libraries do.

I managed to get } errors out of the code (hopefully), now I'm left with the verify problem,

As far as I can see all the drivers are installed, Arduino IDE is administrator rights, but still no board to select. Any suggestion on what I might be overlooking?

This is where I'm at right now, I removed the LED state as the output is enough for that.

const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;

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(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);

  pinMode(latchPin1, OUTPUT);
  pinMode(latchPin2, OUTPUT);
  pinMode(latchPin3, OUTPUT);
  pinMode(latchPin4, OUTPUT);
}

void loop() {
  buttonState1 = digitalRead(buttonPin1);
  if (buttonState1 == HIGH) {
  }
  thisState1 = digitalRead(mySwitch1);
  if (thisState1 != lastMySwitchState1)
    lastMySwitchState1 = thisState1;
  if (thisState1 == HIGH) {
    digitalWrite(latchPin1, !digitalRead(latchPin1));
  }

  buttonState2 = digitalRead(buttonPin2);
  if (buttonState2 == HIGH) {

  }
  thisState2 = digitalRead(mySwitch2);
  if (thisState2 != lastMySwitchState2)
    lastMySwitchState2 = thisState2;
  if (thisState2 == HIGH) {
    digitalWrite(latchPin2, !digitalRead(latchPin2));
  }

  buttonState3 = digitalRead(buttonPin3);
  if (buttonState3 == HIGH) {

  }
  thisState3 = digitalRead(mySwitch3);
  if (thisState3 != lastMySwitchState3)
    lastMySwitchState3 = thisState3;
  if (thisState3 == HIGH) {
    digitalWrite(latchPin3, !digitalRead(latchPin3));
  }

  buttonState4 = digitalRead(buttonPin4);
  if (buttonState4 == HIGH) {

  }
  thisState4 = digitalRead(mySwitch4);
  if (thisState4 != lastMySwitchState4)
    lastMySwitchState4 = thisState4;
  if (thisState4 == HIGH)
    digitalWrite(latchPin4, !digitalRead(latchPin4));
}

consider this starting point to make your life easier - using the Toggle library

I created a class grouping a button, a led and a latch so that you have one grouped variable representing an element and you don't need to code many times all the checks.

click to see the code
#include <Toggle.h> // https://github.com/Dlloydev/Toggle

class ToggleGroup : public Toggle {
  public:
    ToggleGroup(const char * name, const byte buttonPin, const byte ledPin, const byte latchPin)
      : name(name), buttonPin(buttonPin), ledPin(ledPin), latchPin(latchPin) {}

    void begin() {
      pinMode(ledPin, OUTPUT);
      pinMode(latchPin, OUTPUT);
      Toggle::begin(buttonPin);
    }

    void ledOn() {
      digitalWrite(ledPin, HIGH);
    }
    void ledOff() {
      digitalWrite(ledPin, LOW);
    }
    void latchOn() {
      digitalWrite(latchPin, HIGH);
    }
    void latchOff() {
      digitalWrite(latchPin, LOW);
    }
    const char * getName() {
      return name;
    }

    void check() {
      Toggle::poll();
      if (Toggle::onPress()) {
        Serial.print(name);
        Serial.println(" pressed");
      }
      else if (Toggle::onRelease()) {
        Serial.print(name);
        Serial.println(" released");
      }
    }

  private:
    const char * name;
    byte buttonPin;
    byte ledPin;
    byte latchPin;
};

ToggleGroup groups[] = {
  {"G1", 2, 13, 9},
  {"G2", 3, 14, 10},
  {"G3", 4, 15, 11},
  {"G4", 5, 16, 12},
};

void setup() {
  for (auto& g : groups) g.begin();
  Serial.begin(115200);
  Serial.println("READY");
}

void loop() {
  for (auto& g : groups) g.check();
}

you could add the leds with their current limiting resistors and relays for your latched output for example and get everything running in wokwi

Which operating system are you using? If the IDE does not allow you to select a port, you'll have to check at the operating system level; in Windows, check Windows device manager, in Linux check the output of lsusb and dmesg and @J-M-L can tell you what to do on a Mac.

If your operating system does not react at all when you connect the board, check your cables; you might be using charge-only cables instead of data/sync cables.

I’m running Windows 11 23H2

Drivers are present, when I connect an Arduino Boards it shows up in Device Manger, I can change ports, I can delete it and reinstall the driver. However, it does not show up in Arduino IDE.

I did however locate a part of the problem, but do not know what causes it.

The problem is on my PC at my workspace. That PC is drive me crazy when using Arduino IDE
However, my game pc at home lets me run Arduino IDE without issues, I can select boards, I can verify code and upload it.

I managed to get the code above wording with the help of @J-M-L Thank you for your guidance in this.

I also tried using the library, but can't yet get it to work.

Furthermore, I also tried a completely different approach with the help of video's and a few books and managed to come up with the following code

const unsigned char In1Pin = 2;
const unsigned char In2Pin = 3;
const unsigned char In3Pin = 4;
const unsigned char In4Pin = 5;

const unsigned char Out1Pin = 9;
const unsigned char Out2Pin = 10;
const unsigned char Out3Pin = 11;
const unsigned char Out4Pin = 12;

const unsigned long Debounce = 30000;

unsigned long CurTime = micros();
unsigned long SignalTime1 = CurTime;
unsigned long SignalTime2 = CurTime;
unsigned long SignalTime3 = CurTime;
unsigned long SignalTime4 = CurTime;

void setup() {
	pinMode(In1Pin, INPUT_PULLUP);
	pinMode(In2Pin, INPUT_PULLUP);
	pinMode(In3Pin, INPUT_PULLUP);
	pinMode(In4Pin, INPUT_PULLUP);
	
	pinMode(Out1Pin, OUTPUT);
	digitalWrite(Out1Pin, 0);
	pinMode(Out2Pin, OUTPUT);
	digitalWrite(Out2Pin, 0);
	pinMode(Out3Pin, OUTPUT);
	digitalWrite(Out3Pin, 0);
	pinMode(Out4Pin, OUTPUT);
	digitalWrite(Out4Pin, 0);
}

void loop() {
	CurTime = micros();
	
	if (digitalRead(In1Pin) == 0) {
		if ((unsigned long)(CurTime-SignalTime1) >= Debounce) {
			digitalWrite(Out1Pin, !digitalRead(Out1Pin));
		}
		SignalTime1 = CurTime;
	}
	
	if (digitalRead(In2Pin) == 0) {
		if ((unsigned long)(CurTime-SignalTime2) >= Debounce) {
			digitalWrite(Out2Pin, !digitalRead(Out2Pin));
		}
		SignalTime2 = CurTime;
	}
	
	if (digitalRead(In3Pin) == 0) {
		if ((unsigned long)(CurTime-SignalTime3) >= Debounce) {
			digitalWrite(Out3Pin, !digitalRead(Out3Pin));
		}
		SignalTime3 = CurTime;
	}
	
	if (digitalRead(In4Pin) == 0) {
		if ((unsigned long)(CurTime-SignalTime4) >= Debounce) {
			digitalWrite(Out4Pin, !digitalRead(Out4Pin));
		}
		SignalTime4 = CurTime;
	}
}

I tried this code and successfully uploaded it into a Pro Micro using a 32u4 MC.

I still am left with an unwilling PC that simply refuses to let me run Arduino IDE and select a board. I have no idea where to look and what to do with this error. I tried reinstalling Arduino, I tried reinstalling windows, I tried using an older version of windows, back to Windows 7 I'm about to throw that pc out the window it's driving me nuts.

Consider.
For debouncing, start here:

I worked up this little Wokwi demo for you. You'll have to think through the ganssle information, as well as boning up on arrays. Note that you can vary this from 1 through 6 relays/LEDs by simply changing the NUMVALS constant and pin numbers in their arrays.

You can remove the LEDs if you want, by deleting the associated lines. Often, I find I want an indicator on a panel, but the relay board is hidden.

Which version of the IDE?
Is there any chance that an antivirus software blocks an application that is used by / part of the IDE?

In case you're not going to Wokwi,

//define some constants
const uint16_t NUMVALS = 4;  //explicitly defines size of arrays for inputs and states; guards against instances of arrays of different sizes
const uint8_t PRESSED = 0;//can be 0 or 1, depending on wiring of buttons; we have assumed button to ground, pin pullup enabled
const uint16_t buttonPin[NUMVALS] = {2, 3, 4, 5};
const uint16_t latchPin[NUMVALS] = {9, 10, 11, 12};
const uint16_t ledPin[NUMVALS] = {14, 15, 16, 17};

//variable arrays; they are global so auto-init to 0
bool rawButtonState[NUMVALS];
bool buttonState[NUMVALS];
bool prevButtonState[NUMVALS];
bool outputLatchState[NUMVALS];
uint16_t debounceState[NUMVALS];//stores bitfield for debouncing, so needs to be an integer

void setup() {
  for (uint16_t index = 0; index < NUMVALS; index++) {
    pinMode(buttonPin[index], INPUT_PULLUP);
    pinMode(ledPin[index], OUTPUT);
    pinMode(latchPin[index], OUTPUT);
  }
  Serial.begin(19200);
}
void loop() {  //(IP(M)O)  Input, Process, (Message) Output structure
  for (uint16_t index = 0; index < NUMVALS; index++) {   //do only one input-output set at a time
    //INPUT; read button
    rawButtonState[index] = digitalRead(buttonPin[index]) == PRESSED ? true : false;    //returns true if button pressed
    //PROCESS  debounce and conditionally change
    debounceState[index] = debounceState[index] << 1 | !rawButtonState[index] | 0xe000; // uses Ganssle shifting-0 technique
    buttonState[index] = (debounceState[index] == 0xf000) ? 1 : 0;                      //modify buttonState only if a new, debounced, button press has been detected
    if (buttonState[index] != prevButtonState[index]) {                                 //button wasn't pressed, now is, so set/reset output
      prevButtonState[index] = buttonState[index];                                      //follow button
      if (buttonState[index]) {                                                         //button pressed, so act on it
        outputLatchState[index] = !outputLatchState[index];  //swaps output state
        Serial.print("Button pressed: ");
        Serial.print(index);
        Serial.print(" Output State: ");
        Serial.println(outputLatchState[index]);
      }
    }
    //OUTPUT write output
    digitalWrite(latchPin[index], outputLatchState[index]); //write the output
    digitalWrite(ledPin[index], !outputLatchState[index]);  //write the indicator(in this case, I have the indicator reverse-logic to the Latch)
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.