Button counter affected by if/while/switch
hi, i'm bit new arduino , encountering problem codes.
first of all, using arduino control strip of addressable ws2812b led strip. had set-up button potentially switch modes patter light led. also, use potentiometer control patter.
this first code set-up works fine
now problem whenever insert a/an if/while/switch statement following code using but1counter, whenever turn potentiometer, affects value of but1counter.
can me identify why happening?
tia
first of all, using arduino control strip of addressable ws2812b led strip. had set-up button potentially switch modes patter light led. also, use potentiometer control patter.
this first code set-up works fine
quote
// following initializationsit doesn't switch modes yet. can see, value of but1counter cycles values {0,1,2,3} , when turn potentiometer, pattern of lights follows expected. 2 values independent , doesn't affect each other whatever do.
#include <fastled.h>
#define num_leds 5
#define data_pin 5
crgb leds[num_leds];
// these initializations of button select mode
const int but1 = 13;
int but1counter = 0;
int but1state = 0;
int but1laststate = 0;
// set-up
void setup() {
fastled.addleds<ws2812b, data_pin, grb>(leds, num_leds);
fastled.clear();
float delayamount = 1000*(1.5/num_leds);
// loop starts led lighting each 1 @ time color white clears short while before proceeding main program
for(int = 0; < num_leds; i++){
leds.setrgb(100,100,100);
delay(delayamount);
fastled.show();
}
delay(500);
fastled.clear();
for(int = 0; < num_leds; i++){
leds.setrgb(0,0,0);
fastled.show();
}
delay(500);
// setting-up button input , serial monitor
pinmode(but1,input);
digitalwrite(but1,high);
serial.begin(9600);
}
void loop() {
togglepattern(); // calls button potentially switch modes, in case showing separate functions
lightwaves(); // calls pattern light leds
}
//the mode switch function
void togglepattern(){
but1state = digitalread(but1);
if(but1state != but1laststate){
if(but1state == high){
but1counter++;
but1counter = but1counter%4;
}
}
but1laststate = but1state;
serial.println(but1counter);
}
//the led pattern function
void lightwaves(){
int val = analogread(0);
int point = map(val, 0, 1023, 0, num_leds - 1);
// first, clear existing led values
fastled.clear();
int = 0;
int pnt = 0;
while( < num_leds ) {
int point = map(i, 0, num_leds, 0, 1520);
leds.setrgb(redcounter(point),greencounter(point),bluecounter(point)); //the 3 color functions called written below
i++;
}
for(int = 0; < point; i++) {
for(int j = 0; j < num_leds - i; j++) {
leds[num_leds - j] = leds[num_leds - j - 1];
}
}
fastled.show();
}
//the 3 color functions
int redcounter(int counter)
{
int red = 0;
int green = 0;
int blue = 0;
if (counter <= 255)
{
red = 255;
green = counter;
blue = 0;
}
else if (counter > 255 && counter <=510)
{
red = 255 - counter;
green = 255;
blue = 0;
}
else if (counter > 510 && counter <= 765)
{
red = 0;
green = 255;
blue = counter - 510;
}
else if (counter > 765 && counter <= 1020)
{
red = 0;
green = 1020 - counter;
blue = 255;
}
else if (counter > 1020 && counter <= 1275)
{
red = counter - 1020;
green = 0;
blue = 255;
}
else
{
red = 255;
green = 0;
blue = 1520 - counter;
}
return red;
}
int greencounter(int counter)
{
int red = 0;
int green = 0;
int blue = 0;
if (counter <= 255)
{
red = 255;
green = counter;
blue = 0;
}
else if (counter > 255 && counter <=510)
{
red = 255 - counter;
green = 255;
blue = 0;
}
else if (counter > 510 && counter <= 765)
{
red = 0;
green = 255;
blue = counter - 510;
}
else if (counter > 765 && counter <= 1020)
{
red = 0;
green = 1020 - counter;
blue = 255;
}
else if (counter > 1020 && counter <= 1275)
{
red = counter - 1020;
green = 0;
blue = 255;
}
else
{
red = 255;
green = 0;
blue = 1520 - counter;
}
return green;
}
int bluecounter(int counter)
{
int red = 0;
int green = 0;
int blue = 0;
if (counter <= 255)
{
red = 255;
green = counter;
blue = 0;
}
else if (counter > 255 && counter <=510)
{
red = 255 - counter;
green = 255;
blue = 0;
}
else if (counter > 510 && counter <= 765)
{
red = 0;
green = 255;
blue = counter - 510;
}
else if (counter > 765 && counter <= 1020)
{
red = 0;
green = 1020 - counter;
blue = 255;
}
else if (counter > 1020 && counter <= 1275)
{
red = counter - 1020;
green = 0;
blue = 255;
}
else
{
red = 255;
green = 0;
blue = 1520 - counter;
}
return blue;
}
now problem whenever insert a/an if/while/switch statement following code using but1counter, whenever turn potentiometer, affects value of but1counter.
can me identify why happening?
tia
please use code tags (</> button on toolbar), not quote tags, when post code or warning/error messages. reason forum software can interpret parts of code markup, leading confusion, wasted time, , reduced chance problem. make easier read code , copy ide or editor. using code tags , other important information explained in how use forum post. please read it.
now problem whenever insert a/an if/while/switch statement following code using but1counter, whenever turn potentiometer, affects value of but1counter.i guess forgot post "following code".
Arduino Forum > Using Arduino > Programming Questions > Button counter affected by if/while/switch
arduino
Comments
Post a Comment