SLOW SLOW SLOW, but why??

Good Evening Duinoids,

I have encountered an unfortunate problem with my circuit. I have just rewired a circuit powered by the Duemilanove board and the program runs about 30 times a slow as it should. The sketch uses the millis() function to generate delays, timing the switching on and off of LEDs. The sketch ran at the correct speed on a breadboard, just before the rewiring.

I have re-uploaded the sketch to no prevail.

Has anyone ecountered this problem before? What should I do??

Thanks in advance

Lozza Mugumbe

So without a schematic of what you wired and without looking at the code we are supposed to tell you what you have done wrong? I know we are good but my crystal ball is out at the menders at the moment.

Hi,

Please find enclosed the code - i do not have a schematic to hand but maybe this is a starting point.

Thanks.

#include <CapSense.h>


int sensorPin = 0;     // Analog input pin 
int sensorValue = 0;   // value read from the sensor
int onoff = 0;
int fadeValue = 0;
int fadeValue2 = 0;
int fadeValue3 = 255;
int pinSelect =0;
int buttonState =0;
int wax_wane = 0;
int lightLevel = 255;
int mode =0;
int lastButtonState  = 0;
int active = 1;
int position;
int runSequence = 0;
int breathCount =0;



float howLongToWait =2;
float lastTimeItHappened = 0;
float howLongItsBeen;

float howLongToWait2 =10;
float lastTimeItHappened2 = 0;
float howLongItsBeen2;

float howLongToWait3 =10;
float lastTimeItHappened3 = 0;
float howLongItsBeen3;


float howLongToWax =6;

unsigned long touchCount = 0;

long btnDnTime; // time the button was pressed down
long btnUpTime; // time the button was released
boolean ignoreUp = false; // whether to ignore the button release because the click+hold was triggered

unsigned long count1,count2;

CapSense   cs_4_2 = CapSense(4,2);        // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil
CapSense   cs_4_7 = CapSense(4,7);        // 10M resistor between pins 4 & 13, pin 13 is sensor pin, add wire, foil


int pin1 = 11;   // light 1
int pin2 = 10;   // light 2
int pin3 = 9;   // light 3
int pin4 = 6;   // light 4
int pin5 = 5;  // light 5
int pin6 = 3;  // light 6


int ledMode1 = 16; 
int ledMode2 = 17;
int ledMode3 = 18;

int electro = 13;   
int electro_toggle = 0;

int buttSelect = 15;  // light 5





void setup() {

  pinMode(pin1, OUTPUT);    // set pins to be outputs
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  pinMode(pin4, OUTPUT);
  pinMode(pin5, OUTPUT);
  pinMode(pin6, OUTPUT);  

  pinMode(ledMode1, OUTPUT);  
  pinMode(ledMode2, OUTPUT); 
  pinMode(ledMode3, OUTPUT);
  pinMode(buttSelect, INPUT);
  pinMode(electro, OUTPUT);  

  randomSeed(42);      

  Serial.begin(9600);  // for debugging   

  count1=millis();
  count2=millis();


  //cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example

}

void loop() {

  while(1)

  {  

    // TOUCH TOP_________________________________________________________________________


    int total =  cs_4_2.capSense(10);

    if (count2 < millis()){

      //Serial.print("\t"); 
      //Serial.print(total);
      //Serial.print("\t"); 
      // Serial.print(electro_toggle);
      //Serial.print("\n");                   //new line 

      count2=millis() + 200;                //serial output delay
    }                                  

    if(total>=50) 

      touchCount++; 

    if (touchCount ==30) 
      electro_toggle = (electro_toggle + 1) %2;



    if(electro_toggle == 0)
      digitalWrite(electro, LOW);

    else if(electro_toggle == 1)
      digitalWrite(electro, HIGH); 



    if(total<=50) 
      touchCount=0;




    //Touch Main________________________________________________________________________________________________


    int total2 =  cs_4_7.capSense(10);  


    if(total2>=100 && pinSelect == 0) 
      pinSelect = 1;

    if (6 > pinSelect >=1)
      fadeup();

    if(pinSelect == 6 && wax_wane==0 && breathCount < 16)
      wane();

    if(pinSelect == 6 && wax_wane==1 && breathCount < 16)
      wax();


    if(breathCount ==16 && fadeValue3>=0)

    {
      howLongItsBeen3 = millis() - lastTimeItHappened3;    

      if (howLongItsBeen3 >= 20) 
      {        
        fadeValue3 -=1;    
        analogWrite(pin6, fadeValue3); 
        analogWrite(pin5, fadeValue3);  
        analogWrite(pin4, fadeValue3);  
        analogWrite(pin3, fadeValue3);  
        analogWrite(pin2, fadeValue3);  
        analogWrite(pin1, fadeValue3);  

        lastTimeItHappened3 = millis(); 

      }

    }




    if(fadeValue3 ==0)
    {
      breathCount =0;
      pinSelect =0;
      fadeValue =0;
      fadeValue2 =0;
      fadeValue3 =255;
      wax_wane=0;
    }




    if(pinSelect >= 2 && fadeValue2 < 255 && breathCount<16) 
    {
      howLongItsBeen2 = millis() - lastTimeItHappened2;    

      if (howLongItsBeen2 >= 200) 
      {        
        fadeValue2 +=1;    
        analogWrite(pin6, fadeValue2);  

        lastTimeItHappened2 = millis(); 

      }

    }





    Serial.print(total);
    Serial.print("\t");  
    Serial.print(pinSelect);
    Serial.print("\t");  
     Serial.print(wax_wane);
    Serial.print("\t"); 
    Serial.print(breathCount);
    Serial.print("\n");   //new line 




  }

}




//FUNCTIONS___________________________________________________________________________________________________________________


int fadeup()
{  

  howLongItsBeen = millis() - lastTimeItHappened;    

  if (howLongItsBeen >= howLongToWait) 
  {  


    if(fadeValue >= 255 && pinSelect <6)
    {      
      pinSelect++;
      fadeValue=0;     
      lightLevel=255;     
      howLongToWait = howLongToWait+2;
    }

    if(pinSelect == 1)   {
      fadeValue +=1;    
      analogWrite(pin1, fadeValue);  
    }

    else if (pinSelect == 2){
      fadeValue +=1;  
      analogWrite(pin2, fadeValue);
    }  

    else if (pinSelect == 3){
      fadeValue +=1;  
      analogWrite(pin3, fadeValue);
    }  

    else if (pinSelect == 4){
      fadeValue +=1;  
      analogWrite(pin4, fadeValue);
    }  

    else if (pinSelect == 5){
      fadeValue +=1;  
      analogWrite(pin5, fadeValue);
    }  

    lastTimeItHappened = millis(); 
  }

}



int wax() {

  howLongItsBeen = millis() - lastTimeItHappened;    

  if (howLongItsBeen >= 6) 
  {   

    if (fadeValue >= 254) 
    {   
      wax_wane = 0; 
      breathCount ++;      
    }


    if(wax_wane == 1)   {
      fadeValue +=1;    
      analogWrite(pin5, fadeValue);  
      analogWrite(pin4, fadeValue);
      analogWrite(pin3, fadeValue);
      analogWrite(pin2, fadeValue);
      analogWrite(pin1, fadeValue);
    }

    lastTimeItHappened = millis(); 


  }

  lightLevel= fadeValue;

  //Serial.println(lightLevel);
}





int wane()

{  

  if(fadeValue ==0)
    fadeValue=lightLevel;
  howLongItsBeen = millis() - lastTimeItHappened;    

  if (howLongItsBeen >=6) 
  {   

    if(wax_wane == 0)   {
      fadeValue -=1;    
      analogWrite(pin5, fadeValue);  
      analogWrite(pin4, fadeValue);
      analogWrite(pin3, fadeValue);
      analogWrite(pin2, fadeValue);
      analogWrite(pin1, fadeValue);
    }

    lastTimeItHappened = millis(); 


    if (fadeValue < 10)     
    {
      wax_wane = 1;
      breathCount ++;
    }

  }

  lightLevel= fadeValue;

  //Serial.println(lightLevel);
}

Go back, modify your comment, highlight the code, hit the # key...

apologies...

OK lets start off by correcting some errors. Loose the infinite loop:- while(1) { You don't need it as loop() will do that for you. I also think your { were missplaced because I couldn't see one at the end.

Next all your times are stored as floats, these need to be long ints, maybe that is screwing you up.

Incorrect fuse programming could easily cause a sketch to run 16x slower (internal 1MHz clock instead of external 16MHz clock.) But that shouldn't happen just as a result of rewiring...

Having another look at you code the only thing that acts as an input is the two cap sense readings. Therefore those two commands are the only thing that could be slowing it down if you rewired it, everything else being equal. Therefore I suggest you comment those out and return a fixed value into those variables and see if it speeds up. I know the cap sense works by looking at how long it takes a charge to decay. If you have changed the wiring it could be that the leakage currents have changed considerably and that is slowing things down.