Go Down

Topic: slow detected state change (Read 247 times) previous topic - next topic

joural

Jan 19, 2019, 02:27 pm Last Edit: Jan 19, 2019, 02:31 pm by joural
Hello,
so im using this code to control apa102 led strip
two switches connected to between pins 7 & 9, 8 & 10
when either or both of these switches are closed the change on
led strip takes about 5 seconds, and if switch1 is closed, then open and
switch 2 is closed it registers as both switches are closed for about 5 seconds
any idea what might be causing this behaviour please?
code is more less just glued together from various sources as this forum
stackoverflow and such. when i set pins 9 and 10 set as INPUT_PULLUP
it doesnt work at all. for testing purposes switches are currently replaced with
jumper cables betweeen above mentioned pins.
thank you for help.
P.S. its running on arduino nano, i had to use old bootloader to be able to upload it. If it helps
Code: [Select]
#include <hsv2rgb.h>
#include <fastspi.h>
#include <noise.h>
#include <FastLED.h>
#include <fastspi_dma.h>
#include <colorutils.h>
#include <controller.h>
#include <led_sysdefs.h>
#include <platforms.h>
#include <fastspi_types.h>
#include <fastled_config.h>
#include <pixelset.h>
#include <bitswap.h>
#include <fastspi_nop.h>
#include <lib8tion.h>
#include <fastled_progmem.h>
#include <cpp_compat.h>
#include <power_mgt.h>
#include <pixeltypes.h>
#include <color.h>
#include <fastpin.h>
#include <fastspi_ref.h>
#include <fastspi_bitbang.h>
#include <colorpalettes.h>
#include <fastled_delay.h>
#include <chipsets.h>
#include <FastLED.h>
//leds conf
#define DATA_PIN    12
#define CLOCK_PIN   11
#define NUM_LEDS    22
int buttonled1 = 9;   // the number of the pushbutton p
int buttonled2 = 10;
CRGB leds[NUM_LEDS];


void setup()
{
  FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);          // sets 2 digital pins 7 & 8 as output
  pinMode(9, INPUT);
  pinMode(10, INPUT);
  digitalWrite(8, HIGH);       // sets the digital pin 13 on
  digitalWrite(7, HIGH);
}

void loop()
{
  buttonled1 = digitalRead(9);
  buttonled2 = digitalRead(10);
  if (buttonled1 == HIGH && buttonled2 == HIGH) // black
    {
    LEDS.showColor(CRGB(255, 255, 255)); // off
    }
  if (buttonled1 == LOW && buttonled2 == HIGH) //red
    {
    LEDS.showColor(CRGB(255, 0, 0));
    }
  if (buttonled1 == HIGH && buttonled2 == LOW) //blue
    {
    LEDS.showColor(CRGB(0, 0, 255));
    } 
  if (buttonled1 == LOW && buttonled2 == LOW) //white
    {
    LEDS.showColor(CRGB(0, 0, 0));
    } 
}

PaulRB

Quote
two switches connected to between pins 7 & 9, 8 & 10
That doesn't make sense. Why would 4 pins be needed for 2 switches? How have you connected the switches? What type of switch are they?

joural

Okey maybe my problem is wrong connected switches, it's just cable from output pin set high to input pin waiting for state

Grumpy_Mike

#3
Jan 19, 2019, 05:06 pm Last Edit: Jan 19, 2019, 05:07 pm by Grumpy_Mike
Quote
when i set pins 9 and 10 set as INPUT_PULLUP it doesnt work at all.
Switches should be wired between the input pin and ground. Then do the above. Note that when reading the switches high and low will be swapped to what you might be used to.

Then we need a schematic, note not a Fritzing layout diagram a schematic. Hand drawn will be fine.

PaulRB

#4
Jan 19, 2019, 07:01 pm Last Edit: Jan 19, 2019, 07:01 pm by PaulRB
Okey maybe my problem is wrong connected switches, it's just cable from output pin set high to input pin waiting for state
That will not work, because the switch will not be able to connect the input pin to ground. To read HIGH, a pin must be connected to 5V. To read LOW it must be connected to ground. With your idea, the switch will read HIGH when it is pressed, because it will be connected to 5V by the output pin. But when the switch is not pressed, the input pin is not connected to anything, so will probably continue to read HIGH, or perhaps LOW, or who knows? This is what we call a "floating input" and it will not give a reliable, predictable reading.

If you change your output pins to LOW, or just use the GND pin like normal people, and make the input pins INPUT_PULLUP instead of INPUT, that will work ok, except that the switch will read HIGH when not pressed and LOW when pressed, which is also ok.

joural

Yeah, all of you are right, my switch connection was the issue, connecting b/w pin and gnd solved my issue

Go Up