Led off button and analog input dependent.
ok, working on project led on, unless button pressed.
it's getting analog input.
i concentrating on 3 analog inputs, 0v.........3.3v...........and 5v.
led suppose on button off whole time, unless button pressed, need led turn on when sees correct analog reading.
so if analog reading 0v , button pressed led should go off , turn on when analog reading 3.3v...............that part working how should, problem having when analog reading 3.3v , button pressed led should go off till analog reading 5v.
the led staying on when analog reading 0v. not sure doing wrong.
it's getting analog input.
i concentrating on 3 analog inputs, 0v.........3.3v...........and 5v.
led suppose on button off whole time, unless button pressed, need led turn on when sees correct analog reading.
so if analog reading 0v , button pressed led should go off , turn on when analog reading 3.3v...............that part working how should, problem having when analog reading 3.3v , button pressed led should go off till analog reading 5v.
the led staying on when analog reading 0v. not sure doing wrong.
code: [select]
const int buttonpin = 2; // number of pushbutton pin
const int ledpin = 13; // number of led pin
int outputvalue = 0;
int sensorvalue = 0;
// variables change:
int ledstate = high; // current state of output pin
int buttonstate; // current reading input pin
int lastbuttonstate = high; // previous reading input pin
int state = 0;
const int sensorpin = a0; // sensor input pin
// following variables long's because time, measured in miliseconds,
// become bigger number can stored in int.
long lastdebouncetime = 0; // last time output pin toggled
long debouncedelay = 50; // debounce time; increase if output flickers
void setup() {
pinmode(2,input_pullup); // internal pull up
pinmode(ledpin, output);
// set initial led state
digitalwrite(ledpin, ledstate);
}
void loop() {
// read state of switch local variable:
int reading = digitalread(buttonpin);
sensorvalue = analogread(sensorpin);
// check see if pressed button
// (i.e. input went high low), , you've waited
// long enough since last press ignore noise:
// if switch changed, due noise or pressing:
if (reading != lastbuttonstate) {
// reset debouncing timer
lastdebouncetime = millis();
}
if ((millis() - lastdebouncetime) > debouncedelay) {
// whatever reading at, it's been there longer
// debounce delay, take actual current state:
// if button state has changed:
if (reading != buttonstate) {
buttonstate = reading;}
if ((buttonstate == low && state == 0 && sensorvalue <50) || (buttonstate ==low && state ==0 && sensorvalue >500 && sensorvalue <800) || (buttonstate == low && state== 0 && sensorvalue >1000)) {
digitalwrite(ledpin, low);
state = 1; }
if ((state ==1 && sensorvalue >500 && sensorvalue <800) || ( state == 1 && sensorvalue >1000)) {
digitalwrite(ledpin, high);
state = 0;}}
// save reading. next time through loop,
// it'll lastbuttonstate:
lastbuttonstate = reading;
}
first, might description confusing, contradict in statements.
here thing,
"(buttonstate == low && state == 0 && sensorvalue > 500 && sensorvalue < 800) || (buttonstate == low && state == 0 && sensorvalue > 1000)"
and
"(state == 1 && sensorvalue > 500 && sensorvalue < 800) || ( state == 1 && sensorvalue > 1000)"
if first 1 true, make second 1 true , in essence turning led off, on. seems me want led turn off when not between 500 , 800, first if statement should be
quote
the led should go off , turn on when analog reading 3.3vthen say...
quote
the led should go off till analog reading 5v.sounds me want led go on when 3.3v or 5v know...
code: [select]
if ((buttonstate == low && state == 0 && sensorvalue < 50) || (buttonstate == low && state == 0 && sensorvalue > 500 && sensorvalue < 800) || (buttonstate == low && state == 0 && sensorvalue > 1000)) {
digitalwrite(ledpin, low);
state = 1;
}
if ((state == 1 && sensorvalue > 500 && sensorvalue < 800) || ( state == 1 && sensorvalue > 1000)) {
digitalwrite(ledpin, high);
state = 0;
}
here thing,
"(buttonstate == low && state == 0 && sensorvalue > 500 && sensorvalue < 800) || (buttonstate == low && state == 0 && sensorvalue > 1000)"
and
"(state == 1 && sensorvalue > 500 && sensorvalue < 800) || ( state == 1 && sensorvalue > 1000)"
if first 1 true, make second 1 true , in essence turning led off, on. seems me want led turn off when not between 500 , 800, first if statement should be
code: [select]
//turn led off while below 3.3v or greater 3.3v, less 5v, off while (0-500, 800-1000)
if (buttonstate == low) {
if (sensorvalue <= 500 || sensorvalue >= 800) && sensorvalue <= 1000)
digitalwrite(ledpin, low);
else
digitalwrite(ledpin, high);
}
}
Arduino Forum > Using Arduino > Programming Questions > Led off button and analog input dependent.
arduino
Comments
Post a Comment