Go Down

Topic: I want to add a slider pot to my LED matrix, cant get my head around the logic (Read 80 times) previous topic - next topic

rperez88

Let me preface this by saying my project is working perfectly as of now, just want to add to it and I cant even wrap my head around how to implement this feature I want. If someone could point me in the right logical direction I would appreciate it.

project gif: https://gfycat.com/DisguisedFittingCanadagoose

A generic slider pot: http://www.robotmesh.com/grove-slide-potentiometer?gclid=CjwKEAjwr6ipBRCM7oqrj6O30jUSJACff2WHlAj2aO_wSqOvDlYQs6imAxqMKEUaCwPKvdK-ahsMLxoCW9nw_wcB

There's a million ways to skin a cat, but here's the code and logic I went with for this:

Code: [Select]
#define ledPin1 13
#define ledPin2 2
#define ledPin3 3
#define ledPin4 4
#define ledPin5 5
#define ledPin6 6
#define ledPin7 7
#define ledPin8 8
#define ledPin9 9
#define ledPin10 10
#define ledPin11 11
#define ledPin12 12

int potpin_x = 1;
int potpin_y = 0;


int val_y;
int val_y2;
int val_y3;
int val_x;

void setup()
{

pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(ledPin6, OUTPUT);
pinMode(ledPin7, OUTPUT);
pinMode(ledPin8, OUTPUT);
pinMode(ledPin9, OUTPUT);
pinMode(ledPin10, OUTPUT);
pinMode(ledPin11, OUTPUT);
pinMode(ledPin12, OUTPUT);

Serial.begin(9600);
}

void loop()
{

val_x = analogRead(potpin_x);         
val_x = map(val_x, 200, 800, 1, 3);

val_y = analogRead(potpin_y);         
val_y = map(val_y, 0, 1023, 1, 4);

val_y2 = analogRead(potpin_y);
val_y2 = map(val_y2, 0, 1023, 1, 4);

val_y3 = analogRead(potpin_y);
val_y3 = map(val_y3, 0, 1023, 1, 4); 

delay(30);

if (val_x == 1) {

digitalWrite(ledPin5, HIGH);
digitalWrite(ledPin6, HIGH);
digitalWrite(ledPin7, HIGH);
digitalWrite(ledPin8, HIGH);
digitalWrite(ledPin9, HIGH);
digitalWrite(ledPin10, HIGH);
digitalWrite(ledPin11, HIGH);
digitalWrite(ledPin12, HIGH);

if (val_y == 1){

digitalWrite(ledPin1, LOW);

}  else{
digitalWrite(ledPin1, HIGH);
}                 

if (val_y == 2){
digitalWrite(ledPin2, LOW);
}
else{
digitalWrite(ledPin2, HIGH);
}

if (val_y == 3 ){
digitalWrite(ledPin3, LOW);
}
else{
digitalWrite(ledPin3, HIGH);
}

if (val_y == 4 ){
digitalWrite(ledPin4, LOW);
}
else{
digitalWrite(ledPin4, HIGH);
}
}

if (val_x == 2) {

digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, HIGH);
digitalWrite(ledPin9, HIGH);
digitalWrite(ledPin10, HIGH);
digitalWrite(ledPin11, HIGH);
digitalWrite(ledPin12, HIGH);

if (val_y2 == 1){
digitalWrite(ledPin5, LOW);

}  else{
digitalWrite(ledPin5, HIGH);
}                 

if (val_y2 == 2 ){
digitalWrite(ledPin6, LOW);
}
else{
digitalWrite(ledPin6, HIGH);
}

if (val_y2 == 3 ){
digitalWrite(ledPin7, LOW);
}
else{
digitalWrite(ledPin7, HIGH);
}

if (val_y2 == 4 ){
digitalWrite(ledPin8, LOW);
}
else{
digitalWrite(ledPin8, HIGH);
}
}
if (val_x == 3) {

digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, HIGH);
digitalWrite(ledPin5, HIGH);
digitalWrite(ledPin6, HIGH);
digitalWrite(ledPin7, HIGH);
digitalWrite(ledPin8, HIGH);

if (val_y3 == 1){
digitalWrite(ledPin9, LOW);

}  else{
digitalWrite(ledPin9, HIGH);
}                 

if (val_y3 == 2 ){
digitalWrite(ledPin10, LOW);
}
else{
digitalWrite(ledPin10, HIGH);
}

if (val_y3 == 3 ){
digitalWrite(ledPin11, LOW);
}
else{
digitalWrite(ledPin11, HIGH);
}

if (val_y3 == 4 ){
digitalWrite(ledPin12, LOW);
}
else{
digitalWrite(ledPin12, HIGH);
}
}
Serial.println(val_y);
Serial.print("      ");
Serial.println(val_x);
}


As you can see in the gif, the thumb stick is controlling the off LED and "moving" it around the matrix. Here's what I want:

When the slider pot is all the way to the left (zero) I want my project to work as you see in the gif, but as i move it to the right, I want the "selected moving off LED" to expand, that one off LED becomes that one and the 3 others around it, essentially allowing me to move a square of LEDs around the matrix instead of just one.

thank you for your time!

Robin2

I wonder if it is time to tidy up your code by dividing it up into functions and using arrays insted of line after line digitalWrite()s. Look at how the code is organised in planning and implementing a program.

I'm not saying this just because I like tidy code. By putting the code into self-contained functions it will be much easier to understand the overall logic and to make changes in one part without screwing up something else.

I suggest you approach your slider question in 2 different parts. First of all figure out how to make your code do what you want based on a number hard-wired into a variable.

Second, write a short program that reads the value of the slider and saves it to a variable.

Byt dealing with it as two pieces debugging will be much easier. If you can cause the changes to happen using a variable with a fixed value it should be easy to add in the extra code to change that value using the slider. I you designed the short slider test code as a function you will be able to copy and paste it into your main program.

...R

rperez88

I suggest you approach your slider question in 2 different parts. First of all figure out how to make your code do what you want based on a number hard-wired into a variable.

Can you expand on this a bit please? I can write a function for the slider no problem, but I cant imagine how Id turn my current code into segmented functions.

Thank you for taking the time to respond.

Robin2

but I cant imagine how Id turn my current code into segmented functions.
I was hoping that the example in planning and implementing a program would give you ideas.

What I have in mind is that you have code that makes an LED (or several of them) do something. Put that code in a function so it can be called from loop(). My aim would be to reduce the code in loop() to something like


Code: [Select]
void loop() {
  currentMillis = millis();
  readSlider();
  readThumbStick();
  updateLEDs();
}



The idea is to have a self-contained piece of code (or maybe 2 or 3 pieces, each self-contained) that can be tested and proved on their own.

...R

rperez88

I was hoping that the example in planning and implementing a program would give you ideas.

What I have in mind is that you have code that makes an LED (or several of them) do something. Put that code in a function so it can be called from loop(). My aim would be to reduce the code in loop() to something like


Code: [Select]
void loop() {
  currentMillis = millis();
  readSlider();
  readThumbStick();
  updateLEDs();
}



The idea is to have a self-contained piece of code (or maybe 2 or 3 pieces, each self-contained) that can be tested and proved on their own.

...R
Thanks, I actually do understand this part, but if I did that it would pretty much still be the same code, just condensed into a few functions instead of how it is now.

I'm just having trouble figuring out how the function consolidation would help me implement the off LED as its own variable regardless of its position, and furthermore how I would be able to utilize the LEDs around it with the slider.

Maybe if I make each LED its own variable as well?

Robin2

I'm just having trouble figuring out how the function consolidation would help me implement the off LED as its own variable regardless of its position, and furthermore how I would be able to utilize the LEDs around it with the slider.
Developing code is in large part a process of seeing patterns. Noticing that E is very like 3 and then realizing that it would be easy to turn either into H. Or maybe they could all be simplified to give O.

Having the code organized properly greatly facilitates this process by allowing your mind to see one part to the exclusion of the others.

I don't know (now) how to modify your code to achieve what you want. But with the code you posted I can't see the wood for the trees and consequently no suggestions pop into my mind.

...R

rperez88

thanks for your help! I'll give it a shot and see what I come up with.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy