Hi,
I want to turn on a led in i + 1. It means that at the first time, the led should not light.
the code does not work. I don't know why?
Please help me.
#include <Keyboard.h>
int led = 4;
int switcher = 2;
int buttonstate = 0;
int i=0;
void setup(){
pinMode(led, OUTPUT);
pinMode(switcher,INPUT);
attachInterrupt(digitalPinToInterrupt(0),buttonPressed,FALLING);
Serial.begin(9600);
}
void loop(){
buttonstate = digitalRead(switcher);
Serial.print(buttonstate);
if (buttonstate == HIGH && i==1) {
digitalWrite(led, HIGH);
}
else {
digitalWrite(led, LOW);
}
}
void buttonPressed() {
i=1;
}
Variables used in interrupt service routine and in other routines must be declared volatile. Otherwise, the compiler will see that loop() never changes the value of i, so it's use of i will be optimized away.
What is connected to pin 2, and how? Wiring the switch using INPUT_PULLUP makes the wiring easier.
There is NO reason to be using an interrupt to read the state of a pin that has a switch pressed by a human connected to it.
@septillion there is a normal resistor of 10k ohm. @PaulS there is a switch connected to the pin 2.
I have change the code but it doesn't work too. I need your help because the deadline is approaching.
#include <Keyboard.h>
int led = 4;
int switcher = 2;
int buttonstate = 0;
int state=0;
int t=0;
int z=0;
int i=0;
void setup() {
Serial.begin(9600);
Serial.println("CLEARDATA"); // on efface les données déjà présentes, s'il y a lieu
Serial.println("LABEL,Date,Time,Batchdown, Bundle");
pinMode(led, OUTPUT);
pinMode(switcher,INPUT);
Serial.begin(9600);
}
void loop() {
buttonstate = digitalRead(switcher);
Serial.print(buttonstate);
if(buttonstate == HIGH && i==0) {
digitalWrite(led, LOW);
state=0;
}
if(buttonstate == HIGH && i+1)
{
digitalWrite(led, HIGH);
state=1;
}
if (state == 1) {
Serial.print("DATA,DATE,TIME,"); // envoi du temps et la mesure à Excel
Serial.println(state);
delay(1000);
}
}
It has been mentioned that INPUT_PULLUP makes for easier wiring. You were asked about the wiring. You still haven't explained the wiring. It IS important.
I have change the code but it doesn't work too.
Nonsense. The code is working perfectly. It might not be doing what you expect, because either your expectations are wrong or the hardware isn't wired to match your expectations.
In any case, we can't see what the code actually does, so YOU have to tell us.
We don't know what you expect the code to do, so you have to tell us.
@PaulS
The wiring is:
the led is connected so that the anode at pin 4 and the cathode at the resistor. The limit switch is connected so that the anode in (+) and the cathode in pin 2. The + is 5v and the (-) GND. Resistances are at (-)
What I want is: When the sensor is operated the first time, it should not turn on the led is only from the second time that it starts to turn on the led.
The wiring is correct because when I don't insert this condition of loop from i+1, it works.
What I want is: When the sensor is operated the first time, it should not turn on the led is only from the second time that it starts to turn on the led.
It sounds like you need to detect when the input becomes HIGH rather than when it is HIGH
Take a look at the StateChangeDetection example in the IDE
The limit switch is connected so that the anode in (+) and the cathode in pin 2.
Switches do not have anodes or cathodes.
In any case, when the switch is pressed, the pin is pulled up to 5V. When the switch is not pressed, the pin voltage is undefined. NOT a good situation. You need an external resistor to pull the pin LOW when the switch is not pressed.
Or, you wire the switch so that one leg goes to ground and the other leg goes to the digital pin, and you use the internal pullup resistor, to pull the pin HIGH when the switch is not pressed. Pressing the switch will ground the pin, so the pin reads LOW.
Agreed, make life simpler (hard enough already) and connect the switch between GND and a pin and call pinMode(pin, INPUT_PULLUP).
Next, realize
i+1
is just a calculation of which you use the result only in the if() an then throw out. It does NOT increment i. ++i or i++ does (but do evaluate differently in an if()).
Next, not the difference between a switch being HIGH (= a state which might happen for a longer period) and a switch becoming high (= single point in time). See the State change example for that.
I did what you say. But my main problem is not solved yet!
#include <Keyboard.h>
const int led = 4;
const int switcher = 2;
int i=0;
int buttonstate = 0;
int lastButtonState = 0;
int state=0;
void setup() {
Serial.begin(9600);
Serial.println("CLEARDATA"); // on efface les données déjà présentes, s'il y a lieu
Serial.println("LABEL,Date,Time,Batchdown, Bundle");
pinMode(led, OUTPUT);
pinMode(switcher,INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
buttonstate = digitalRead(switcher);
Serial.print(buttonstate);
if(buttonstate == HIGH && i==0) {
digitalWrite(led, LOW);
state=0;
}
if(buttonstate == HIGH && i++){
{
digitalWrite(led, HIGH);
state=1;
}
}
if (state == 1) {
Serial.print("DATA,DATE,TIME,"); // envoi du temps et la mesure à Excel
Serial.println(state);
delay(1000);
}
}
This time once I press the switch the led turns on and does not go off. what I want is: the led should not turn on during the first click of the switch until the second time.
@jremington the purpose of "i" is to detect the first time the switch is pressed, because I want in the first time not to turn on the led until the second time.
First note, don't use variable names which don't explain themselves like 'i'.
But do you really need to count or do you only want to know if the switch was pressed once / multiple?
Just out of curiosity, let's take a step back, why do you need that?
My suggestion, delete everything, start with a blank sketch, open the state change example next to it and start over. Trust me, that's probably the easiest way