Pages: [1]   Go Down
Author Topic: Quick Question about updating a variable in 'void loop()'  (Read 392 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is my code for a sequencer (for a musical instrument) based on an ATTiny85 and a 4051 multiplexer. I've set up the code for adjustable speed via an analog pin, but I was wondering if the variable will update properly as written. I'm unsure about line 110, in my loop - do I need to check the variable here?
Quote
/*4051 SELECT PINS*/
int S0 = 0;
int S1 = 0;
int S2 = 0;

/*DEFAULTS*/
int COUNT = 0; // Start sequence counter at 0
int M_ =  0;   // Start default mode at 0 (sequence, 8 steps)
int STEP =  7; // Step count (0-7, 8 steps)
int SPACE = map(analogRead(3), 0, 256, 0, 1024); /* Delay time between steps (mapped from approx. 0-1s)
                                          ^^^^Change this value to increase/decrease maximum delay time*/

/*MODE BUTTON CHECKS*/
boolean HELD = false; // Ignores button hold if < 2s, activates if button hold is > 2s - defaults to false
int T_HOLD = 2000;    // Button hold test (> 2s to activate)
int debounce = 20;    // Debounce time of 20mS
int BTN = 0;          // Button state variable
int LAST = 0;         // Checks previous button press
long T_DN;            // Button down time
long T_UP;            // Button up time

void setup(){
  // initialize all I/O pins
  pinMode(0, OUTPUT); // S0 pin
  pinMode(1, OUTPUT); // S1 pin
  pinMode(2, OUTPUT); // S2 pin
  pinMode(3, INPUT);  // "RATE" - Linear pot (20kΩ), adjusts sequence speed
  pinMode(4, INPUT);  // "MODE" - Pushbutton PRESS selects Seq/Rand, & HOLD selects 4/6/8 steps
}

void Mode_Select(){
  BTN = digitalRead(4);
  // Check for button press & store T_DN value
  if (BTN == LOW && LAST == HIGH && (millis() - T_UP) > debounce){
    T_DN = millis();
  }
  // Check for button press & store T_UP value
  if (BTN == HIGH && LAST == LOW && (millis() - T_DN) > debounce){
    if (HELD == false){
      M_ = M_ ++;   // Toggles between M_ = 0 and M_ = 1
      if (M_ > 1){
        M_ = 0;
      }
      Seq_Type();
    }
    else HELD = false;
    T_UP = millis();
  }
  // Check for button hold > 2s
  if (BTN == LOW && (millis() - T_DN) > T_HOLD){
    Step_Count();
    HELD = true;
    T_DN = millis();
  }
  LAST = BTN;
}

void Seq_Type(){  // Selects between Sequenced and Random modes
  switch (M_){
   
    // Sequenced mode - counts steps 1-8, 1-6, or 1-4 consecutively
  case 0:
    for (COUNT = 0; COUNT <= STEP; COUNT ++ ){
      delay(SPACE);
      // select the bit
      S0 = bitRead(COUNT,0);   
      S1 = bitRead(COUNT,1);
      S2 = bitRead(COUNT,2);   

      digitalWrite(0, S0);
      digitalWrite(1, S1);
      digitalWrite(2, S2);
    }
    break;
    
    // Random mode - selects a random ouput between 1-8, 1-6, or 1-4
  case 1:
    int RANDY;
    RANDY = random(STEP + 1);
    delay(SPACE);
    // select the bit
    S0 = bitRead(RANDY,0);   
    S1 = bitRead(RANDY,1);
    S2 = bitRead(RANDY,2);   

    digitalWrite(0, S0);
    digitalWrite(1, S1);
    digitalWrite(2, S2);
    break;
  }
}

void Step_Count(){  // Selects 8, 6, or 4 steps
  STEP = STEP - 2;
  delay(250);
  if (STEP < 3){
    STEP = 7;
  }
}

void loop() {
  SPACE = SPACE; // This may be unnecessary ??? Check and store SPACE value from A3 ???
  Mode_Select(); // Just keep swimming...
}

Do I need to check the SPACE variable in the loop here, or is it updated already by virtue of me calling the Mode_Select() function in each iteration?
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16523
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to put your code into a # window not a quote window as the formatting will mangle some of the code.

Lefty
Logged

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 15
Posts: 348
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int SPACE = map(analogRead(3), 0, 256, 0, 1024); /* Delay time between steps (mapped from approx. 0-1s)

This declaration only declares a variable named SPACE and assigns it the value of the mapped read on the right of the = sign. Before setup runs this is likely no good. To update it this reading has to happen frequently like once a loop.

Code:
//So instead of
SPACE = SPACE;

//  in loop() use

SPACE = map(analogRead(3), 0, 256, 0, 1024);
Or you could make SPACE a function.
Code:
int SPACE(void) {
     return map(analogRead(3), 0, 256, 0, 1024);
}

Then replace every reference to SPACE with the call to the function SPACE() and get a fresh reading each time. No need to read in loop() this way.

From my experience with Arduino I'd just read it in loop() and use the variable approach. The way it appears you thought it should work is more like the function.
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This line

SPACE = SPACE;

is a noop, i.e. it doesn't do anything at all.

You have to use SPACE = map(...);

BTW, compare your line:
Code:
SPACE = map(analogRead(3), 0, 256, 0, 1024);

with the reference page about map():
Code:
map(value, fromLow, fromHigh, toLow, toHigh)

I suspect you swapped "from" and "to" agruments, as analogRead goes from 0 to 1023, not from 0 to 256. If I'm correct, the line should be:
Code:
SPACE = map(analogRead(3), 0, 1023, 0, 255);

my 2 cents
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46141
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
      M_ = M_ ++;   // Toggles between M_ = 0 and M_ = 1
First off, no, it doesn't.

Second, M_++ is equivalent to M_ = M_ + 1. So, your statement is equivalent to M_ = M_ = M_ + 1, which is clearly silly.
Logged

Pages: [1]   Go Up
Jump to: