Go Down

Topic: Touch on Touch off multiple Leds Capacative touch  (Read 1 time) previous topic - next topic

Sparkyduke

Jun 27, 2016, 01:21 pm Last Edit: Jun 27, 2016, 01:35 pm by Sparkyduke
Hi
All Im Struggling with a little capacative touch project.
using a the example script I have numerous capacative switches controlling and fading LEDs.
but I want to turn them into on / off switches with a fade.

could some one help me as i think i need to use a Boolean but have been seriously struggling to control each switch individually.

I've posted my basic control script is anyone could help that would be great thank you.



#include <CapacitiveSensor.h>
int led = 10;                                          //change '42' to any desired pin...
int led2 = 9;
long time = 0;
int state = HIGH;

int debounce = 200;

CapacitiveSensor   cs_4_2 = CapacitiveSensor(4,2);        // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired
CapacitiveSensor   cs_4_6 = CapacitiveSensor(4,6);        // 10M resistor between pins 4 & 6, pin 6 is sensor pin, add a wire and or foil
//CapacitiveSensor   cs_4_8 = CapacitiveSensor(4,8);        // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil

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

   Serial.begin(9600);
   pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  //pinMode(led, OUTPUT);
}

void loop()                   
{
    long start = millis();
    long total1 =  cs_4_2.capacitiveSensor(30);
    long total2 =  cs_4_6.capacitiveSensor(30);
   // long total3 =  cs_4_8.capacitiveSensor(30);

    Serial.print(millis() - start);        // check on performance in milliseconds
    Serial.print("\t");                    // tab character for debug windown spacing

    Serial.print(total1);                  // print sensor output 1
    Serial.print("\t");
    Serial.print(total2);                  // print sensor output 2
    Serial.print("\t");
  //  Serial.println(total3);                // print sensor output 3

                           // arbitrary delay to limit data to serial port
  // see if there's incoming serial data:
 if ( total1 > 170)
      led = led + 15;
     
    else
      led = led - 0.5;
   
    if (led > 255) 
      led = 255;
   
    if (led < 0)
      led = 0;
     
    analogWrite(10, led);
   
 if (total2 > 500  )
 led2 = led2 + 15;
    else
      led2 = led2 - 0.5;
   
    if (led2 > 255) 
      led2 = 255;
   
    if (led2 < 0)
      led2 = 0;
     
    analogWrite(9, led2);
   
      Serial.println(millis()-time);
      delay(10);
}

PaulS

Code: [Select]
if ( total1 > 170)
      led = led + 15;
     
    else
      led = led - 0.5;

It appears as though led is a pin number. Why are you incrementing the pin number? led IS an int. Decrementing an int by 1/2 is useless.

You REALLY should start over. Use pin in the name of any variable that contains a pin number. Use value in the name of any variable that contains a value.

Do logical things with the types that you choose.

Quote
but I want to turn them into on / off switches with a fade.
I really don't understand what this means. Do you want, when you press a switch, for the associated LED to fade on or fade off? What should happen if you press the switch again while the associated LED is fading?

J-M-L

I suppose you read this Arduino Tutorial

In your code some things feel weird right away:

Code: [Select]
int led = 10;                                          //change '42' to any desired pin...
int led2 = 9;

..

 pinMode(led, OUTPUT);
 pinMode(led2, OUTPUT);

...



so led and led2 are  PINS number definition - (poor variables names)


but then later in your code you do mess around with those variables to compute probably a dimming value for PWM as you do


Code: [Select]

    analogWrite(10, led);
..
    analogWrite(9, led2);


it's a good programming practice to not mess around with what variables are meant to do.



then you have weird comments

Code: [Select]
// arbitrary delay to limit data to serial port
  // see if there's incoming serial data:


not sure what they refer to as there is no Serial code...

OK - but this is not why your code does not work. I think the problem is here:

you have led and led2 declared as a pin references which you use to do some math (bad practice, I insist :)), but they are integers and then in the code you do (same with led2)

Code: [Select]
else led = led - 0.5;


If you are unsure about mixing integer and float values, I would suggest you print the result of this calculation

Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

