hello guys, I'm trying to make the led stay white when a certain sequence is done on the JOYSTICK, but when it was the wrong command he reset the counter, making him need to do the sequence again!
const int r1 = 31;
const int g1 = 32;
const int b1 = 33;
int buttonState = 0;
int countJoy = 0;
int eixo_X = A0; //PINO REFERENTE A LIGAÇÃO DO EIXO X
int eixo_Y = A1; //PINO REFERENTE A LIGAÇÃO DO EIXO Y
const int botao = 30; //PINO REFERENTE A LIGAÇÃO NO PINO KEY (EIXO Z)
unsigned long espera3 = 0;
void setup() {
Serial.begin(9600);
pinMode(r1, OUTPUT);
pinMode(g1, OUTPUT);
pinMode(b1, OUTPUT);
pinMode(botao, OUTPUT);
}
void loop() {
joystickColor();
buttonState = digitalRead(botao);
Serial.print(countJoy);
Serial.println("\n");
}
void joystickColor() {
if (countJoy == 5) {
white1();
}
if ((millis() - espera3) > 500) {
if (buttonState == LOW) // light the LED
{
black1();
espera3 = millis();
}
if (analogRead(eixo_X) <= 50) // light the LED
{
blue1();
if (countJoy == 1) {
countJoy++;
}
if (countJoy == 3) {
countJoy++;
}
}
if (analogRead(eixo_Y) <= 50) // light the LED
{
green1();
if (countJoy == 0){
countJoy++;
}
if (countJoy == 2) {
countJoy++;
}
espera3 = millis();
}
if (analogRead(eixo_X) >= 1000) // light the LED
{
red1();
if (countJoy == 4) {
countJoy++;
}
espera3 = millis();
}
if (analogRead(eixo_Y) >= 1000) // light the LED
{
countJoy = 0;
espera3 = millis();
}
}
}
void red1()
{
digitalWrite(r1, HIGH);
digitalWrite(g1, LOW);
digitalWrite(b1, LOW);
}
void green1()
{
digitalWrite(r1, LOW);
digitalWrite(g1, HIGH);
digitalWrite(b1, LOW);
}
void blue1()
{
digitalWrite(r1, LOW);
digitalWrite(g1, LOW);
digitalWrite(b1, HIGH);
}
void white1()
{
digitalWrite(r1, HIGH);
digitalWrite(g1, HIGH);
digitalWrite(b1, HIGH);
}
void black1()
{
digitalWrite(r1, LOW);
digitalWrite(g1, LOW);
digitalWrite(b1, LOW);
}
i'm not sure i understand how your code works. but when properly indented, it's obvious that it's only reading the joystick when 500 msec has expired. i don't think that's intended
const int r1 = 31;
const int g1 = 32;
const int b1 = 33;
int buttonState = 0;
int countJoy = 0;
int eixo_X = A0;
//PINO REFERENTE A LIGAÇÃO DO EIXO X
int eixo_Y = A1;
//PINO REFERENTE A LIGAÇÃO DO EIXO Y
const int botao = 30;
//PINO REFERENTE A LIGAÇÃO NO PINO KEY (EIXO Z)
unsigned long espera3 = 0;
void setup() {
Serial.begin(9600);
pinMode(r1, OUTPUT);
pinMode(g1, OUTPUT);
pinMode(b1, OUTPUT);
pinMode(botao, OUTPUT);
}
void loop() {
joystickColor();
buttonState = digitalRead(botao);
Serial.print(countJoy);
Serial.println("\n");
}
void joystickColor() {
if (countJoy == 5) {
white1();
}
if ((millis() - espera3) > 500) {
if (buttonState == LOW) // light the LED
{
black1();
espera3 = millis();
}
if (analogRead(eixo_X) <= 50) // light the LED
{
blue1();
if (countJoy == 1) {
countJoy++;
}
if (countJoy == 3) {
countJoy++;
}
}
if (analogRead(eixo_Y) <= 50) // light the LED
{
green1();
if (countJoy == 0){
countJoy++;
}
if (countJoy == 2) {
countJoy++;
}
espera3 = millis();
}
if (analogRead(eixo_X) >= 1000) // light the LED
{
red1();
if (countJoy == 4) {
countJoy++;
}
espera3 = millis();
}
if (analogRead(eixo_Y) >= 1000) // light the LED
{
countJoy = 0;
espera3 = millis();
}
}
}
void red1()
{
digitalWrite(r1, HIGH);
digitalWrite(g1, LOW);
digitalWrite(b1, LOW);
}
void green1()
{
digitalWrite(r1, LOW);
digitalWrite(g1, HIGH);
digitalWrite(b1, LOW);
}
void blue1()
{
digitalWrite(r1, LOW);
digitalWrite(g1, LOW);
digitalWrite(b1, HIGH);
}
void white1()
{
digitalWrite(r1, HIGH);
digitalWrite(g1, HIGH);
digitalWrite(b1, HIGH);
}
void black1()
{
digitalWrite(r1, LOW);
digitalWrite(g1, LOW);
digitalWrite(b1, LOW);
}