Go Down

Topic: Trying to Merge these programs together. Please Help. (Read 650 times) previous topic - next topic

Hi Arduino People,

So im pretty new with this stuff and Im having trouble merging two ideas into one program.
I basically want the functions of example 05 but I want to use the touch sensor (project 13) as my pushbutton switch. below are the two separate arduino codes.


------------------------------------------------
//  Example 05: Turn on LED when the button is pressed
//  and keep it on after it is released
//  including simple de-bouncing.
//  If the button is held, brightness changes.
//
// Copy and paste this example into an empty Arduino sketch

#define LED 9     // the pin for the LED
#define BUTTON 7  // input pin of the pushbutton

int val = 0;      // stores the state of the input pin

int old_val = 0;  // stores the previous value of "val"
int state = 0;    // 0 = LED off while 1 = LED on

int brightness = 128;        // Stores the brightness value
unsigned long startTime = 0; // when did we begin pressing?

void setup() {
  pinMode(LED, OUTPUT);   // tell Arduino LED is an output
  pinMode(BUTTON, INPUT); // and BUTTON is an input
}

void loop() {

  val = digitalRead(BUTTON); // read input value and store it
                             // yum, fresh

  // check if there was a transition
  if ((val == HIGH) && (old_val == LOW)) {

    state = 1 - state; // change the state from off to on
                       // or vice-versa

    startTime = millis(); // millis() is the Arduino clock
                          // it returns how many milliseconds
                          // have passed since the board has
                          // been reset.

    // (this line remembers when the button
    // was last pressed)
    delay(10);
  }

 
// check whether the button is being held down
  if ((val == HIGH) && (old_val == HIGH)) {

    // If the button is held for more than 500ms.
    if (state == 1 && (millis() - startTime) > 500) {

      brightness++; // increment brightness by 1
      delay(10);    // delay to avoid brightness going
                    // up too fast

      if (brightness > 255) { // 255 is the max brightness

        brightness = 0; // if we go over 255
                        // let's go back to 0
      }
    }
  }

  old_val = val; // val is now old, let's store it

  if (state == 1) {     
    analogWrite(LED, brightness); // turn LED ON at the                                               // current brightness level
  } else {
    analogWrite(LED, 0); // turn LED OFF
  }
}

-----------------------------------------------------------------

/*
  Arduino Starter Kit example
Project 13  - Touch Sensor Lamp

This sketch is written to accompany Project 13 in the
Arduino Starter Kit

Parts required:
1 Megohm resistor
metal foil or copper mesh
220 ohm resistor
LED

Software required :
CapacitiveSensor library by Paul Badger
http://arduino.cc/playground/Main/CapacitiveSensor

Created 18 September 2012
by Scott Fitzgerald

http://arduino.cc/starterKit

This example code is part of the public domain
*/

// import the library (must be located in the
// Arduino/libraries directory)
#include <CapacitiveSensor.h>

// create an instance of the library
// pin 4 sends electrical energy
// pin 2 senses senses a change
CapacitiveSensor capSensor = CapacitiveSensor(4,2);

// threshold for turning the lamp on
int threshold = 1000;

// pin the LED is connected to
const int ledPin = 12;


void setup() {
  // open a serial connection
  Serial.begin(9600);
  // set the LED pin as an output
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // store the value reported by the sensor in a variable
  long sensorValue = capSensor.capacitiveSensor(30);

  // print out the sensor value
  Serial.println(sensorValue);

  // if the value is greater than the threshold
  if(sensorValue > threshold) {
    // turn the LED on
    digitalWrite(ledPin, HIGH);
  }
  // if it's lower than the threshold
  else {
    // turn the LED off
    digitalWrite(ledPin, LOW);
  }

  delay(10);
}

------------------------------------------------------

Please Help!!!!

johnwasser

Code: [Select]

// import the library (must be located in the
// Arduino/libraries directory)
#include <CapacitiveSensor.h>

// create an instance of the library
// pin 4 sends electrical energy
// pin 2 senses senses a change
CapacitiveSensor capSensor = CapacitiveSensor(4,2);

// threshold for turning the lamp on
int threshold = 1000;

#define LED 9     // the pin for the LED

int val = 0;      // stores the state of the input pin

int old_val = 0;  // stores the previous value of "val"
int state = 0;    // 0 = LED off while 1 = LED on

int brightness = 128;        // Stores the brightness value
unsigned long startTime = 0; // when did we begin pressing?

void setup() {
  // open a serial connection
  Serial.begin(9600);
  pinMode(LED, OUTPUT);   // tell Arduino LED is an output
}

void loop() {
  // store the value reported by the sensor in a variable
  long sensorValue = capSensor.capacitiveSensor(30);

  // print out the sensor value
  Serial.println(sensorValue);

  // if the value is greater than the threshold, 'button' is down
  val = (sensorValue > threshold);

  // check if there was a transition
  if ((val == HIGH) && (old_val == LOW)) {

    state = 1 - state; // change the state from off to on
    // or vice-versa

    startTime = millis(); // millis() is the Arduino clock
    // it returns how many milliseconds
    // have passed since the board has
    // been reset.

    // (this line remembers when the button
    // was last pressed)
    delay(10);
  }


  // check whether the button is being held down
  if ((val == HIGH) && (old_val == HIGH)) {

    // If the button is held for more than 500ms.
    if (state == 1 && (millis() - startTime) > 500) {

      brightness++; // increment brightness by 1
      delay(10);    // delay to avoid brightness going
      // up too fast

      if (brightness > 255) { // 255 is the max brightness

        brightness = 0; // if we go over 255
        // let's go back to 0
      }
    }
  }

  old_val = val; // val is now old, let's store it

  if (state == 1) {     
    analogWrite(LED, brightness); // turn LED ON at the                                               // current brightness level
  }
  else {
    analogWrite(LED, 0); // turn LED OFF
  }
}
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

thanks for the Post.  It works perfect.  If I want to change the increment brightness what do I change in the code?  i seems to take a long time to brighten.

Thanks a bunch

johnwasser


If I want to change the increment brightness what do I change in the code?  i seems to take a long time to brighten.


I would change the line that says:
Code: [Select]
brightness++; // increment brightness by 1

That line is a shortcut for:
Code: [Select]
brightness += 1; // increment brightness by 1

Which is a shortcut for:
Code: [Select]
brightness = brightness + 1; // increment brightness by 1
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Skorn


Code: [Select]
brightness++;
delay(10);



 // you could reduce the delay

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy