Go Down

Topic: Arduino push button/LED/ not in sync (Read 97 times) previous topic - next topic

NVID

Hello. The problem is, every time i push my button down, the Arduino fails, the LEDs aren't doing what they're supposed to do, and if I try to upload the code again, it says that it is not in sync.

Ive used the right COM port and the right board.
If i remove the push button, it works just fine. The sound reactive LED that is.

The LEDs use 5V and there are 60 of them. The resistor is 10k ohm.

Here is a link to my setup:
https://gyazo.com/d6c9144e0fe4df17b35da2562d22bebf

Here is the code:
(Ignore the // because it's in my own language)
Code: [Select]
//Hetta er eitt savn, sum hendan kotan er tilgjørd eftir.
#include <FastLED.h>

//Hvønn pinn LED DATA/DIN pinnurin er koyrdur til í Arduino.
#define LED_PIN 2
//Hvønn pinn ljóð sensor A0 pinnurin er koyrdur til í Arduino.
#define ANALOG_READ A0
//Hvussu nógvar perur eru í LED reimuni.
#define NUM_LEDS 60
//Hvat slag LED reimin er.
#define LED_TYPE WS2812B

//LED skipanin. Hetta er í fylgi FastLED savninum.
CRGB leds[NUM_LEDS];

//Hvønn pinn knótturin er koyrdur til í Arduino.
int switchPin = 3;
//Virði á støðuni hjá knóttinum.
int val;
//Støðan hjá knóttinum.
int buttonState;
//Hvussu ofta tað er trýst á knøttin.
int buttonPresses = 0;

//Kotan, sum verur koyrd ígjøgnum einaferð.
void setup()
{
  //Hetta ger so upplýsingar koma frá knóttinum, og til Arduino.
  pinMode(switchPin, INPUT);
  //Her lesa vit støðuna hjá knøttinum av.
  buttonState = digitalRead(switchPin);
  //Hendan kodan er um man vil avlesa hvussu høgt knøtturin er komin upp í, og/ella virði frá ljóð sensorinum.
  //Vit siga 9600 bits per sekund/baud, tí tað er vanligt, og ríkiligt fyri tey flestu.  
  Serial.begin(9600);

  //Hetta tekur #define's omanfyri, brúkar FastLED savni, koyrir tað saman og inní Arduino.
  //Hetta er tað, sum ger at LED reimin at lýsa, og sum eisini ger at honn kann innstillast.
  FastLED.addLeds<LED_TYPE, LED_PIN>(leds, NUM_LEDS);
  //Hetta petti rakar øll ljósini í LED reimuni, og verur ofta brúkt.
  for (int i = 0; i < NUM_LEDS; i++)
  {
    //Í hesum førum raka vit øll LED ljósini, og skifta lit.
    leds[i] = CRGB(0, 255, 0);
  }
  //Her er eitt delay, sum syrgir fyri at kotan higartil er stabil.
  delay(1000);
}

//Kotan, sum verur koyrd ígjøgnum ummaftur, og ummaftur.
void loop()
{
  //Her verur sagt at val skal vera virði, sum verur lisið av.
  val = digitalRead(switchPin);
  //Um val IKKI er tað sama, sum buttonState, so hendir kotan niðanfyri.
  if ( val != buttonState)
  {
    //Um val er LOW, so hendir kotan niðanfyri.
    if (val == LOW)
    {
      //Hetta økir talið eftir hvussu ofta man hevur trýst á knøttin.
      buttonPresses++;
      
      //Um man vil síggja hvussu ofta man hevur trýst á knøttin, kann man brúka kotuna niðanfyri.
      Serial.print(buttonPresses);
    }
  }
  //Her verur virði á knøttinum goymt, so tað kann brúkast ummaftur.
  buttonState = val;

  //Frá linju 72 til og við linju 145 er ein rúgva av if kotu. uppfangar hvussu ofta man hevur trýst á knøttin.
  //Alt eftir hvussu ofta man hevur trýst, skifti tað lit eftir egnum ynski.
  if (buttonPresses == 0)
  {
    for (int i = 0; i < NUM_LEDS; i++)
    leds[i] = CRGB(0, 255, 0);  
  }
  if (buttonPresses == 1)
  {
    for (int i = 0; i < NUM_LEDS; i++)
    leds[i] = CRGB(128, 255, 0);  
  }
  if (buttonPresses == 2)
  {
    for (int i = 0; i < NUM_LEDS; i++)
    leds[i] = CRGB(255, 255, 0);  
  }
  if (buttonPresses == 3)
  {
    for (int i = 0; i < NUM_LEDS; i++)
    leds[i] = CRGB(255, 128, 0);  
  }
  if (buttonPresses == 4)
  {
    for (int i = 0; i < NUM_LEDS; i++)
    leds[i] = CRGB(255, 0, 0);  
  }
  if (buttonPresses == 5)
  {
    for (int i = 0; i < NUM_LEDS; i++)
    leds[i] = CRGB(255, 0, 128);  
  }
  if (buttonPresses == 6)
  {
    for (int i = 0; i < NUM_LEDS; i++)
    leds[i] = CRGB(255, 0, 255);  
  }
  if (buttonPresses == 7)
  {
    for (int i = 0; i < NUM_LEDS; i++)
    leds[i] = CRGB(128, 0, 255);  
  }
  if (buttonPresses == 8)
  {
    for (int i = 0; i < NUM_LEDS; i++)
    leds[i] = CRGB(0, 0, 255);  
  }
  if (buttonPresses == 9)
  {
    for (int i = 0; i < NUM_LEDS; i++)
    leds[i] = CRGB(0, 128, 255);  
  }
  if (buttonPresses == 10)
  {
    for (int i = 0; i < NUM_LEDS; i++)
    leds[i] = CRGB(0, 255, 255);  
  }
  if (buttonPresses == 11)
  {
    for (int i = 0; i < NUM_LEDS; i++)
    leds[i] = CRGB(0, 255, 128);  
  }
  if (buttonPresses == 12)
  {
    for (int i = 0; i < NUM_LEDS; i++)
    leds[i] = CRGB(255, 255, 255);  
  }
  if (buttonPresses == 13)
  {
    for (int i = 0; i < NUM_LEDS; i++)
    leds[i] = CRGB(0, 0, 0);  
  }
  if (buttonPresses == 14)
  {
    buttonPresses = 0;
  }

  //Her seta vit eitt virði til analog pinnin í Arduino, sum kemur frá ljóð sensorinum.
  int sensor_value = analogRead(ANALOG_READ);

  //Frá linju 152 til og við linju 215 er ein rúgva av if kotu. Alt eftir hvussu sterkt siginalið frá ljóð sensorinum er, so
  //vera perurnar í LED reimini dempaðar so nógv. Hetta kann broytast til at vera meira/minni sensitivt.
  if (sensor_value <= 650)
  {
    FastLED.setBrightness(254);
  }
  if ((sensor_value >= 650) && (sensor_value <= 675))
  {
    FastLED.setBrightness(236);
  }
  if ((sensor_value >= 675) && (sensor_value <= 700))
  {
    FastLED.setBrightness(218);
  }
  if ((sensor_value >= 700) && (sensor_value <= 725))
  {
    FastLED.setBrightness(200);
  }
  if ((sensor_value >= 725) && (sensor_value <= 750))
  {
    FastLED.setBrightness(182);
  }
  if ((sensor_value >= 750) && (sensor_value <= 775))
  {
    FastLED.setBrightness(164);
  }
  if ((sensor_value >= 775) && (sensor_value <= 800))
  {
    FastLED.setBrightness(146);
  }
  if ((sensor_value >= 800) && (sensor_value <= 825))
  {
    FastLED.setBrightness(128);
  }
  if ((sensor_value >= 825) && (sensor_value <= 850))
  {
    FastLED.setBrightness(110);
  }
  if ((sensor_value >= 850) && (sensor_value <= 875))
  {
    FastLED.setBrightness(92);
  }
  if ((sensor_value >= 875) && (sensor_value <= 900))
  {
    FastLED.setBrightness(74);
  }
  if ((sensor_value >= 900) && (sensor_value <= 925))
  {
    FastLED.setBrightness(56);
  }
  if ((sensor_value >= 925) && (sensor_value <= 950))
  {
    FastLED.setBrightness(38);
  }
  if ((sensor_value >= 950) && (sensor_value <= 975))
  {
    FastLED.setBrightness(20);
  }
  if ((sensor_value >= 975) && (sensor_value <= 1000))
  {
    FastLED.setBrightness(2);
  }
  if (sensor_value >= 1000)
  {
    FastLED.setBrightness(0);
  }

  //tá ið allar instillingar eru gjørdar, er tað vigtugt at LED reimin skal vera frá, og vísa LED perurnar.
  FastLED.show();

  //Kotan niðanfyri er til um man vil síggja virði, sum kemur frá ljóð sensorinum.
  //Hetta er gott at síggja um man vil instilla hann eftir tørvi.
  Serial.println(sensor_value);

  //Her er eitt delay fyri stabilitet.
  delay(1);
}


I know the code can be optimized, but it was working just fine before. It isn't too slow, it doesn't matter I think.
After trying to upload again, I get the not in sync error. I also tried different Arduinos, different ports, and diffirent USB cables.

Also, the red ON light is working at all times. The green L light is on when i power up the Arduino. But as soon the LED goes to red (the startup color) the light goes rouge. If i manage somehow to change the LED to a different color, maybe blue, the green L light is dimmed, or fully on.

Any help is appreciated.

ballscrewbob

Ok thats way more LED's than the board can handle.



Consider a seperate power supply for the LED's with a common ground to the Arduino.

Bob.
It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

NVID

Are you sure? Seemd to work fine 2 days ago

ballscrewbob

#3
May 09, 2019, 07:50 pm Last Edit: May 09, 2019, 07:55 pm by ballscrewbob
Quite sure !

The regulator on the board is only capable of so much and you are more that likely past that limit.



60 times 60 mA =3,600 mA
Now look at the limits for the regulator if you need further proof.

There are plenty of other post about similar issues if you want to take a little time to look up issues involving multiple LED's and power issues.

It may be you have simply weakened the regulator circuitry to the point it might fail.

Look up addressable LED circuits and notice all the GOOD schematics show two power rails.

Many also mention the limits as well as the reason behind the need for two supply rails.

Bob.
It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

ballscrewbob

Additionally.

Input Voltage Limits:

Recommended: 7~12V

Absolute: 6~20V (to ensure longer board life never go to the max level but stay in the 7-10 volt range)

Input/Output (I/O) pins: -0.5V to +5.5V (the actual max is "Vcc + 0.5V," which for a 5V Arduino, is +5.5V)

Output Current Limits:
When powered by USB: total of 500mA

With external battery or power supply: total of 500mA~1A (as with voltage try stay a little below max although it will generally only draw what is required)

5V pin: same as above: 500mA or 500mA~1A (see above)

Each input/output pin: 40mA (use amplifiers or buffers of some type if you need more)

Sum of all input/output pins combined (but NOT including the "5V" pin): 200mA.

Bob.
It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

NVID

Thank you. I'll try hooking it up with a different power supply that can hold the limits.

Go Up