Go Down

Topic: Help with coding a (simple) array (Read 681 times) previous topic - next topic

Reflex

Hi,

I have a loop with a delay at the end.
What I want is to make this delay a variable and let that variable be changed every 5 minutes into the next on my array.

For instance, for the first 5 minutes, delay = 200ms
for the second 5 minutes, delay = 1200ms
for the third 5 minutes, delay = 500 ms
etc. etc. up untill I have 2 hours filled (24 variables), then repeat the cycle.

It doesn't seem to complicated but I am new to this so I hope someone can help me or point me in the right direction :)

Thanks in advance!

AWOL

#1
Mar 30, 2012, 02:38 pm Last Edit: Mar 30, 2012, 03:10 pm by AWOL Reason: 1
It would make it easier to help you if you posted your code
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Reflex

This is what I have now, the function of the potmeter has to be replaced by a piece of code to regulate the delay.

Quote

int brightness = 0;    // how bright the LED is
int fadeAmount = 10;    // how many points to fade the LED by
int sensorValue = 0;
int  outputValue =0;
//const int analogInPin = A0;  //  make frequency controlled by potmeter

void setup()  {
  // declare pin 9 to be an output:

  pinMode(9, OUTPUT);
  Serial.begin(9600);


void loop()  {
    //sensorValue = analogRead(analogInPin);           
  // map it to the range of the analog out:
  //outputValue = map(sensorValue, 0, 1023, 200, 1600); 
   
  Serial.println("1");
  
  while(brightness >= 0) {
    // set the brightness of pin 9:
    analogWrite(9, brightness);
    
    //Serial.println("kjklo");
  
    // change the brightness for next time through the loop:
    brightness = brightness + fadeAmount;
  
    // reverse the direction of the fading at the ends of the fade:
    if (brightness == 230) {
      fadeAmount = -fadeAmount ; 
    }     
    // wait for 30 milliseconds to see the dimming effect
        
    delay(1);
  }
  
  //delay(100);
  brightness = 0;
  fadeAmount = 10;
  
   while(brightness >= 0) {
     // set the brightness of pin 9:
     analogWrite(9, brightness);
     
         //Serial.println("dd");
     
    // change the brightness for next time through the loop:
    brightness = brightness + fadeAmount;
  
    // reverse the direction of the fading at the ends of the fade:
    if (brightness == 150) {
      fadeAmount = -fadeAmount ; 
    }     
    // wait for 30 milliseconds to see the dimming effect
    delay(5);
  }
  
  fadeAmount = 10;
  brightness = 0;
  delay(outputValue); //variable output from potmeter
    Serial.println(outputValue);   
}


dxw00d

Quote
point me in the right direction


http://arduino.cc/en/Reference/Array

michael_x

You already noticed that loop will be very often, and you correctly modify brightness step by step from one loop run to the next.
Similar, you switch direction
  fadeAmount = -fadeAmount;
at the end.

But then you destroy that logic by

//delay(100);
 brightness = 0;
 fadeAmount = 10;


... if I get your code right.

After you got that fading right, you should add another state variable ( like your brightness, which keeps a state from one loop to the next ) telling where in the overall story you are.
This might be a "step" going from 0 to 23 and being used as an index to your array of duration times.  

I hope this delay per loop should control the fading speed ?


Reflex

What the code I have written creates is a visualization of a heartbeat (the "lubb-dubb" sound made by the heart as it beats) by fading a LED twice, one shortly after the other.
What the delay per loop does is alter the frequency of the heartbeat, let's say from 60BPM to 120BPM. This is now done by a potmeter which I want to see replaced by a piece of code that changes this delay in what I program it to be in 24 intervals of 5 minutes.

Go Up