0

(EDIT) My goal with this code is to use two potentiometers, pot0 and pot1 respectively, to control 8 leds. Pot0 controls which sequence the leds are running (1, 2, or 3) and pot1 controls which LEDs within each sequence are active. Currently, I am having a problem because I am running Sequence 1 (the first if statement) to my arduino, but I can't seem to make the LEDs light up when being called out by the potentiometer. The only LED that works is LED 0, and that's only if both potentiometers were in the proper positions when the code begins running.

int pot0 = 0;
int pot1 = 1;
int led[] = {7, 6, 5, 4, 3, 2, 1, 0};



void setup() {

for (int i = 0; i < led; i++){
  pinMode(led, OUTPUT)
;}
}
void loop() {
  int potread = analogRead(pot0);
  int val = analogRead(pot1);
  int potR = map(val, 0, 1023, 0, 7);

if (potread <= 341) {
  for (int i; i == potR;)
  digitalWrite(led[i], HIGH);

}

else if (potread <= 682) {

}
else {

}
}
4
  • I actually figured out what was causing the error, but I am now having an issue with what the actual code is doing, so I will update my post Apr 21, 2020 at 1:54
  • separate the pot reading and the LED lighting ... instead of if (potread <= 341) { digitalWrite(led, HIGH);} use a flag variable .... do something like this if (potread <= 341) level1 = true; ... further down in the program if ( level1 ) digitalWrite(led, HIGH); ... that way your code will be less convoluted
    – jsotola
    Apr 21, 2020 at 2:18
  • I have updated my post. I would appreciate if you would take another look at it and give me some advice. Apr 21, 2020 at 3:08
  • you have a malformed for loop
    – jsotola
    Apr 21, 2020 at 3:33

2 Answers 2

1

For this, you should be using map(). Instead of int potR = val % 8; try int potR = map(val, [minimum pot value], [maximum pot value], 0, 7). Also, I'm pretty sure you don't need the for loop if all you're doing is led[i]. In that case, you could use led[potR].

4
  • I updated my post, thank you for your help. I fixed the For loop issue, and it will now compile to my board. Please read the post for the new issues Apr 21, 2020 at 2:07
  • This will likely work, but right now I can't really see if it will because I can't get my arduino to stop only lighting leds 6 and 7, and I'm not sure what the issue is. When I run the code, it starts with both of them at a high brightness, and after maybe a second it dims the brightness on LED 6. Is there something in my code that might cause this? Apr 21, 2020 at 2:14
  • Try some simple troubleshooting like doing a for loop through your LED array.
    – KTibow
    Apr 21, 2020 at 2:16
  • I managed to figure it out (My arduino was running an old sketch for some reason). Everything seems to be in order except that my pot1 doesn't seem to be affecting which LEDs are lit up, it's only lighting up LED 0 even if I turn pot1 all the way to max. Apr 21, 2020 at 2:28
1

I spoke to someone the other night while working on this who was able to help me, and have since resolved all of my issues. Thank you to everyone who helped me fix the issues that I was having!

For those interested in how my final code came out ->

int pot0 = 0;
int pot1 = 1;
int led[] = {7, 6, 5, 4, 3, 2, 8, 9};
const int DelayPeriod = 100;


void setup() {
pinMode(pot0, INPUT);
pinMode(pot1, INPUT);
for (int i = 0; i < led; i++){
  pinMode(led, OUTPUT)
;}
}
void loop() {
  int potread = analogRead(pot0);
  int val = analogRead(pot1);
  int potR = map(val, 0, 1023, 0, 7);
if (potread <= 341) {
  for (int i = 0; i <= led; i++){
    if (i == potR){
      digitalWrite(led[i], HIGH);
    }
    else {
      digitalWrite(led[i], LOW);
    }
  }


}
else if (potread <= 682) {
  for (int i = 0; i <= led; i++){
    if (i <= potR){
      digitalWrite(led[i], HIGH);
    }
    else {
      digitalWrite(led[i], LOW);
    }
  }

}
else {
  for (int i = 0; i <= led; i++){
    if ((i <= (potR + 1)) && (i >= (potR - 1))){
      digitalWrite(led[i], HIGH);
    }
    else {
      digitalWrite(led[i], LOW);
    }
  }
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.