Pages: [1]   Go Down
Author Topic: Help with coding a (simple) array  (Read 535 times)
0 Members and 1 Guest are viewing this topic.
Eindhoven, The Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 17
better your best
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

Thanks in advance!
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 236
Posts: 24268
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It would make it easier to help you if you posted your code
« Last Edit: March 30, 2012, 08:10:55 am by AWOL » Logged

"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.

Eindhoven, The Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 17
better your best
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);   
}

Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
point me in the right direction

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

Germany
Offline Offline
Faraday Member
**
Karma: 49
Posts: 2717
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?

Logged

Eindhoven, The Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 17
better your best
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: