DIYstompboxes.com

DIY Stompboxes => Digital & DSP => Topic started by: patrick398 on July 08, 2020, 06:13:14 PM

Title: Arduino/Attiny85 code help
Post by: patrick398 on July 08, 2020, 06:13:14 PM
So i'm trying to bend my tiny mind around coding and this is my first experience with anything like this so i'm crawling along. I want to use an ATtiny85 to control a relay and LED for bypass.
I have cobbled this code together from snippets i've found on the internet, had to edit it a few times so might have got confused, and it's possible it's complete jibberish.
Would someone mind taking a look? I'm going to be away from my workshop for a few days so can't actually try it out.

I need to edit the pin numbers to work with the '85 chip, but i think they're right for the arduino UNO. After a bit of fiddling it now verifies which is promising but i'm sure there's loads of mistakes in there. I can just about follow it and have a rough idea what's doing what. Debouncing is coming form the ezButton library.

//CHANGE PIN NUMBERS FOR ATTINY85


#include <ezButton.h>

/// constants won't change
const int LED_PIN    = 4; // Arduino pin connected to LED's pin
const int BUTTON_PIN = 7; // Arduino pin connected to button's pin
const int RELAY_PIN  = 3; // Arduino pin connected to relay's pin

ezButton button(BUTTON_PIN);  // create ezButton object that attach to pin 7;

// variables will change:
int relayState = LOW;   // the current state of relay
int ledState = LOW;     // the current state of LED
int lastButtonState;    // the previous state of button
int currentButtonState; // the current state of button

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
  pinMode(RELAY_PIN, OUTPUT);        // set arduino pin to output mode
  pinMode(LED_PIN, OUTPUT);   // set arduino pin to output mode
  button.setDebounceTime(50); // set debounce time to 50 milliseconds
}

void loop() {
  button.loop(); // MUST call the loop() function first

  if (button.isPressed()) {

    // toggle state of relay
    relayState = !relayState;

    // control relay arccoding to the toggled state
    digitalWrite(RELAY_PIN, relayState);


    if (button.isPressed()) {
      digitalWrite(RELAY_PIN, HIGH); // turn on
    }
    else if (button.isPressed()) {
      digitalWrite(RELAY_PIN, LOW);  // turn off

      if (button.isPressed()) {

        // toggle state of LED
        ledState = !ledState;

        // control LED arccoding to the toggleed sate
        digitalWrite(LED_PIN, ledState);
      }
    }


  }
}




My brain hurts  :icon_exclaim:
Title: Re: Arduino/Attiny85 code help
Post by: ElectricDruid on July 08, 2020, 08:13:26 PM
I don't really speak Arduino, so I can't say much about the syntax. The only thing that jumps out is that I can't see anywhere where 'LOW' is defined. Does the Arduino environment give you Boolean variables like LOW and HIGH to use, or do you have to do that yourself?

The logic is still scrambled. You do the same test "if (button.isPressed()) {" about four times!

Do it once, and put *all* the stuff that happens when the button is pressed in that section.

Also this is not good:

if (button.isPressed()) {
     // Stuff
} else if (button.isPressed()) {
    // Other stuff
}


No wonder your brain hurts! Trying to make sense out of that will do that to a person!
You could just leave the condition out of the bottom half, in which case it will run whenever the button is *not* pressed (e.g. all the rest of the time, over and over again)
if (button.isPressed()) {
     // Stuff
} else {
    // Other stuff
}


Or you might want to test for some other event, but only when the button is not pressed - the "else" clause will only run if the first condition is not met. But I don't see any other conditions anywhere in your code.
if (button.isPressed()) {
     // Stuff
} else (some other condition){
    // Other stuff
}


I'd guess it could be like this:

if (button.isPressed()) {

    // toggle state of relay
    relayState = !relayState;

    // control relay arccoding to the toggled state
    digitalWrite(RELAY_PIN, relayState);

    // toggle state of LED
   ledState = !ledState;

    // control LED arccoding to the toggleed sate
    digitalWrite(LED_PIN, ledState);
}


If the button is pressed, we toggle the relay, and then write the new state, and then toggle the LED, and write its new state too. Job done!


Title: Re: Arduino/Attiny85 code help
Post by: vigilante397 on July 08, 2020, 08:53:56 PM
This is the code I used with ATTiny85, using the Arduino IDE (with added comments because I almost never comment my code while I'm writing it :P):

int chState = 0; //starts off

void setup() {
  pinMode(3, INPUT_PULLUP); //set pin modes
  pinMode(1, OUTPUT);
}

void loop() {
  int bypass = digitalRead(4); //read the status of the switch
  if (bypass == 0) //if the button has been pressed
  {
    if (byState == 0) //if the current state is off
    {
      digitalWrite(0, HIGH); //turn on
      byState = 1; //set the current state flag to on
      delay(100); //100ms debounce delay
    }
    else if (byState == 1) //if the current state is on
    {
      digitalWrite(0, LOW); //turn off
      byState = 0; //set current state flag to off
      delay(100); //100ms debounce delay
    }
  }
}
}


It's dirt simple, but I think it's pretty intuitive and it definitely gets the job done. I like the ATTiny85 because it's self-contained and tiny but still has enough IO to run a pair of switches, I recently did one for bypass and a channel switch with a single ATTiny85.
Title: Re: Arduino/Attiny85 code help
Post by: niektb on July 09, 2020, 03:37:00 AM
Quote from: ElectricDruid on July 08, 2020, 08:13:26 PM
Also this is not good:

if (button.isPressed()) {
     // Stuff
} else if (button.isPressed()) {
    // Other stuff
}


Agreed with ElectricDruid. You're checking twice for the same condition (if apple then ... otherwise if apple). This means that the 'other stuff' in the code snippet above will never execute.
Vigilante's approach will work for simple switching systems. However, if you have multiple states (for example if you incorporate series/parallel switching or whatever) you'd be better off using a simple state machine.

Okay, I'm drawing up a bit of code for a digitally-controlled PT2399 pedal, with some more modes that are controlled using a single footswitch (Ideas I'm toying with are a Tap Tempo Mode and a Bypass Mode, Modulation Mode etc.). The whole stuff is a little work-in-progress (only the mode switching and the RGB coloring is built-in yet, and quite rough still) so it's not guaranteed to work haha. This code is btw tested on a Arduino Nano.


/* HARDWARE DEFINITION */
// RGB LED Definition
const int pin_rgb_r       = 9;
const int pin_rgb_g       = 10;
const int pin_rgb_b       = 11;
// Footswitch
const int pin_footswitch  = 2;

/* FIRMWARE SETUP */
// Mode definitions and variables
#define MODE_BYPASS 0
#define MODE_TEMPO  1
#define MODE_MOD    2
#define MODE_TAILS  3
int mode = MODE_BYPASS;
#define MODE_BYPASS_COLOR 0xFF0000
#define MODE_TEMPO_COLOR  0xFFFF00
#define MODE_MOD_COLOR    0xFF00FF
#define MODE_TAILS_COLOR  0xFFFFFF

// Switch debounce variables
unsigned long debounce_time_ms = 200;
unsigned long debounce_timer = 0;
bool debouncing = false;

/* MAIN FUNCTIONS */
void setup() {
  // Initialize Serial Port
  Serial.begin(115200);
 
  // Initialize RGB LED
  pinMode(pin_rgb_r, OUTPUT);
  pinMode(pin_rgb_g, OUTPUT);
  pinMode(pin_rgb_b, OUTPUT);

  // Initialize Footswitch
  pinMode(pin_footswitch, INPUT);
  attachInterrupt(digitalPinToInterrupt(pin_footswitch), switchMode, FALLING);

  modeSetRGB();
}

