Neopixel and TFT?

you are not so Grumpy

Mike's grumpiness is proportional to the stupidity/laziness of the newbie. So you are not doing too badly!

PaulRB:
Mike's grumpiness is proportional to the stupidity/laziness of the newbie!

:slight_smile:
True.

Do not confuse stupidity with not knowing.

Ignorance is the prerequisite of learning as my old supervisor used to say.

Grumpy Mike,

Using two buttons if I press Button one (B1) it should go to Red. While Red is "on" And Button two (B2) is pressed it should run that pattern and show Green. If B1 is pressed again, it should turn Red.

So option a, was what I had in mind. But that seems like it might be impossible building on what I have so far?

What do you think?

PaulRB and Grumpy, I try not to be Lazy when doing things and although, I know I am not stupid, programming makes me feel that way sometimes...Haha more often than not. :slight_smile:

So option a, was what I had in mind. But that seems like it might be impossible building on what I have so far?

No perfectly possible. But you have to define what you want to do before you can implement it.

This is what you had in reply #25 isn't it?

Had to step out for a bit. Yep, I think if I can get two buttons from the #25 sketch working, I shouldn't have too much trouble making it work with four.

~Dennis

Grumpy_Mike:
No perfectly possible. But you have to define what you want to do before you can implement it.

This is what you had in reply #25 isn't it?

Haha, good one there!

That is exactly what I have in reply #25... I need to think about how I want them to turn off!

What I had envisioned was if a pattern was running for a certain button, pressing that button again would turn it off.

For example:

in the beginning it is dark...no lights are lit.
pressing B1 would make the LED's Red
Pressing it again would turn them off
pressing B2 would make the LED's Green
Pressing it again would turn them off

However, I also wanted it to work this way:
in the beginning it is dark...no lights are lit.
pressing B1 would make the LED's Red
pressing B2 would make the LED's Green
Pressing B2 again would turn them off

Is this even possible?

~Dennis

What I had envisioned was if a pattern was running for a certain button, pressing that button again would turn it off.

OK so that should be in your specification.

Is this even possible?

I would think so.

In the code that sets the pattern variable to a specific number, before you set it look to see what value the pattern variable is already. If it is at the same value as you are going to set the pattern number to then set the pattern number to your blank pattern. A simple if ... else structure should do this.

Code is very specific. There is an old joke, a programmer is asked by his wife "go to the shops and get a pint of milk, and if they have eggs get six"
The programmer arrived home with six pints of milk, his wife says "why have you got six pints of milk", to which he replied "because they had eggs"

I will try this asap, thanks. :slight_smile:

Haha good joke, apt but funny!

~Dennis

Grumpy_Mike:
In the code that sets the pattern variable to a specific number, before you set it look to see what value the pattern variable is already. If it is at the same value as you are going to set the pattern number to then set the pattern number to your blank pattern. A simple if ... else structure should do this.

Thank you very, very, much, It works exactly the way I want it to now! I used:

if (pattern == 1) {
      pattern = 0;
      }
      else {
        pattern = 1 ;
      }

and

if (pattern == 2) {
      pattern = 0;
      }
      else {
        pattern = 2 ;
      }

This makes it work perfectly! I am very grateful, have some more karma. :slight_smile:
I will post the working sketch for other people with this or a similar problem to use as soon as I can.

~Dennis

1st part of project (4 TFT buttons and Neopixel) is solved but 2nd part (integration of a slider) is still Ongoing
The 1st part is in an attachment on Post #0.

I am not sure the etiquette here for this but I hope its ok not to start a new thread for a continuation of this project.

I thought I had learned enough to integrate a color slider into the 4 button sketch but I am having some difficulty. :frowning: I am trying to get the buttons working the same as before but include a color slider in the sketch and have it set the Neopixel color. I want it to work independently as the buttons do and have the Neopixel turn off when the red, green, and blue sliders are at the top and full white when at the bottom, as well as each color working independently... I have the slider set up and it works perfectly when the button code is commented out and vice versa. I am having a hard time integrating it. Any help would be appreciated as expected.

Also the code is rather big and exceeds the 9000 character limit. I will attach the .ino but if it is preferred I can also split it up into two (2) parts in it is desired.

ButtonSlider.ino (15.2 KB)

What you need to do is to write your slider code in the same way as the pattern code, then treat it just like a pattern.

This means the code should look at the sliders and set the colours just once and then return.

I will try that today, thanks again.

~Dennis

