vendredi 15 mars 2019

Having trouble using 2 potentiometers to determine settings on Arduino

Ok. So long story short I am working on a relatively simple project: a machine that blinks a led at specific intervals varying depending on what setting it is on, which will also show which setting is selected on a 4 digit 7 segment LED display. I eventually plan to incorporate a start/stop button, as well.

Currently the circuit consists of 2x 10k pots, a the 4 digit display, and an LED connected to an arduino, everything hardware wise seems to be working fine. 1 pot is to select mode, 1 pot is to select which cycle to run in said mode.

However, only ONE of my three modes is working, all cycles within the code (a steady consistent blink at a specified hertz rate) work as intended. The other two modes, which contain 3 individual cycles each do not function at all- the LED display goes blank or shows 2 random lights lit, and the LED stops blinking. I am assuming it is due to how I wrote the code/intervals or counter - what did I do wrong/how can I write it so that it makes sense? Code is as follows:

int sensorPin = A0;
int ledPin = 1;
float sensorValue = 0;
float sensorValue2 = 0;
float hertz = 0;
int sensorPin3 = A2;
int sensorValue3 = 0;
int mode = 0;
int shortCycle = 0;
int longCycle = 0;
float hertz2 = 0;
int count = 0;


int pMin = 0;
int pMax = 1023;

int ledState = LOW;
unsigned long previousMillis = 0;
//store when led was updated

long interval = 1000;

#include "SevSeg.h"

SevSeg sevseg; //Initiate a seven segment controller object

void setup() {

byte numDigits = 4; 
 byte digitPins[] = {13, 12, 11, 10};
 byte segmentPins[] = {2, 3, 4, 5, 6, 7, 8, 9};

sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins);

sevseg.setBrightness(90);

pinMode(ledPin, OUTPUT);
pinMode(sensorPin, INPUT);
pinMode(sensorPin3, INPUT);

}

void loop() {

sensorValue3 = analogRead(sensorPin3);

mode = map(sensorValue3, pMin, pMax, 1, 3);

if (mode == 1) {

sensorValue = analogRead(sensorPin);

hertz = map(sensorValue, pMin, pMax, 18, 0);

sensorValue2 = ( 1000 / hertz );

if (hertz == 1) {sevseg.setChars(" 1 h");}
else if (hertz == 2) {sevseg.setChars(" 2 h");}
else if (hertz == 3) {sevseg.setChars(" 3 h");}
else if (hertz == 4) {sevseg.setChars(" 4 h");}
else if (hertz == 5) {sevseg.setChars(" 5 H");}
else if (hertz == 6) {sevseg.setChars(" 6 H");}
else if (hertz == 7) {sevseg.setChars(" 7 H");}
else if (hertz == 8) {sevseg.setChars(" 8 H");}
else if (hertz == 9) {sevseg.setChars(" 9 H");}
else if (hertz == 10) {sevseg.setChars("10 H");}
else if (hertz == 11) {sevseg.setChars("11 H");}
else if (hertz == 12) {sevseg.setChars("12 H");}
else if (hertz == 13) {sevseg.setChars("13 H");}
else if (hertz == 14) {sevseg.setChars("14 H");}
else if (hertz == 15) {sevseg.setChars("15 H");}
else if (hertz == 16) {sevseg.setChars("16 H");}
else if (hertz == 17) {sevseg.setChars("17 H");}
else if (hertz == 18) {sevseg.setChars("18 H");}

sevseg.refreshDisplay(); // Must run repeatedly


} else if(mode == 2) {
  //put short cycles here

  sensorValue = analogRead(sensorPin);
  shortCycle = map(sensorValue, pMin, pMax, 0, 4);
  sensorValue2 = 1000 / hertz2;

  if(sensorValue == 1) {sevseg.setChars("a 1");
    if(count >= 0 and count< 3900){hertz2 == 13;} //5 min
    if(count >= 3900 and count< 4620){hertz2 == 12;} //1 min
    if(count >= 4620 and count < 5280){hertz2 == 11;} //1 min
    if(count >= 5280 and count< 6480){hertz2 == 10;} //2 min
    if(count >= 6480 and count< 7020){hertz2 == 9;} //1 min
    if(count >= 7020){hertz2 == 10;} //5 min = 10020 count

      //10 minutes + end (~15 minute session)

      sevseg.refreshDisplay(); // Must run repeatedly

  
  }
  
  else if(sensorValue == 2){sevseg.setChars("th 1");
    if(count >= 0 and count< 3900){hertz2 == 13;} //5 min
    if(count >= 3900 and count< 4620){hertz2 == 12;} //1 min
    if(count >= 4620 and count< 5280){hertz2 == 11;} //1 min
    if(count >= 5280 and count< 6480){hertz2 == 10;} //2 min
    if(count >= 6480 and count< 7020){hertz2 == 9;}//1 min
    if(count >= 7020 and count < 7500){hertz2 == 8;} //1 min
    if(count >= 7500 and count< 7920){hertz2 == 7;} //1 min
    if(count >= 7920 and count< 8280){hertz2 == 6;} //1 min
    if(count >= 8280 and count < 8880){hertz2 == 5;} //2 min
    if(count >= 8880 and count< 9120){hertz2 == 4;} // 1 min
    if(count >= 9120){hertz2 == 5;} // 5 min = 10620

      //16 minutes + end (~21 minute session)

  }

  else if(sensorValue == 3){sevseg.setChars("d 1");
    if(count >= 0 and count< 3900){hertz2 == 13;}; //5 min
    if(count >= 3900 and count< 4620){hertz2 == 12;} //1 min
    if(count >= 4620 and count< 5280){hertz2 == 11;} //1 min
    if(count >= 5280 and count < 6480){hertz2 == 10;} //2 min
    if(count >= 6480 and count< 7020){hertz2 == 9;} //1 min
    if(count >= 7020 and count< 7500){hertz2 == 8;} //1 min
    if(count >= 7500 and count< 7920){hertz2 == 7;} //1 min
    if(count >= 7920 and count< 8280){hertz2 == 6;} //1 min
    if(count >= 8280 and count< 8880){hertz2 == 5;} //2 min
    if(count >= 8880 and count< 9600){hertz2 == 4;} // 3 min
    if(count >= 9600 and count < 10320){hertz2 == 3;} // 4 min
    if(count >= 10320){hertz2 == 2;} // 5 min = 10920

    sevseg.refreshDisplay(); // Must run repeatedly
      
      //23 min + end (~28 minute session)
  }
  
  
  
} else if(mode == 3) {
  //put long cycles here
  //fill in times as you calculate

  sensorValue = analogRead(sensorPin);
  longCycle = map(sensorValue, pMin, pMax, 0, 4);
  sensorValue2 = 1000 / hertz2;

if(sensorValue == 1){sevseg.setChars("a 2");
    if(count >= 0 and count< 3900){hertz2 == 13;} //5 min
    if(count >= 3900 and count< 4620){hertz2 == 12;} //1 min
    if(count >= 4620 and count< 5280){hertz2 == 11;} //1 min
    if(count >= 5280 and count< 6480){hertz2 == 10;} //2 min
    if(count >= 6480 and count< 7020){hertz2 == 9;} //1 min
    if(count >= 7020 and count< 10020){hertz2 == 10;} //5 min
    if(count >= 10020 and count< 11640){hertz2 == 9;} //3 min
    if(count >= 11640 and count< 13440){hertz2 == 10;} //3 min
    if(count >= 13440 and count< 14760){hertz2 == 11;} //2 min
    if(count >= 14760 and count < 16200){hertz2 == 12;} //2 min
    if(count >= 16200){hertz2 == 13;} //5 min == 20100

    sevseg.refreshDisplay(); // Must run repeatedly

      //25 min + end (~30 min session)

}

else if(sensorValue == 2){sevseg.setChars("t 2");
    if(count >= 0 and count< 3900){hertz2 == 13;} //5 min
    if(count >= 3900 and count< 4620){hertz2 == 12;} //1 min
    if(count >= 4620 and count< 5280){hertz2 == 11;} //1 min
    if(count >= 5280 and count< 6480){hertz2 == 10;} //2 min
    if(count >= 6480 and count < 7020){hertz2 == 9;} //1 min
    if(count >= 7020 and count< 7500){hertz2 == 8;} //1 min
    if(count >= 7500 and count < 7920){hertz2 == 7;} //1 min
    if(count >= 7920 and count< 8280){hertz2 == 6;} //1 min
    if(count >= 8280 and count< 8880){hertz2 == 5;} //2 min
    if(count >= 8880 and count< 9120){hertz2 == 4;} // 1 min
    if(count >= 9120 and count< 10620){hertz2 == 5;} // 5 min
    if(count >= 10620 and count< 11340){hertz2 == 4;} //3 min
    if(count >= 11340 and count < 12240){hertz2 == 5;} //3 min
    if(count >= 12240 and count< 12960){hertz2 == 6;} //2min
    if(count >= 12960 and count< 13800){hertz2 == 7;} //2 min
    if(count >= 13800 and count< 14280){hertz2 == 8;} //1 min
    if(count >= 14280 and count< 14820){hertz2 == 9;}//1 min
    if(count >= 14820 and count< 15420){hertz2 == 10;}//1 min
    if(count >= 15420 and count< 16080){hertz2 == 11;}//1 min
    if(count >= 16080 and count < 16800){hertz2 == 12;} //1 min
    if(count >= 16800 ){hertz2 == 13 ;}// 5 min = 20700

sevseg.refreshDisplay(); // Must run repeatedly

      //36 min + end (~41 min session)

}

else if(sensorValue == 3){sevseg.setChars("d 2");
    if(count >= 0 and count< 3900){hertz2 == 13;} //5 min
    if(count >= 3900 and count< 4620){hertz2 == 12;} //1 min
    if(count >= 4620 and count < 5280){hertz2 == 11;} //1 min
    if(count >= 5280 and count< 6480){hertz2 == 10;} //2 min
    if(count >= 6480 and count< 7020){hertz2 == 9;} //1 min
    if(count >= 7020 and count< 7500){hertz2 == 8;} //1 min
    if(count >= 7500 and  count< 7920){hertz2 == 7;}//1 min
    if(count >= 7920 and count < 8280){hertz2 == 6;} //1 min
    if(count >= 8280 and count < 8880){hertz2 == 5;} //2 min
    if(count >= 8880 and count < 9600){hertz2 == 4;} // 3 min
    if(count >= 9600 and count < 10320){hertz2 == 3;} // 4 min
    if(count >= 10320 and count < 10920){hertz2 == 2;} // 5 min
    if(count >= 10920 and count< 11640){hertz2 == 3;} //4 min
    if(count >= 11640 and count < 12360){hertz2 == 4;} //3 min
    if(count >= 12360 and count< 12960){hertz2 == 5;} //2 min
    if(count >= 12960 and count < 13680){hertz2 == 6;} //2 min
    if(count >= 13680 and count< 14520){hertz2 == 7;} //2 min
    if(count >= 14520 and count < 15000){hertz2 == 8;} //1 min
    if(count >= 15000 and count < 15540){hertz2 == 9;} //1 min
    if(count >= 15540 and count < 16140){hertz2 == 10;} //1 min
    if(count >= 16140 and count < 16800){hertz2 == 11;} //1 min
    if(count >= 16800 and count < 17520){hertz2 == 12;}//1 min
    if(count >= 17520){hertz2 == 13;} // 5 min = 21420

    sevseg.refreshDisplay(); // Must run repeatedly


      //45 min + end (~50 min session)


}
}

unsigned long currentMillis = millis();

if((ledState == LOW) && (currentMillis - previousMillis >= sensorValue2))
  //makes light blink at the begining of each "beat" 
  //as opposed to being on for a beat, off for a beat

  // may remove "/hertz" at the end, if a better solution is found, becomes unusable after 4 hrtz
{
    //saves last time you blinked led

  previousMillis = currentMillis;

       ledState = HIGH;

  count =(count ++);


       
    } else if ((ledState == HIGH) && (currentMillis - previousMillis >= 15))
    
    {
      ledState = LOW;

    }

    digitalWrite(ledPin, ledState);
sensorValue3 = analogRead(sensorPin3);
}

Aucun commentaire:

Enregistrer un commentaire