void loop() {
  if (debouncing && (millis() - debounce_timer >= debounce_time_ms)) {
    attachInterrupt(digitalPinToInterrupt(pin_footswitch), switchMode, FALLING);
    debouncing = false;
  }
}

/* CORE FUNCTIONS */
void switchMode () {
  detachInterrupt(digitalPinToInterrupt(pin_footswitch)); // disable more interrupts
  debouncing = true;
  debounce_timer = millis();

  mode++;
  if (mode > MODE_TAILS)
    mode = MODE_BYPASS;

  modeSetRGB();
  Serial.println(mode);
}

void modeSetRGB(){
  if (mode = MODE_BYPASS)
    RGB_color(MODE_BYPASS_COLOR);
  else if (mode = MODE_TEMPO)
    RGB_color(MODE_TEMPO_COLOR);
  else if (mode = MODE_MOD)
    RGB_color(MODE_MOD_COLOR);
  else // mode = MODE_TAILS
    RGB_color(MODE_TAILS_COLOR); 
}

/* HELPER FUNCTIONS */
void RGB_color(int val) {
  int R = val >> 16;
  int G = (val >> 8) & 0xF;
  int B = val & 0xF;
 
  analogWrite(pin_rgb_r, R);
  analogWrite(pin_rgb_g, G);
  analogWrite(pin_rgb_b, B);
}


Now don't look at how I use interrupts, that might cloud your understanding of this code. Basically, the microcontroller idles around (and in my case, will probably be busy looking at pot values and controlling PT2399) till you press a button :)
What I want to zoom in on is the function switchMode(). Every state (or mode) of the pedal is represented by a numeric value (0 to 3) and a corresponding color. Every time you press a button, the function switchMode() is executed, which changes the mode to the next state and calls the modeSetRGB() function once which updates the RGB LED color. (and in your case, you could create a modeSetBypass() function which updates the Bypass relay f.e.

Okay bottomline: If this is too advanced for you yet, forget anything I said (in order to not get confused :icon_mrgreen: ) and stick with the basics. Otherwise, this might provide an interesting foundation for more complex switching  ;D
Title: Re: Arduino/Attiny85 code help
Post by: potul on July 09, 2020, 04:54:32 AM
Your code in the first post is really a mess... you are doing things twice (changing state of the relay, etc...). I have cleaned a little your code, and left only what should be there. I have also added something in the setup function to set the initial state of relay and LED, otherwise it remains undefined until you press the button the first time. Give it a try.
BTW, you are defining a couple of variables that are never used (lastButtonState and currentButtonState)


//CHANGE PIN NUMBERS FOR ATTINY85

#include <ezButton.h>

/// constants won't change
const int LED_PIN    = 4; // Arduino pin connected to LED's pin
const int BUTTON_PIN = 7; // Arduino pin connected to button's pin
const int RELAY_PIN  = 3; // Arduino pin connected to relay's pin

ezButton button(BUTTON_PIN);  // create ezButton object that attach to pin 7;

// variables will change:
int relayState = LOW;   // the current state of relay
int ledState = LOW;     // the current state of LED
int lastButtonState;    // the previous state of button
int currentButtonState; // the current state of button

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
  pinMode(RELAY_PIN, OUTPUT);        // set arduino pin to output mode
  pinMode(LED_PIN, OUTPUT);   // set arduino pin to output mode
  button.setDebounceTime(50); // set debounce time to 50 milliseconds
  digitalWrite(RELAY_PIN, relayState);
  digitalWrite(LED_PIN, ledState);
}

void loop() {
  button.loop(); // MUST call the loop() function first

  if (button.isPressed()) {

    // toggle state of relay
    relayState = !relayState;

    // control relay arccoding to the toggled state
    digitalWrite(RELAY_PIN, relayState);
 
    // toggle state of LED
    ledState = !ledState;

    // control LED arccoding to the toggleed sate
   digitalWrite(LED_PIN, ledState);
   
  }
}


On the other side, I think the approach of toggling the relay and LEDs but not synchronizing them is not the best one.... Supposedly you always want the LED to be ON when the relay is ON, and viceversa, so you really don't need to keep track of both states, just one state should make it, unless you want to do more fancy stuff with the LED. So I would leave it like that:

//CHANGE PIN NUMBERS FOR ATTINY85

#include <ezButton.h>

/// constants won't change
const int LED_PIN    = 4; // Arduino pin connected to LED's pin
const int BUTTON_PIN = 7; // Arduino pin connected to button's pin
const int RELAY_PIN  = 3; // Arduino pin connected to relay's pin

ezButton button(BUTTON_PIN);  // create ezButton object that attach to pin 7;

// variables will change:
int relayState = LOW;   // the current state of relay
int lastButtonState;    // the previous state of button
int currentButtonState; // the current state of button

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
  pinMode(RELAY_PIN, OUTPUT);        // set arduino pin to output mode
  pinMode(LED_PIN, OUTPUT);   // set arduino pin to output mode
  button.setDebounceTime(50); // set debounce time to 50 milliseconds
  digitalWrite(RELAY_PIN, relayState);
  digitalWrite(LED_PIN, relayState);
}

void loop() {
  button.loop(); // MUST call the loop() function first

  if (button.isPressed()) {

    // toggle state of relay
    relayState = !relayState;

    // control relay arccoding to the toggled state
    digitalWrite(RELAY_PIN, relayState);
 
    // control LED arccoding to the toggleed sate
   digitalWrite(LED_PIN, relayState);
   
  }
}
Title: Re: Arduino/Attiny85 code help
Post by: patrick398 on July 09, 2020, 05:04:33 AM
Thanks so much for all the replies guys, really helpful info! As suspected that method of combining different snippets of code makes things confusing and convoluted, and makes sense that i ended up with the same instructions several times.
Thanks for all the pointers and for the additional code too, i'm going to spend the next few days going through it to try and properly understand every line. There's definitely a couple i don't understand so no doubt i'll be back with more questions :)
Title: Re: Arduino/Attiny85 code help
Post by: patrick398 on July 09, 2020, 05:40:34 AM
Also, is this code setting the relay pin high on one button press, and low on another? So i can use this with a non-latching relay?
At some point i might want to include the optoisolator muting, but i'll try and get the main bypass working first. I'm cartwheeling before i can walk  :icon_lol:
Title: Re: Arduino/Attiny85 code help
Post by: potul on July 09, 2020, 10:25:48 AM
This code is basically setting both LED and relay LOW on startup, and then toggling both between HIGH and LOW on every button press.


Title: Re: Arduino/Attiny85 code help
Post by: patrick398 on July 09, 2020, 11:27:45 AM
Quote from: potul on July 09, 2020, 10:25:48 AM
This code is basically setting both LED and relay LOW on startup, and then toggling both between HIGH and LOW on every button press.

Thanks! So the relay pin stays high until toggled low and I can use a non latching relay?
Title: Re: Arduino/Attiny85 code help
Post by: vigilante397 on July 09, 2020, 11:39:21 AM
My personal opinion is that it's easiest to use a non-latching relay and put the LED in parallel with the coil, that way you only need to use one output pin to drive both.
Title: Re: Arduino/Attiny85 code help
Post by: ElectricDruid on July 09, 2020, 03:06:33 PM
Quote from: patrick398 on July 09, 2020, 11:27:45 AM
Quote from: potul on July 09, 2020, 10:25:48 AM
This code is basically setting both LED and relay LOW on startup, and then toggling both between HIGH and LOW on every button press.

Thanks! So the relay pin stays high until toggled low and I can use a non latching relay?

Yes. As written, that's what it expects.
Title: Re: Arduino/Attiny85 code help
Post by: knutolai on July 10, 2020, 10:39:00 AM
Any luck with the code I sent you? It's verified as working with the bounce2 library.
Title: Re: Arduino/Attiny85 code help
Post by: patrick398 on July 13, 2020, 02:07:19 PM
Quote from: knutolai on July 10, 2020, 10:39:00 AM
Any luck with the code I sent you? It's verified as working with the bounce2 library.

I didn't have any latching relays so wanted to try get something working with the non-latching ones i have :)
Title: Re: Arduino/Attiny85 code help
Post by: patrick398 on July 21, 2020, 05:34:46 PM
Haven't tried the code properly with a relay but used a momentary toggle to switch an LED and that all seems to work well. Just sketching a circuit on Kicad and it occurred to me i'd need a transistor to drive the relay. Can i set it up like this and use it to also drive the LED?
Thanks again for all your help. I'm having a lot of fun with the arduino!


(https://i.postimg.cc/CRcjfqNx/Screenshot-2020-07-21-at-22-30-13.png) (https://postimg.cc/CRcjfqNx)
Title: Re: Arduino/Attiny85 code help
Post by: potul on July 22, 2020, 02:27:27 AM
If you always want the LED in sync with the Relay, yes, you can do it this way. Looking at the schematic, there is no limiting resistor for the LED.. is this OK?

Title: Re: Arduino/Attiny85 code help
Post by: niektb on July 22, 2020, 02:47:41 AM
Don't you need to connect pin 12 of the relay to 5V also?
Title: Re: Arduino/Attiny85 code help
Post by: patrick398 on July 22, 2020, 03:18:23 AM
Quote from: potul on July 22, 2020, 02:27:27 AM
If you always want the LED in sync with the Relay, yes, you can do it this way. Looking at the schematic, there is no limiting resistor for the LED.. is this OK?

Ah yes i need a CLR, thanks!
I can't think of any reason why i wouldn't want to LED to always be in sync with the relay.

Quote from: niektb on July 22, 2020, 02:47:41 AM
Don't you need to connect pin 12 of the relay to 5V also?

The relay is connected to 5 volts through the transistor. When the ATtiny85 senses a button press it puts a voltage on the base of the transistor to turn it on, which turns the relay on. Another button press cuts the voltage to the base, so the transistor and relay both turn off
Title: Re: Arduino/Attiny85 code help
Post by: niektb on July 22, 2020, 04:01:03 AM
Aaah yes my bad, I've seen low-side relay switching too often  :icon_mrgreen:
Title: Re: Arduino/Attiny85 code help
Post by: ElectricDruid on July 22, 2020, 06:48:43 AM
Quote from: patrick398 on July 22, 2020, 03:18:23 AM
Quote from: niektb on July 22, 2020, 02:47:41 AM
Don't you need to connect pin 12 of the relay to 5V also?

The relay is connected to 5 volts through the transistor. When the ATtiny85 senses a button press it puts a voltage on the base of the transistor to turn it on, which turns the relay on. Another button press cuts the voltage to the base, so the transistor and relay both turn off

Nope, I still don't get it. If that's the case (and it clearly is) then doesn't pin 12 need to be connected to ground? I don't see how that coil operates with only one wire attached to it.
Title: Re: Arduino/Attiny85 code help
Post by: potul on July 22, 2020, 09:02:56 AM
Definitively, there is something wrong in the relay connection. I would review the whole thing.

I would put the relay coil between 5v and the transistor collector. And do the same with the LED and CLR (in parallel with the relay)

Title: Re: Arduino/Attiny85 code help
Post by: patrick398 on July 22, 2020, 12:55:40 PM
Ah yes sorry i've left the GND connection off pin 12 of the relay. This should make more sense  :icon_lol:

The relay connections are the same as when i was controlling it with a 555 which worked well, so hoping it still works ok with the uC


(https://i.postimg.cc/PvyqpTzg/Screenshot-2020-07-22-at-17-53-11.png) (https://postimg.cc/PvyqpTzg)
Title: Re: Arduino/Attiny85 code help
Post by: Firesledge on July 26, 2020, 11:06:50 AM
It would be safer to put the whole load on the collector side (and therefore tie the relay pin 12 to +5V, and flip the diodes). Otherwise the emitter potential will be floating. Reaching the saturated zone will be not as straightforward and you might not get enough voltage to activate the relay. Then you could use a higher value for the base resistor, to avoid drawing too much current from the ATtiny pin.
Title: Re: Arduino/Attiny85 code help
Post by: patrick398 on September 28, 2020, 05:31:14 PM
Continuing with my arduino adventures and the theme of head meeting wall....
is there anything about this code that would cause the muting not to work as expected. I'm wondering if there's something about the button debounce that is not compatible with the delay on opto pin.
The opto should turn on and ground the output 30ms before the relay flips and turn off 30ms after but i don't think i can detect any muting and it sounds pretty much identical with or without the opto in place.

//CHANGE PIN NUMBERS FOR ATTINY85

#include <ezButton.h>


const int BUTTON_PIN = 3; // footswitch connected to attiny (2)
const int RELAY_PIN  = 0; // relay connected to attiny (5)
const int OPTO_PIN = 1; // opto connected to attiny pin (6)
int onTime = 30;

ezButton button(BUTTON_PIN);  // create ezButton object that attach to pin 7;

// variables will change:
int relayState = LOW;   // the current state of relay
int lastButtonState;    // the previous state of button
int currentButtonState; // the current state of button

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
  pinMode(RELAY_PIN, OUTPUT);        // set arduino pin to output mode
  button.setDebounceTime(40); // set debounce time to 50 milliseconds
  digitalWrite(RELAY_PIN, relayState);
}

void loop() {
 
  button.loop(); // MUST call the loop() function first

  if (button.isPressed()) {
   
    digitalWrite (OPTO_PIN, HIGH);
    delay (onTime);
   

    // toggle state of relay
    relayState = !relayState;

    // control relay arccoding to the toggled state
    digitalWrite(RELAY_PIN, relayState);
    delay(onTime);

  digitalWrite (OPTO_PIN, LOW);

  }
}


Thanks! (again)
Title: Re: Arduino/Attiny85 code help
Post by: ElectricDruid on September 28, 2020, 06:37:59 PM
Seems pretty straightforward. I can't see anything obvious.

Some possibilities occur to me:

1) The relay switching just isn't that noisy, hence no difference with or without the opto
2) The opto muting isn't doing what you think it's doing in the hardware for some reason. How long does the opto take to turn on? (30msecs or more is going to make it useless)
3) The PIN numbers are wrong so the opto is never getting switched
Title: Re: Arduino/Attiny85 code help
Post by: potul on September 29, 2020, 02:25:28 AM
First of all, the delay() instructions you use ARE affecting the button debouncing, but in a "good way". What happens is that you add 2 delays of 30ms in the loop, so in reality, the debouncing routing will only be called  60ms after the button press is detected. I'm not familiar with ezButton inner code, but I would say this should not affect your code, as the button press is already detected and debounced.

What I could try in order to debug the whole system is:

-Try longer delays
-Add an LED or similar to the opto, to check that it's really activating

What opto are you using? It can be tricky sometimes to get an opto to fully mute your signal.

I have an unfinished project that uses an opto to change the volume of a signal sending it to ground, and I can tell I had to light the LED in the opto reaaaaaally bright to make it "almost" mute the signal, and still it's not really 0.





Title: Re: Arduino/Attiny85 code help
Post by: patrick398 on September 29, 2020, 06:51:55 AM
I've tried a few different things. The code is definitely doing what it should, i've hooked up the relay bypass on a breadboard with a hand rolled opto and it's working, pretty quietly. There's more of a fade in/out which i guess is down to the response time of the LDR. When i try it with the TLP222A though it's pretty indistinguishable from when it's not in circuit at all. I started to think the optp itself was causing some noise but the pop is basically identical with or without it.
Even more frustratingly, the pop is worse with the TLP222 on the PCB, and even using the hand rolled opto on the PCB i'm still getting pop. These results are so inconsistent i'm not even sure where to look. I thought it could be a layout problem, admittedly there's an output trace which runs between the relay coil  :icon_redface: but i cut that trace and wired the output with a piece of wire avoiding the relay and got the same result.
More fiddling required...
Title: Re: Arduino/Attiny85 code help
Post by: patrick398 on September 29, 2020, 07:44:52 AM
Seems as though the button itself is causing the noise? I've changed the code so everything happens slowly. Button is pressed, i hear a pop instantly, opto lights, delay of 100ms, relay flips, no noise, delay for 1000ms, opto off, signal unmutes.

Without the opto: button is pressed, i hear a pop instantly, delay of 100ms, relay flips, pop!

So the opto is definitely covering the relay switching noise, but there's a pop upon button press which can't be caused by the opto because it's present with and without the opto.

So when everything is running real time i think i'm getting two pops, very close together. The first upon button press, and the second as the relay flips (which i can cancel with the opto).
But where the hell is that first pop coming from?
Title: Re: Arduino/Attiny85 code help
Post by: ElectricDruid on September 29, 2020, 09:39:59 AM
Good work on the debugging. That seems to clear up a lot of things.

Quote from: patrick398 on September 29, 2020, 07:44:52 AM
But where the hell is that first pop coming from?

The pin is set up as "INPUT_PULLUP", isn't it? Does that set it up *both* as an input *and* with a pull-up? And the button is then connecting the pin to ground? No other possible short from the pin?

The only thing I can think of is that the button is yanking the power rail around somehow, probably by shorting something. Like might happen if the button input had the output driver still turned on and driving the pin high (which you wouldn't notice when you read it, since it's *supposed* to read high with the pull-up anyway).

Title: Re: Arduino/Attiny85 code help
Post by: patrick398 on September 29, 2020, 01:15:49 PM
Quote from: ElectricDruid on September 29, 2020, 09:39:59 AM
Good work on the debugging. That seems to clear up a lot of things.

Quote from: patrick398 on September 29, 2020, 07:44:52 AM
But where the hell is that first pop coming from?

The pin is set up as "INPUT_PULLUP", isn't it? Does that set it up *both* as an input *and* with a pull-up? And the button is then connecting the pin to ground? No other possible short from the pin?

The only thing I can think of is that the button is yanking the power rail around somehow, probably by shorting something. Like might happen if the button input had the output driver still turned on and driving the pin high (which you wouldn't notice when you read it, since it's *supposed* to read high with the pull-up anyway).

Good shout. I did try putting a 1k resistor in between the switch and ground so that it's not connecting straight to ground but the result was the same. I should upload a schematic and maybe the layout too, i think it's pretty sound but maybe there's something in there i can't see
Title: Re: Arduino/Attiny85 code help
Post by: patrick398 on October 12, 2020, 11:11:39 AM
Had some more time to fiddle with this, putting the PCB in an enclosure seems to make it a lot quieter. In terms of switching noise it's actually basically silent now, there's little to no relay noise. But when i press the switch there is a bit of crackling noise, not especially loud but it can be heard even when playing guitar so not ideal obviously. I'm stumped as to why this is happening though. Somehow some noise from the switch contacts is making its way into the audio path. I tried changing the switch but same issue. It's not being generated from the relay switching because i can hear it if i press the switch and wiggle it and it's there even before and after the relay flips. It kind of sounds like some DC is getting onto the audio path somewhere. Anything obvious come to mind? Or anybody had similar issues before?

Thanks