code advice ?!

I have 2 buttons and lcd, buttons used to lower/higher the threshold of microphone sensor. my old sensor code is:

 int val = analogRead(0);
  switch(val)
  {
    case 600:
    {
        digitalWrite(FLASH_PIN, HIGH);
        delay(100);
        digitalWrite(FLASH_PIN, LOW);
        break;
    }

as you can see i use switch and when the microphone rises 600 i trigger LED,

Question is! how can i make the code to get the new setting of threshold via buttons ? like instead of case 600 it should be case 'new setting'

thanks

I'd ditch the switch/case, and use a simple if/else with a range rather than a single threshold value. The chances of hitting it exactly may be pretty slim.

AWOL: I'd ditch the switch/case, and use a simple if/else with a range rather than a single threshold value. The chances of hitting it exactly may be pretty slim.

could you please advice me on how to code this?

if the value stays 600 should it do it again and again?

please post the whole code so we understand it better

jasperfly:

AWOL: I'd ditch the switch/case, and use a simple if/else with a range rather than a single threshold value. The chances of hitting it exactly may be pretty slim.

could you please advice me on how to code this?

int val = analogRead(0);
if (val >= 600)
{
        digitalWrite(FLASH_PIN, HIGH);
        delay(100);
        digitalWrite(FLASH_PIN, LOW);
}

is this code good enough?

val = 400;
if(con >= val){
flash on;
}else{
  flash off;
}
if( con >= 500){
  flash on;
}else{
  flash off;
}
if (con < 400){
  flash off;
}

here is whole code of flash

#define CAMERA_FLASH_PIN 4
int val = 0;   
void setup()
{
  pinMode(CAMERA_FLASH_PIN, OUTPUT);//flash pin out
  digitalWrite(CAMERA_FLASH_PIN, LOW);//off default
}

void loop()
{
  int val = analogRead(0);//read the input pin 0 
  switch(val)
  {
    case 600:
    {
        digitalWrite(CAMERA_FLASH_PIN, HIGH);  //ON
        delay(100);                            //wait
        digitalWrite(CAMERA_FLASH_PIN, LOW);   //OFF
        break;
    }
  }
}

and whole code of lcd button:

#include <Wire.h>
#include <LiquidCrystal.h>   // include LCD library
#define BUTTON_ADC_PIN           A0  // A0 is the button ADC input
#define LCD_BACKLIGHT_PIN         3  // D3 controls LCD backlight
// ADC readings expected for the 5 buttons on the ADC input
#define RIGHT_10BIT_ADC           0  // right
#define UP_10BIT_ADC            145  // up
#define DOWN_10BIT_ADC          329  // down
#define LEFT_10BIT_ADC          505  // left
#define SELECT_10BIT_ADC        741  // right
#define BUTTONHYSTERESIS         10  // hysteresis for valid button sensing window
//return values for ReadButtons()
#define BUTTON_NONE               0  // 
#define BUTTON_RIGHT              1  // 
#define BUTTON_UP                 2  // 
#define BUTTON_DOWN               3  // 
#define BUTTON_LEFT               4  // 
#define BUTTON_SELECT             5  // 
//some example macros with friendly labels for LCD backlight/pin control, tested and can be swapped into the example code as you like
#define LCD_BACKLIGHT_OFF()     digitalWrite( LCD_BACKLIGHT_PIN, LOW )
#define LCD_BACKLIGHT_ON()      digitalWrite( LCD_BACKLIGHT_PIN, HIGH )
#define LCD_BACKLIGHT(state)    { if( state ){digitalWrite( LCD_BACKLIGHT_PIN, HIGH );}else{digitalWrite( LCD_BACKLIGHT_PIN, LOW );} }
/*--------------------------------------------------------------------------------------
  Variables
--------------------------------------------------------------------------------------*/
byte buttonJustPressed  = false;         //this will be true after a ReadButtons() call if triggered
byte buttonJustReleased = false;         //this will be true after a ReadButtons() call if triggered
byte buttonWas          = BUTTON_NONE;   //used by ReadButtons() for detection of button events
/*--------------------------------------------------------------------------------------
  Init the LCD library with the LCD pins to be used
--------------------------------------------------------------------------------------*/
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 );   //Pins for the freetronics 16x2 LCD shield. LCD: ( RS, E, LCD-D4, LCD-D5, LCD-D6, LCD-D7 )
/*--------------------------------------------------------------------------------------
  setup()
  Called by the Arduino framework once, before the main loop begins
--------------------------------------------------------------------------------------*/
int analogPin = 0;
int val;
int con = 400;
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
void setup()
{
   //button adc input
   pinMode( BUTTON_ADC_PIN, INPUT );         //ensure A0 is an input
   digitalWrite( BUTTON_ADC_PIN, LOW );      //ensure pullup is off on A0
  //lcd backlight control
   digitalWrite( LCD_BACKLIGHT_PIN, HIGH );  //backlight control pin D3 is high (on)
   pinMode( LCD_BACKLIGHT_PIN, OUTPUT );     //D3 is an output
   //set up the LCD number of columns and rows: 
   lcd.begin( 16, 2 );
   //Print some initial text to the LCD.
   lcd.setCursor( 0, 0 );   //top left
   lcd.print( "THRESHOLD:" );
   lcd.setCursor( 0, 1 );   //bottom left
}
/*--------------------------------------------------------------------------------------
  loop()
  Arduino main loop
--------------------------------------------------------------------------------------*/
void loop()
{
   byte button;
  
   //get the latest button pressed, also the buttonJustPressed, buttonJustReleased flags
   button = ReadButtons();
   //blank the demo text line if a new button is pressed or released, ready for a new label to be written
   if( buttonJustPressed || buttonJustReleased )
   {
     lcd.setCursor( 4, 1 );
     lcd.print( "            " );
   }
   //show text label for the button pressed
   switch( button )
   {
      case BUTTON_NONE:
      {
         break;
      }
      
      case BUTTON_UP:
      {
         lcd.setCursor( 4, 1 );
         con++;
         delay(1000);
         lcd.print(con);
         break;
      }

      case BUTTON_DOWN:
      {
         lcd.setCursor( 5, 2 );
         con--;
         delay(1000);
         lcd.print(con);
         break;
      }
      default:
     {
        break;
     }
   }
   //clear the buttonJustPressed or buttonJustReleased flags, they've already done their job now.
   if( buttonJustPressed )
      buttonJustPressed = false;
   if( buttonJustReleased )
      buttonJustReleased = false;
}
/*--------------------------------------------------------------------------------------
  ReadButtons()
  Detect the button pressed and return the value
  Uses global values buttonWas, buttonJustPressed, buttonJustReleased.
--------------------------------------------------------------------------------------*/
byte ReadButtons()
{
   unsigned int buttonVoltage;
   byte button = BUTTON_NONE;   // return no button pressed if the below checks don't write to btn
   
   //read the button ADC pin voltage
   buttonVoltage = analogRead( BUTTON_ADC_PIN );
   //sense if the voltage falls within valid voltage windows
   if( buttonVoltage < ( RIGHT_10BIT_ADC + BUTTONHYSTERESIS ) )
   {
      button = BUTTON_RIGHT;
   }
   else if(   buttonVoltage >= ( UP_10BIT_ADC - BUTTONHYSTERESIS )
           && buttonVoltage <= ( UP_10BIT_ADC + BUTTONHYSTERESIS ) )
   {
      button = BUTTON_UP;
   }
   else if(   buttonVoltage >= ( DOWN_10BIT_ADC - BUTTONHYSTERESIS )
           && buttonVoltage <= ( DOWN_10BIT_ADC + BUTTONHYSTERESIS ) )
   {
      button = BUTTON_DOWN;
   }
   else if(   buttonVoltage >= ( LEFT_10BIT_ADC - BUTTONHYSTERESIS )
           && buttonVoltage <= ( LEFT_10BIT_ADC + BUTTONHYSTERESIS ) )
   {
      button = BUTTON_LEFT;
   }
   else if(   buttonVoltage >= ( SELECT_10BIT_ADC - BUTTONHYSTERESIS )
           && buttonVoltage <= ( SELECT_10BIT_ADC + BUTTONHYSTERESIS ) )
   {
      button = BUTTON_SELECT;
   }
   //handle button flags for just pressed and just released events
   if( ( buttonWas == BUTTON_NONE ) && ( button != BUTTON_NONE ) )
   {
      //the button was just pressed, set buttonJustPressed, this can optionally be used to trigger a once-off action for a button press event
      //it's the duty of the receiver to clear these flags if it wants to detect a new button change event
      buttonJustPressed  = true;
      buttonJustReleased = false;
   }
   if( ( buttonWas != BUTTON_NONE ) && ( button == BUTTON_NONE ) )
   {
      buttonJustPressed  = false;
      buttonJustReleased = true;
   }
   
   //save the latest button value, for change event detection next time round
   buttonWas = button;
   
   return( button );
}

robtillaart: if the value stays 600 should it do it again and again?

please post the whole code so we understand it better

no only once

Create a state machine that monitors if the flash was done

enum state { PREPARE; FLASHING; FLASHED };

state flasher = PREPARE;

void loop()
{
  val = analogRead(A0);

  switch(flasher)
  {
    case PREPARE:
      if (val > 600) state = FLASHING;
      break;
    case FLASHING:
      state = FLASHED;
      break;
    case FLASHED:
      if (digitalRead(reset) == HIGH) state = PREPARE;
     break;
  }

  if (state == FLASHING)
  {
        digitalWrite(CAMERA_FLASH_PIN, HIGH);  //ON
        delay(100);                            //wait
        digitalWrite(CAMERA_FLASH_PIN, LOW);   //OFF
  }
}

better have the state machine with these states so you can get rid of the delay(100)...

enum state { PREPARE; FLASHON; FLASHOFF; FLASHED };

thanks the issue with your code is it didn't use my new setting for val ! using the buttons.

Buttons are used to lower/higher the '600' thanks

this is pseudo code : default threshold is 600. user is unhappy wants to change it without touching the code. uses button to higher to 700. now the device only flashes when analogue reaches 700. thanks