1

ok so I dont actually know anything about coding or electronics outside of being an electrician. This is something i've been working on for about a month. I may be asking something dumb but here it is.

also i dont know where to put my code so ill put it in the comments (sorry).

so basically I want a visual metronome. I want to plug a control box into the wall, plug a lamp into the control box to blink at a set interval. the box will have a rotary encoder and lcd display to control the beats per minute. to achieve this im gonna use a SSR and some transformers to achieve a variety of voltages for different led lights. but I want to affect a 120v cct to do it all. but I just want to get the code down before I build it so im trying to blink the onboard led on my arduino uno for now.

I have the code, which is 3 different sketches, frankensteined together. probably poorly. I added an arithmetic equation to facilitate the metronome timing but I cant transfer that value to the "interval" for the blinking light.

In my serial viewer, the equation works and it gives me the values I desire. I just cant get that into the interval.

the math equation is simple (60/bpm)1000 = intended interval

example: (60/120)1000 = 500 as the milli interval for the blinking light. 2 blinks per second.

enter code here





// rotary encoder -----------------

int val;
int encoder0PinA = 6;
int encoder0PinB = 7;
int encoder0Pos = 59;
int encoder0PinALast = LOW;
int n = LOW;

// lcd screen ---------------------

     // include the library code:
#include <LiquidCrystal.h>

     // initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


// led blink ----------------------

     // the number of the LED pin
const int ledPin =  LED_BUILTIN;

     // ledState used to set the LED
int ledState = LOW;             

     // will store last time LED was updated
unsigned long previousMillis = 0;      

     // interval at which to blink (milliseconds) --    long interval = ;


// mathssss -----------------------

float a = (60 / encoder0Pos) * 1000;



// on to setup -------------------------------------------------------



void setup() {
      // put your setup code here, to run once:




// rotary encoder ---------------
 pinMode (encoder0PinA, INPUT);
  pinMode (encoder0PinB, INPUT);
  Serial.begin (9600);



  Serial.println ("hi");

// lcd screen -------------------
      // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
      // Print a message to the LCD.
  lcd.print("BeatsPerMinute");

// led blink -------------------
pinMode(ledPin, OUTPUT);

}


// on to loop --------------------------------------------------------
void loop() {
      // put your main code here, to run repeatedly:
      long interval = (60.00 / encoder0Pos) * 1000;

// rotary encoder --------------
  n = digitalRead(encoder0PinA);
  if ((encoder0PinALast == LOW) && (n == HIGH)) {
    if (digitalRead(encoder0PinB) == LOW) {
      encoder0Pos--;
    } else {
      encoder0Pos++;
    }

     // serial ----------------------------------------------------------------------
  Serial.println ("BPM");
  Serial.println (encoder0Pos);
  Serial.println ("interval");
  Serial.println (interval);
  Serial.println ("intended interval");
  Serial.println (60.00 / encoder0Pos * 1000, 8);
  Serial.println ("----------------");
     // serial done -----------------------------------------------------------------
  }
  encoder0PinALast = n;

  // lcd screen ----------------

       // set the cursor to column 0, line 1
       // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
       // print the number of seconds since reset:
  lcd.print(encoder0Pos);

  // led blink -----------------

    unsigned long currentMillis = millis();

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


    digitalWrite(ledPin, ledState);

 // serial --------------------


 // done


  }

}
  • oh no where do i post the code? – tom Oct 27 at 14:42
  • Post your code in your question. Copy and paste it, then select the code and press the {} button to format it. – Majenko Oct 27 at 14:45
  • im clearly not formatting the code correctly in this forum. I copy it, then click the dual squiggly brackets and then paste? – tom Oct 27 at 15:05
  • No. You copy. Then you paste. Then you select (highlight your pasted code). Then you press {}. It should indent all your code 4 spaces. – Majenko Oct 27 at 15:08
  • And why don't you just set the interval varaible with your math formula? Something like interval = <math formula> – chrisl Oct 27 at 15:12
2

As I wrote in my comment, you don't actually set the value of interval to the new value, when the encoder position changes. So the blinking interval does not change. You need to use the formula to set the new value to interval. Insert

interval = 60.00 / encoder0Pos * 1000;

before your Serial.print() statements. Also, you need to remove the const keyword at the definition of interval, or you won't be able to change it's value.


Judging from your comments, you misunderstand how variables work. A variable does not save a formula, just a value. a is defined at global scope in your code (outside of any function). That means, that the definition get's executed at the very start of the program. Since encoder0Pos has the value 59 at that time, the value 1016.95 get's assigned to the variable a. This value only ever changes, if you actively assign a new value to that variable (which you never do). The formula is only executed once during the assignment.

  • ok I think im understanding, ill edit the post to update the code, but if im understanding you correctly, ive already tried this and it still doesnt give the result i want. how do Ihave variable 'a' change with the encoder reading? also, I tried adding the interval = 60.00 / encoder0Pos * 1000; above the serial.println's but I got an error message – tom Oct 27 at 17:09
  • ok I also added the formula above the serial.println section. that has provided me with the correct value being returned from the interval in my serial viewer! thats great! but its not affecting the rate of blink of the led still. I updated the code in the OP. – tom Oct 27 at 17:24
  • ok I got it. thank you very much for your help! the math doesnt stay true into the higher values, past around 200 bpm but it will certainly work for my purposes! – tom Oct 27 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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