I have got the slider to work as intended but it messes up the way the buttons work. I like the way that the slider when set to a particular color stays on after a button pattern is turned on then off again.

  1. Everything Off
  2. Sliders set to Red (or any color)
  3. NEO set to Red (or any color)
  4. Button 1 is pressed
  5. Runs the pattern for button one (rainbow():wink:
  6. Button 2 is pressed
  7. Pattern for #1 is turned off
  8. Sliders and NEO is still set to red

However, the buttons have to be turned off before running another pattern which is contrary to what I had. I want it to do the following;

  1. Everything Off
  2. Sliders set to Red (or any color)
  3. NEO set to Red (or any color)
  4. Button 1 is pressed
  5. Runs the pattern for button one (rainbow():wink:
  6. Button 2 is pressed
  7. Pattern for #2 runs (rainbowCycle():wink:
  8. Button 2 is pressed again
  9. Turns off pattern for #2 (rainbowCycle():wink:
  10. Sliders and NEO are still set to red

The code is attached.

~Dennis

ButtonNEOSlider.ino (16.2 KB)

Your code is getting difficult to follow and it still has errors in it.
For example

boolean touched1 = false;
..............
  int reading1 = touched1;
................
touched1 = !touched1;
.................

So what is going on here then, it doesn't make sense.

However, the buttons have to be turned off before running another pattern

That means your code for reading if a button is touched or not is wrong. Could be something with the above. Also you don't seem to always record the last button state.
Why have inverted the touched5 every time the colour wipe does a step?

There is a lot of lines commented out and this is making the code hard to read.

I can't seem to find the bit that actually reads the touch sensors and changes the reading or touched variables.

Grumpy_Mike:
Your code is getting difficult to follow and it still has errors in it.
For example

boolean touched1 = false;

..............
 int reading1 = touched1;
................
touched1 = !touched1;
.................




So what is going on here then, it doesn't make sense.
That means your code for reading if a button is touched or not is wrong. Could be something with the above. Also you don't seem to always record the last button state.
Why have inverted the touched5 every time the colour wipe does a step?

There is a lot of lines commented out and this is making the code hard to read.

I can't seem to find the bit that actually reads the touch sensors and changes the reading or touched variables.

I have cleaned up the code, it should be easier to read now. :slight_smile: Also removed the pattern functions that are not used and did not record the states, they were just for testing. :slight_smile:

Using URTouch, a button is shown as reading the x and y coordinates, if there is data available and if its between the set parameters it is a button touch. Below is the example of button 1. I am using Boolean touched1 = false; to simulate a button press, either on or off. int reading1 = touched1; reads the touched1 as a button press. If the screen is touched between x 112 and x 213 and y 103 and y 204 (inside the box made for button 1) it flips the Boolean and makes it true.
At least this is how I understand it :slight_smile: But please correct me if I am doing this all wrong.

The short answer to your concern to the reason that I am flipping the touched5 for the sliders is...it is the only way I could get it to work close to the way that I want it. :slight_smile:

void buttonPone() {                 // Top Left LED P1
  if (myTouch.dataAvailable()) {
    myTouch.read();
    x = myTouch.getX();
    y = myTouch.getY();
    if ((y >= 103) && (y <= 204))    // Upper row
    {
      if ((x >= 112) && (x <= 213))
      {
        touched1 = !touched1;
      }
    }
  }
}

~Dennis

ButtonNEOSlider.ino (11.1 KB)

The short answer to your concern to the reason that I am flipping the touched5 for the sliders is...it is the only way I could get it to work close to the way that I want it.

But throwing in something that does not make sense is never a good idea even though it might seem to make it a bit more stable, it is never going to actually work. So remove that touched5 = !touched5 as it might be obscuring your real problem.

Thanks for the clean up, you could do with updating the comments as well. At the start of the loop those calls could do with being labeled something like "reading the current state of the buttons". I would drop the call to colorSlider at this stage an insert it into the case 5 block before you call the colour wipe function. The same goes for the three lines that work out the mapping of these sliders. This is so that you only do these functions when you need to do them and not every time, all the time.

Next loose these lines

int reading1 = touched1;
  int reading2 = touched2;
  int reading3 = touched3;
  int reading4 = touched4;
  int reading5 = touched5;

I can't see what they are doing for you. They are of type int being set to a variable of type boolean, a very silly and confusing thing to do. Some languages stop you from doing this sort of thing but C assumes you know what you are doing and unfortunately in this case you don't. The "reading" variables are, as far as I can see, only used in if statements as if they were type boolean, so your use of an int here is wrong. The same goes for the "lastReading" variables they should be static boolean not integers.

Now do that clean up and see what happens. If as might be likely it is not behaving like you want it is time to use some debug printing. This is printing out variables at certain points in the program to see if their value matches your expectations. Do this one at a time until you find something not right and then you can drill down and concentrate on why it is not right.

This will show you what I was thinking.
This is part of your code that uses a button to scroll through patterns of which the above code is based on:

void loop() {
  static int pattern = 0, lastReading;
  int reading = digitalRead(button);
  if(lastReading == HIGH && reading == LOW){
    pattern++ ; // change pattern number
    if(pattern > 3) pattern = 0; // wrap round if too big
    patternInterval = intervals[pattern]; // set speed for this pattern
    wipe(); // clear out the buffer 
    delay(50); // debounce delay
  }
  lastReading = reading; // save for next time

if(millis() - lastUpdate > patternInterval) updatePattern(pattern);
}

note the 1st line taken from your code above

 int reading = digitalRead(button); 
I replaced it with 
int reading = touched1; 
it works

it is the only way that I know how to simulate the HIGH and LOW of a digital button press using the above code.
When there is a reading of a touch in the button1 box it flips the Boolean using touched1 = !touched1;
This led me to believe I could do that with 4 buttons and reworked the code to:

if (lastReading1 == true && reading1 == false) {  // Button P1
    if (pattern == 1) {
      pattern = 0;
    }
    else {
      pattern = 1 ;
    }
    patternInterval = intervals[pattern]; // set speed for this pattern
    delay(100);
  }
  lastReading1 = reading1; // save for next time

If I get rid of the int reading1 = touched1; code, the buttons no longer work. I am having a lot of trouble understanding how to make the touch buttons work in a state machine. :frowning:

If I get rid of the int reading1 = touched1; code, the buttons no longer work.

And have you changed the tests in the if statement from the reading variables to the touched variables. If you have not then it is no wonder why it will not function.