Pages: [1]   Go Down
Author Topic: Switching sketch  (Read 403 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I'm new so I think it'll be polite to introcude myself. I'm mostly into analog circuit I build effect circuit for electric guitar , some synth cicuit sometimes and tube amps as well
I decided to jump into the binary world after I built the Shruthi , a little digital/analog DIY synth with an AVR µC as a brain. I was really amazed by so much possibilities in so little space . So it'll be wrong to miss this opportunity of learning programming and µC.

I've never write a code or touch a µC , so I've everything to learn. No problem , I like learning

anyway, here's the topic .

for education purpose I wanted to create a switching program . I added a LCD display to add (relative) complexity. The idea is simple , there's one input , the button and two outputs .

-first mode, a classic ON/OFF switching , the ouput 1 goes low to high.
-second mode, the tricky one : if the button is pressed twice quickly (say 1/2 second) it actives the second output . With no effect
on the first ouput.

to put the second output low , the same operation has to be done. two quick hits.

Here's what I came up with so far

Code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int inPin = 9;         
int outPin = 8;       

int state =LOW;     
int reading;           
int previous = LOW;   


long time = 0;         
long debounce = 150;   

void setup()
{
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
 lcd.print("BYPASS");
 
 
}

void loop()

{
 
  reading = digitalRead(inPin);

    if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH){
       state = LOW;
       lcd.clear();
       lcd.print("BYPASS");}
     
     
     else{
      state = HIGH;
      time = millis();
      lcd.clear();
      lcd.print("EFFECT ON");}
     
    }

   
   digitalWrite(outPin,state);
   previous = reading;
}

it's silghty modifed version of an exemple.

The question is how adding the second mode ?. I thought about an interrupt with the trigger based on a reading of two hits.
if they're short enought the second mode is actived (or put low). but the deboucing also as to be considered in the calculation.

I'm still looking for the right function and trying codes, but maybe some members could lead me to direction where I could find useful imformations ?

thanks for reading
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 106
Posts: 3772
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I made a sketch a while back that might help you.

Edit: found the working code
Code:

byte ButtonPin = 2; //digital pin 2
byte button, lastButtonState = LOW;
long time, newT = 0;
int type, output, lastOut, cnt = 0;
boolean  flagged = false;

long lastDebounceTime = 0; 
long debounceDelay = 50;

void setup() {
  pinMode(LEDpin, OUTPUT);
  pinMode(ButtonPin, INPUT);
  Serial.begin(9600);
  Serial.println("ready");
}

void loop() {
  button = digitalRead(ButtonPin);
  if ( button != lastButtonState) { // check to see if the button was pressed and debounce the button
    lastDebounceTime = millis();
    lastButtonState = button; // if button was pressed, update the status of the button state
    time = millis(); // record the time that has passed
  }
  type = check(time); // go to function and select the right case  statement
  Serial.println(type);
  switch(type){
  case 0: // leave open
    break;
  case 1:
    Serial.println("pressed 1 time(s)");
    break;
  case 2:
    Serial.println("pressed 2 time(s)");
    break;
  case 3:
    Serial.println("pressed 3 time(s)");
    break;
  }

int check(long time){
  newT=0;
  if ((millis() - lastDebounceTime) > debounceDelay) { // once inside the function, check the button again
    newT=(millis() - time); // check the recorded time to the current time and see how many times you can press the button within 1.5 seconds
    while(newT < 1500){
      button = digitalRead(ButtonPin);

      if(button == HIGH)
      {
        if(!flagged){ // this ensures that the counter does not continuously count when "flagged" is false. When the button is pressed, it will increment the counter by 1
          cnt++;
          flagged = 1; // lockout the if statement and only allow the counter increment by one.
          output = cnt; // save the counter to a new variable to be returned
        }
        //output = cnt;  // debug
        //Serial.print(newT); // debug
        //Serial.print("   "); // debug
        //Serial.println(cnt); // debug
      }
      else {
        flagged = 0; // reset the flag
      }
    }
else {
  cnt=0; // reset the counter
  if(output != lastOut){ // only output the counter if it has changed. This ensures the serial monitor does not get flooded with numbers
    lastOut = output;
    return output;//Serial.println(cnt);
   }
  }
 }


« Last Edit: July 05, 2013, 06:19:00 pm by HazardsMind » Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

London
Offline Offline
Edison Member
*
Karma: 48
Posts: 1498
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The idea is simple , there's one input , the button and two outputs .

-first mode, a classic ON/OFF switching , the ouput 1 goes low to high.
-second mode, the tricky one : if the button is pressed twice quickly (say 1/2 second) it actives the second output . With no effect
on the first ouput.
to put the second output low , the same operation has to be done. two quick hits.
Think about it this way.
1) detect the button press.
2) wait until the button is released (important to distinguish between two short presses and one long one).
3) detect if button is pressed again within time limit.
4) if no second press within time limit, do action 1
5) if second press within time limit, do action 2
« Last Edit: July 08, 2013, 11:20:37 pm by Henry_Best » Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi

thanks for your help .
 
 HazardsMind , i'll try to manipulate your code to fit mine. Then study it to undestand what's going on .

henry-best, it's clear in my mind but when I have to write and chose the correct function it gets a bit empty smiley-mr-green

I could use a second statement "if" for this function, or a "switch/break" . The thing is I have to put the right variable and the correct writting.

« Last Edit: July 05, 2013, 02:47:00 pm by fuzzo » Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 106
Posts: 3772
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I added comments to my code to help you along.
Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Offline Offline
Newbie
*
Karma: 1
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks !

I'm gonna read it .

I've try few things on my own and read document. No succes so far. But I had an idea . Using on counter started on the first press , once the button is pressed the second time the timer stops. I store both in different variable the starting time (basically 0) and the stop time. if start time + stop time = x <500ms   the µC engages the 2nd mode , otherwise it does nothing and continues to the "normal" program.

anyway, I'll read your additional comments HazardsMind and to figure out this little program.

fuzzo
Logged

Pages: [1]   Go Up
Jump to: