Please. When you track the state of something, each thing you track needs its own "lastsesnorstate" to keep track of that button's, um, last state.
Furthermore, you need to actually set the last sensor state, so that the next time it is in fact the old sensor state and can be compared to the new sensor state.
You did not add the poor man's loop delay. It may not matter - did your own analysis lead you to conclude it was unnecessary?
It's almost as if you aren't reading our responses and actually thinking about how state change detection works.
You didn't say how your buttons are wired. You didn't say you are using a pullup or pulldown resistor. At the very least, those should have been something you could answer or ask about or google.
Someone here followed this thread and produced this:
int sensor1 = 13;
int sensor2 = 12;
int buzzer = 4;
# define NSENSORS 2
int sensor1counter = 0;
int sensor2counter = 0;
int sensor1state = 1;
int sensor2state = 1;
int lastsensorstate1 = 1;
int lastsensorstate2 = 1;
int summe;
void setup() {
Serial.begin(9600);
Serial.println("SERIAL PRINTING!!!\n");
pinMode(buzzer, OUTPUT);
pinMode(sensor1, INPUT_PULLUP);
pinMode(sensor2, INPUT_PULLUP);
}
void loop() {
sensor1state = digitalRead(sensor1);
sensor2state = digitalRead(sensor2);
if (sensor1state != lastsensorstate1){
if(sensor1state == 0){
sensor1counter = 1;
}
lastsensorstate1 = sensor1state;
}
if (sensor2state != lastsensorstate2){
if(sensor2state == 0){
sensor2counter = 1;
}
lastsensorstate2 = sensor2state;
}
summe = sensor1counter + sensor2counter;
Serial.println(summe);
if (summe == NSENSORS)
{
digitalWrite(buzzer,HIGH);
// delay(10); buzz for 0.01 seconds? I don't hear that fast
delay(1000);
digitalWrite(buzzer,LOW);
delay(100);
sensor1counter = 0;
sensor2counter = 0;
}
}
which works. For timing, she used delay. If you want to do timing this the "right" way, you need to familiarize youtself with the millis() function. Start with "blink without delay", google it and see the example in the IDE for the idiomatic pattern.
The sketch above is for two sensors. You have a choice: continue in ignorance of the power that array variables will bring to coding things like this, in terms of shorter code what will be easier to write, perfect, modify and one day enhance.
Or copy/paste/edit like you know how to do, and live with the trouble that makes for all the same reasons.
The sketch above also will not mind at all if you press and release the buttons, it fires off after you've pressed (and even if you've released) both buttons at least once. I am quite sure this would not be what you are trying to do.
Try again on your words. If English isn't one of your strengths, perhaps expressing yourself in a language you are more familiar with and using google translate would help.
As far as I can tell, something will start after all five sensors have been pressed, and go on until either some time period has elapsed or a sensor button gets pressed again.
Which just seems odd. Again, guessing, I'd say you meant to say "start a timed motion when all five sensors hare closed (or is it open? You never answered that question). Stop after that time has elapsed, or if any of the five sensors change stateback to the opposite."
This if statment that sets the counter when the switch goes LOW
if (sensor1state == 0){
sensor1counter = 1;
}
has no else clause. But if sensor1state isn't LOW, it means the sensor has become HIGH, so
if (sensor1state == 0){
sensor1counter = 1;
}
else {
// right here it means the sensor has switched the other way.
}
there's a place for reacting to a dropped sensor.
I'm tuning out for awhile. My recommendation is that pursue knowledge in lieu of making progress on your project: array variables.
And basic program logic. You might start working through the IDE examples, or hunt up some kind of organized exposure to basic programming logic.
There is no need to, and good reasons not to, rely on copy/paste/editing to grow your code.
a7