J-M-L

Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

Sparkyduke

Sorry All

I didn't do a very good job at that post.
Ok I've made some corrections that you have mentioned and realised im slightly retarded.

But the main out come what i want is to turn the capacative sensors into toggle switches.
So far they simply fade a Led up and then down when they don't full fill the if.

I would like the same sensors to  turn HIGH with one touch fading a LED up.
Then LOW with another touch fading down a LED.

So far i have my dodgy script


Code: [Select]
#include <CapacitiveSensor.h>
int led1 = 0;                                      
int led2 = 0;
int led3 = 0;                                        


long time = 0; // long csSum;




int debounce = 100;

CapacitiveSensor   cs_4_22 = CapacitiveSensor(4,22);        // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired
CapacitiveSensor   cs_4_24 = CapacitiveSensor(4,24);        // 10M resistor between pins 4 & 6, pin 6 is sensor pin, add a wire and or foil
CapacitiveSensor   cs_4_26 = CapacitiveSensor(4,26);        // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil

void setup()                    
{


   Serial.begin(9600);
   pinMode(2, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(6, OUTPUT);

}

void loop()                    
{
    long start = millis();
    long total1 =  cs_4_22.capacitiveSensor(20);
    long total2 =  cs_4_24.capacitiveSensor(20);
    long total3 =  cs_4_26.capacitiveSensor(20);
 
      
   Serial.print(millis() - start);  // check on performance in milliseconds
   Serial.print("\t");    

    
  
        if (total1 > 40) { total1 = 190 ;
           delay(30);
          total2 =  map(total1, 0, 190, 190, 0  );
          (total3 = 0 );
           (total4 = 0 );
           (total5 = 0);
          (total6 = 0 );
          (total7 = 0);
          (total8 = 0 );
          //(total9 = 0);
        }
        if (total2 > 35) { total2 = 190 ;  
        
          (total3 = 0 );
           (total4 = 0 );
          (total5 = 0);
          (total6 = 0 );
          (total7 = 0);
          //(total8 = 0 );
          //(total9 = 0);
        }
        if (total3 > 35) { total3 = 190 ;  //35 triggering value
         delay(30);
          (total1 = total1 - total3);
          (total2 = total2 - total3);  // minuses the values from the increasing value (removes noise from pin)
          (total4 = 0 );  // Sets other values to 0
          (total5 = 0);
          (total6 = 0 );
          (total7 = 0);
          (total8 = 0 );
          //(total9 = 0);
        }

        
    Serial.print(total1);
    Serial.print("\t");
    Serial.print(total2);
    Serial.print("\t");
    Serial.print(total3);
    Serial.print("\t");


//---------------------------------------LED1------------------------------------------------//
                          
 if ( total1 >= 190)
 
    // Serial.print ("building 1");
    
      led1 = led1 + 10;
      //delay(50);

    else
      led1 = led1 - 3;
    
    if (led1 > 255)  
      led1 = 255;
    
    if (led1 < 0)
      led1 = 0;
      
    analogWrite(2, led1);

   //---------------------------------------LED2------------------------------------------------//
                        
 if (total2 >= 190  )
     led2 = led2 + 10;
    else
      led2 = led2 - 3;
    
    if (led2 > 255)  
      led2 = 255;
    
    if (led2 < 0)
      led2 = 0;
      
    analogWrite(6, led2);
    
    //---------------------------------------LED3------------------------------------------------//
                                
     if ( total3 >= 190)
      led3 = led3 + 10;
      
    else
      led3 = led3 - 3;
    
    if (led3 > 255)  
      led3 = 255;
    
    if (led3 < 0)
      led3 = 0;
      
    analogWrite(8, led3);
  
  
   /////////////////////////////////////////////////////////////end/////////////////////////////////////

  
      Serial.println(millis()-time);
      delay(10);

}

J-M-L

Here is a "dirty" approach with a state machine that I hacked together quickly




wiring:
pin 7 --> jumper wire --> breadboard --> 1 MEG resitsor |--> jumper wire --> pin 8
                                                        |--> jumper wire for user touch

pin 3 --> jumper wire --> breadboard --> LED Anode --> 200 Ohm (or suitable for your lED) current limiting resistor --> GND



image link in higher quality



if you look at the code:

I wrapped the CapacitiveSensor into a struct to keep track of the various elements I wanted to use for a given sensor so that you can have multiple. Obviously the right way to do this would not be a basic struct but just embed that into a class / extend the CapacitiveSensor class

I built a small state machine that tracks what the user does. if I touch quickly (less than TOUCH_ONOFFDELAY) then it's a ON/OFF signal but if you grab the sensor and hold it, then the LED will cycle through fading  (of course need a PWM pin driving the LED).

I've a function  createCapacitive that instantiates a new structure. you need to pass in the sensors pins you use and the PWM pin you want to drive

I've a function handleSensor that will check the status of a given sensor and do the right thing.

this way you can create multiple sensors pointers, possibly in an array, and you just iterate handleSensor for each element of the array in the loop()


Have a look and give it a try, I tested the below code on a UNO R3.

Code: [Select]
// ********************************************************************************************
// demo example
// short touch = on/off
// long touch = fading
//
// wiring:
//
// pin 7 --> jumper wire --> breadboard --> 1 MEG resitsor |--> jumper wire --> pin 8
//                                                         |--> jumper wire for user touch
//
// pin 3 --> jumper wire --> breadboard --> LED Anode --> 200 Ohm (or suitable for your lED) current limiting resistor --> GND
//
// ********************************************************************************************

#include <CapacitiveSensor.h>

#define TOUCH_THRESHOLD 100     // depends on your resistors. above that value, considers that the sensor is touched
#define TOUCH_ONOFFDELAY 200    // in ms if we touch the sensor for less than that time, then it's ON/OFF
#define FADE_DELAY 5            // in ms - when we are fading, that's the delay between 2 consecutive changes in PWM
#define SENSITIVITY 10          // depends on your resistors

enum csStatus {atRest, beingTouched, fading} ; // our different states

struct capacitive {
  CapacitiveSensor * sensor;                   // the sensor we are connected to
  csStatus state;                              // our current status
  long value;                                  // the last sensor reading
  unsigned long int lastStartTouchTime;        // when we last started touching the sensor
  byte pin;                                    // which pin we are driving
  uint8_t pinPWMValue;                         // the PWM value for the pin
  int fadingValue;                             // fading direction and speed (positive we increase brightness, negative we dim the LED)
} ;

capacitive * csNb1;

/* ************************************************ */
/* Build a new capacitive struct and initialize it  */
/* ************************************************ */

capacitive * createCapacitive(byte startPin, byte endPin, byte pwmPin)
{
  capacitive * cs = new capacitive;
  // initialize our first sensor structure
  cs->sensor =  new CapacitiveSensor(startPin, endPin); //1M Resistor between pins 7 and 8
  cs->state = atRest;
  cs->lastStartTouchTime = 0UL;
  cs->pin = pwmPin;                           // needs to be a PWM pin do dim
  cs->pinPWMValue = 0;
  cs->fadingValue = +1;
  pinMode(cs->pin, OUTPUT);
  analogWrite(cs->pin, cs->pinPWMValue);
  return cs;
}


/* ************************************************ */
/* check a sensor and do the right thing with it    */
/* ************************************************ */

void handleSensor(capacitive * mySensor)
{
  mySensor->value = mySensor->sensor->capacitiveSensor(SENSITIVITY);

  if (mySensor->value >= TOUCH_THRESHOLD) {
    // *********************************************
    // we are touching the sensor
    // *********************************************

    switch (mySensor->state) {
      case atRest:
        mySensor->lastStartTouchTime = millis();
        mySensor->state = beingTouched;
        break;
      case beingTouched:
        if (millis() - mySensor->lastStartTouchTime >= TOUCH_ONOFFDELAY) {
          // we are touching it for a long time, start fading/unfading
          mySensor->lastStartTouchTime = millis();
          mySensor->state = fading;
        }
        break;
      case fading:
        // check if it is time to fade a bit more?
        if (millis() - mySensor->lastStartTouchTime >= FADE_DELAY) {
          // YES FADE_DELAY has gone, so we change the PWM and keep it between 0 and 255

          if (mySensor->fadingValue > 0) { // if we are increasing, check if we would go above 255 and if so, stop at 255 and reverse fading direction
            if ((((int) mySensor->pinPWMValue) + mySensor->fadingValue) > 255) { // because we use a uint8_t we need to cast to do the check
              mySensor->pinPWMValue = 255;
              mySensor->fadingValue = -mySensor->fadingValue;
            } else mySensor->pinPWMValue += mySensor->fadingValue;
          } else {   // if we are decreasing, check if we would go below 0 and if so, stop at 0 and reverse fading direction
            if ((((int) mySensor->pinPWMValue) + mySensor->fadingValue) < 0) { // because we use a uint8_t we need to cast to do the
              mySensor->pinPWMValue = 0;
              mySensor->fadingValue = -mySensor->fadingValue;
            } else mySensor->pinPWMValue += mySensor->fadingValue;
          }
          analogWrite(mySensor->pin, mySensor->pinPWMValue);  // set up our PWM at the right new value
          mySensor->lastStartTouchTime += FADE_DELAY;   // update our timer
        }
        break;
    } // end switch state

  } else {

    // *********************************************
    // we are not touching the sensor
    // *********************************************

    switch (mySensor->state) {
      case beingTouched:
        if (millis() - mySensor->lastStartTouchTime <= TOUCH_ONOFFDELAY) {
          // we touched it for a short time, turn it ON or OFF based on current value
          if (mySensor->pinPWMValue < 127) mySensor->pinPWMValue = 255; else mySensor->pinPWMValue = 0;
          analogWrite(mySensor->pin, mySensor->pinPWMValue);
          delay(15); // debounce
        }
        break;
      case fading:
        // we were fading and we stopped touching it. keep current dimmed value but invert dimming direction if we start touching again
        mySensor->fadingValue = -mySensor->fadingValue;
    } // end switch state
    mySensor->state = atRest;
  }
}

// ********************************************************************************************

void setup() {
  csNb1 = createCapacitive(7, 8, 3); // startPin, endPin of the 1Meg Ohm Resistor and pwmPin to drive
}

void loop()
{
  handleSensor(csNb1);
} // end loop



Hope this helps :)
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

Sparkyduke

J-M-L Jesus very cool thanks looks amazing.
Its far beyond me all of that, so I've mimicked your set up but its not compiling on my mega.

Im sure ive done somthing stupid but
Im getting this error.


Internet.ino:23:1: error: 'capacitive' does not name a type
Internet.ino:24:19: error: variable or field 'handleSensor' declared void
Internet.ino:24:19: error: 'capacitive' was not declared in this scope
Internet.ino:24:32: error: 'mySensor' was not declared in this scope
Internet.ino:24:1: error: 'cstruct' does not name a type
Internet.ino:34:1: error: 'capacitive' does not name a type
Internet.ino:40:1: error: 'capacitive' does not name a type
Internet.ino:60:19: error: variable or field 'handleSensor' declared void
Internet.ino:60:19: error: 'capacitive' was not declared in this scope
Internet.ino:60:32: error: 'mySensor' was not declared in this scope
Error compiling. 

J-M-L

Do you have the very last version of the Aduino IDE?
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

J-M-L

If not try this - not sure which version allows to drop the struct keyword

Code: [Select]
// ********************************************************************************************
// demo example
// short touch = on/off
// long touch = fading
//
// wiring:
//
// pin 7 --> jumper wire --> breadboard --> 1 MEG resitsor |--> jumper wire --> pin 8
//                                                         |--> jumper wire for user touch
//
// pin 3 --> jumper wire --> breadboard --> LED Anode --> 200 Ohm (or suitable for your lED) current limiting resistor --> GND
//
// ********************************************************************************************

#include <CapacitiveSensor.h>

#define TOUCH_THRESHOLD 100     // depends on your resistors. above that value, considers that the sensor is touched
#define TOUCH_ONOFFDELAY 200    // in ms if we touch the sensor for less than that time, then it's ON/OFF
#define FADE_DELAY 5            // in ms - when we are fading, that's the delay between 2 consecutive changes in PWM
#define SENSITIVITY 10          // depends on your resistors

enum csStatus {atRest, beingTouched, fading} ; // our different states

struct capacitive {
 CapacitiveSensor * sensor;                   // the sensor we are connected to
 csStatus state;                              // our current status
 long value;                                  // the last sensor reading
 unsigned long int lastStartTouchTime;        // when we last started touching the sensor
 byte pin;                                    // which pin we are driving
 uint8_t pinPWMValue;                         // the PWM value for the pin
 int fadingValue;                             // fading direction and speed (positive we increase brightness, negative we dim the LED)
} ;

struct capacitive * csNb1;

/* ************************************************ */
/* Build a new capacitive struct and initialize it  */
/* ************************************************ */

struct capacitive * createCapacitive(byte startPin, byte endPin, byte pwmPin)
{
 struct capacitive * cs = new capacitive;
 // initialize our first sensor structure
 cs->sensor =  new CapacitiveSensor(startPin, endPin); //1M Resistor between pins 7 and 8
 cs->state = atRest;
 cs->lastStartTouchTime = 0UL;
 cs->pin = pwmPin;                           // needs to be a PWM pin do dim
 cs->pinPWMValue = 0;
 cs->fadingValue = +1;
 pinMode(cs->pin, OUTPUT);
 analogWrite(cs->pin, cs->pinPWMValue);
 return cs;
}


/* ************************************************ */
/* check a sensor and do the right thing with it    */
/* ************************************************ */

void handleSensor(struct capacitive * mySensor)
{
 mySensor->value = mySensor->sensor->capacitiveSensor(SENSITIVITY);

 if (mySensor->value >= TOUCH_THRESHOLD) {
   // *********************************************
   // we are touching the sensor
   // *********************************************

   switch (mySensor->state) {
     case atRest:
       mySensor->lastStartTouchTime = millis();
       mySensor->state = beingTouched;
       break;
     case beingTouched:
       if (millis() - mySensor->lastStartTouchTime >= TOUCH_ONOFFDELAY) {
         // we are touching it for a long time, start fading/unfading
         mySensor->lastStartTouchTime = millis();
         mySensor->state = fading;
       }
       break;
     case fading:
       // check if it is time to fade a bit more?
       if (millis() - mySensor->lastStartTouchTime >= FADE_DELAY) {
         // YES FADE_DELAY has gone, so we change the PWM and keep it between 0 and 255

         if (mySensor->fadingValue > 0) { // if we are increasing, check if we would go above 255 and if so, stop at 255 and reverse fading direction
           if ((((int) mySensor->pinPWMValue) + mySensor->fadingValue) > 255) { // because we use a uint8_t we need to cast to do the check
             mySensor->pinPWMValue = 255;
             mySensor->fadingValue = -mySensor->fadingValue;
           } else mySensor->pinPWMValue += mySensor->fadingValue;
         } else {   // if we are decreasing, check if we would go below 0 and if so, stop at 0 and reverse fading direction
           if ((((int) mySensor->pinPWMValue) + mySensor->fadingValue) < 0) { // because we use a uint8_t we need to cast to do the
             mySensor->pinPWMValue = 0;
             mySensor->fadingValue = -mySensor->fadingValue;
           } else mySensor->pinPWMValue += mySensor->fadingValue;
         }
         analogWrite(mySensor->pin, mySensor->pinPWMValue);  // set up our PWM at the right new value
         mySensor->lastStartTouchTime += FADE_DELAY;   // update our timer
       }
       break;
   } // end switch state

 } else {

   // *********************************************
   // we are not touching the sensor
   // *********************************************

   switch (mySensor->state) {
     case beingTouched:
       if (millis() - mySensor->lastStartTouchTime <= TOUCH_ONOFFDELAY) {
         // we touched it for a short time, turn it ON or OFF based on current value
         if (mySensor->pinPWMValue < 127) mySensor->pinPWMValue = 255; else mySensor->pinPWMValue = 0;
         analogWrite(mySensor->pin, mySensor->pinPWMValue);
         delay(15); // debounce
       }
       break;
     case fading:
       // we were fading and we stopped touching it. keep current dimmed value but invert dimming direction if we start touching again
       mySensor->fadingValue = -mySensor->fadingValue;
   } // end switch state
   mySensor->state = atRest;
 }
}

// ********************************************************************************************

void setup() {
 csNb1 = createCapacitive(7, 8, 3); // startPin, endPin of the 1Meg Ohm Resistor and pwmPin to drive
}

void loop()
{
 handleSensor(csNb1);
} // end loop
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

Sparkyduke

Thanks J-M-L

It partly worked but sorry to be a pain but the scripts seems far to complicated for a simpleton like me I just don't really understand to much of what you have written.
But the response from the LED was relatively flickery, and i didn't even know where to start to see if i could alter the effect.

maybe due to the sensitivity but i had another go at smashing some script together to show you what i'm trying to do.

using this: https://www.arduino.cc/en/Tutorial/StateChangeDetection

I was able to make another dirty script  with he lighting effect I wanted

but again I just cant work out how to duplicate this for multiple sensors and LEDs.

Please have a look at what i have written and if you could simplify your previous script or use what i have written to help the fact.

I REALLY REALLY APPRECIATE ALL THE HELP!!!! SO FAR 




Code: [Select]
/* //////////...................................................////////////////////////////////
Pin 22 to 1Mohm ---> pin 4 Recieve pin

totalx = capacitance value to equal the trigger.
the triggered  value to act as a toggle switch allowing  functions per touch.
i.e touch 1 fade led up
touch 2 change state
touch 3 fade led down

sketch currently has fade up fade down second input can be added under the no count (buttonPushCounter % 3 == 0)


(maybe????)

/////////////...................................................//////////////////////////////*/



#include <CapacitiveSensor.h>


long time = 0; //long time

int led = 2;
int value = 0;
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

CapacitiveSensor cs_4_22 = CapacitiveSensor(4, 22); //10M Resistor between pins 4 and 22, you may also connect an antenna on pin 22

void setup() {
  Serial.begin(9600);
  pinMode(led, OUTPUT);

}


void loop() {
 
  long total1 =  cs_4_22.capacitiveSensor(30);

  if (total1 > 35) { //: capacitive trigger number
    time += total1;
    Serial.println(total1); // print the value given by sensor
    Serial.print("\t");
    buttonState = HIGH;   //activates the button counter
   
    Serial.print("Trigger tota11:"); // prints total1 has been triggered
    Serial.println(time);
    if (time > 0) {
      time = 0; //Reset the time take to the read pin
    }


    cs_4_22.reset_CS_AutoCal(); //Stops readings
    {
     
       
        if (buttonState == HIGH) {
          buttonPushCounter++;
          // Serial.println("on");
          Serial.print("number of button pushes:  ");
          Serial.println(buttonPushCounter); //prints the no. of times the button was high
         
          Serial.print(" ButtonState:   ");
           Serial.println (buttonState);


       
        // Delay a little bit to avoid bouncing
        delay(50);
      }
      // save the current state as the last state,
      //for next time through the loop
      lastButtonState = buttonState;


      // turns on the LED every 2 button pushes by
      // checking the modulo of the button push counter.
      // the modulo function gives you the remainder of
      // the division of two numbers:
      if (buttonPushCounter % 2 == 0) {
        for (value = 0 ; value <= 255; value += 5) { // sets the value (range from 0 to 255)


          analogWrite(led, value);         
          delay(30);



        }
      }

      else {
        //if (buttonPushCounter % 3 == 0) {
        for (value = 255 ; value >= 0; value -= 5) { // sets the value (range from 255 to 0)
          analogWrite (led, value);
          delay(30);

          time = 0; //Timeout caused by bad readings
        }
      }
    }
  }
}

Go Up