Button state does not work inside switc case?

can anyone see? , this is what I tried, tried many different things but none work.

#define E1 10 // Enable Pin for motor 1
#define E2 11 // Enable Pin for motor 2

#define I1 8 // Control pin 1 for motor 1
#define I2 9 // Control pin 2 for motor 1
#define I3 6 // Control pin 1 for motor 2
#define I4 7 // Control pin 2 for motor 2

const int buttonPin = 2;
const int ledPin = 13;
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0;

void setup() {

Serial.begin(9600);
pinMode(E1, OUTPUT);
pinMode(E2, OUTPUT);

pinMode(I1, OUTPUT);
pinMode(I2, OUTPUT);
pinMode(I3, OUTPUT);
pinMode(I4, OUTPUT);

pinMode(ledPin, OUTPUT);
buttonState = digitalRead(buttonPin);
pinMode(buttonPin, INPUT);

}

void loop() {

if (Serial.available() > 0) {
int inByte = Serial.read();
int speed; // Local variable
switch (inByte) {

case ‘1’: // Motor 1 Forward
buttonState = digitalRead(buttonPin);

// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;

} else {
// if the current state is LOW then the button went from on to off:
;
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;

// turns on the LED every four button pushes by checking the modulo of the
// button push counter. the modulo function gives you the remainder of the
// division of two numbers:
if (buttonPushCounter % 4 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}

break;

case ‘3’:

break;

case ‘4’:

break;

case ‘5’:
break;

case ‘6’:
break;

default:
// turn all the connections off if an unmapped key is pressed:
for (int thisPin = 2; thisPin < 11; thisPin++) {
digitalWrite(thisPin, LOW);
}
}
}
}

Use code tags

}

}
    }
      }

Use the Arduino auto-format to put the correct spaces in front of each brace. If it looks like it scrambled your code then your code was wrong. Carefully check your braces.

It looks like your code waits for a serial input. If there’s no serial character available, then nothing is done. 99.9% of the time there won’t be a character because serial characters arrive very slowly compared to the speed of the Arduino loop.

YEs the code has a problem, I’ve trying to mix all sort of codes but i cannot simultaneously run reading serial input, then run the motor and stop the motor when the number of ticket has been released, then reset the whole code. i can run the code seperately but i cant seem to run them together and run in rythym.

Do you think it might be helpful to describe what you want the code to do and how that compares with what it is doing now? I'll posit that the code is running just like you wrote it and if you want something different to happen then you have to write it differently. But without knowing what those two things are it is hard to tell you what to do.

this is how it goes

From my vb code, it send a character,

Which will then be read by my arduino,

The duino will now dispense ticket,

Then the duino will count the number of ticket dispense base on the set value,

like if i send character 'a' then it will dispense 5 tickets, and it should stop after dispensing when the count reaches 5. But counting seems not working inside switch code.

My serial sender is a VB code--
Using an arduino MEGA--
Using Opto as counter for the tickets--

If you send 'a' that code won't do squat. The only character it responds to as written is '1'. And if the button isn't pressed right when the '1' is received then you're out of luck because next time through loop you'll read a new character and it won't be '1' anymore.

And you won't count button presses here since for each one you'll have to send another '1' to get you into that case.

Maybe this will help, go through and take out the comments from the example code you copied from and comment it back with what's really happening in your project. That might help you see it. What you need is more like a state machine.

const int in_1 = 8 ;
const int in_2 = 9 ;
const int ledPin = 13;
const int opto_signal_pin = 2; // opto-sensor pin# (blue wire on DL-1275)
const int dispenser_motor = 10; // motor control pin# (white wire on DL-1275)

int opto_signal_status = 0; // opto-sensor LOW / HIGH status
int current_status = 0; // current state machine status
int previous_status = 0; // last state machine status

void setup() {
// Serial.begin(9600);
pinMode(opto_signal_pin, INPUT_PULLUP);
pinMode(dispenser_motor, OUTPUT);

pinMode(in_1, OUTPUT) ;
pinMode(in_2, OUTPUT) ;
////////////////
pinMode(ledPin, OUTPUT);
opto_signal_status = digitalRead(opto_signal_pin);
Serial.begin(9600);

}

void(* resetFunc) (void) = 0; // reset program function
void loop() {
if (Serial.available() > 0) {
int inByte = Serial.read();
int speed; // Local variable

int tickets_dispensed = 0;
int setTime = 1;
int no_ticket_reset = 0;
int last_opto_signal = 0;
int now;
int current_time = 0;
switch (inByte) {

case ‘#’ :
int tickets_requested = 2;
if (tickets_requested > 0) {

previous_status = digitalRead(opto_signal_pin);
motorOn();
for (; tickets_dispensed < tickets_requested;) {

// 1 = HIGH = no ticket gap detected
// 0 = LOW = ticket gap detected
opto_signal_status = digitalRead(opto_signal_pin);

// state machine conditions
if (opto_signal_status == 1) {
current_status = 1;
previous_status = 1;
}

if (opto_signal_status == 0) {
current_status = 0;
}

if (current_status == 0 && previous_status == 1) {
tickets_dispensed++;
last_opto_signal = now;
previous_status = 0;

if (tickets_dispensed == tickets_requested) {

motorOff();
break;
}

}

current_time = now;
int no_ticket_reset = current_time - last_opto_signal;
// reset if no ticket has ben detected in over 3 seconds
if (no_ticket_reset >= 3) {
hardReset();
}

}

hardReset(); // restet
}

// if * is pressed, reset
case ‘*’ :
hardReset(); // reset
}

}

}

void motorOn() {
digitalWrite(dispenser_motor, HIGH);
digitalWrite(ledPin, HIGH);
}

void motorOff() {
delay(100);
digitalWrite(dispenser_motor, LOW);
digitalWrite(ledPin, LOW);
}

void hardReset() {
motorOff();
delay(100);
resetFunc();
}

now this is working, the only problem is how can i declare the value of ticket requested to each case, so the number of tickets is different with each case, Thanks for the advise DELTA, I am reading, allot of them I guess thats why my brain Got scrambled.

const int in_1 = 8 ;
const int in_2 = 9 ;
const int ledPin = 13;
const int opto_signal_pin = 2; // opto-sensor pin# (blue wire on DL-1275)
const int dispenser_motor = 10; // motor control pin# (white wire on DL-1275)

int opto_signal_status = 0; // opto-sensor LOW / HIGH status
int current_status = 0; // current state machine status
int previous_status = 0; // last state machine status

void setup() {
// Serial.begin(9600);
pinMode(opto_signal_pin, INPUT_PULLUP);
pinMode(dispenser_motor, OUTPUT);

pinMode(in_1, OUTPUT) ;
pinMode(in_2, OUTPUT) ;
////////////////
pinMode(ledPin, OUTPUT);
opto_signal_status = digitalRead(opto_signal_pin);
Serial.begin(9600);

}

void(* resetFunc) (void) = 0; // reset program function
void loop() {
if (Serial.available() > 0) {
int inByte = Serial.read();
int speed; // Local variable

int tickets_dispensed = 0;
int setTime = 1;
int no_ticket_reset = 0;
int last_opto_signal = 0;
int now;
int current_time = 0;
int x = 1;
switch (inByte) {

case ‘#’ :
if (x = 1)
{
int tickets_requested = 2;
if (tickets_requested > 0) {

previous_status = digitalRead(opto_signal_pin);
motorOn();
for (; tickets_dispensed < tickets_requested;) {

// 1 = HIGH = no ticket gap detected
// 0 = LOW = ticket gap detected
opto_signal_status = digitalRead(opto_signal_pin);

// state machine conditions
if (opto_signal_status == 1) {
current_status = 1;
previous_status = 1;
}

if (opto_signal_status == 0) {
current_status = 0;
}

if (current_status == 0 && previous_status == 1) {
tickets_dispensed++;
last_opto_signal = now;
previous_status = 0;

if (tickets_dispensed == tickets_requested) {

motorOff();
break;
}

}

current_time = now;
int no_ticket_reset = current_time - last_opto_signal;
// reset if no ticket has ben detected in over 3 seconds
if (no_ticket_reset >= 3) {
hardReset();
}

}

hardReset(); // restet
}
}
// if * is pressed, reset
case ‘*’ :
hardReset(); // reset
}

}

}

void motorOn() {
digitalWrite(dispenser_motor, HIGH);
digitalWrite(ledPin, HIGH);
}

void motorOff() {
delay(100);
digitalWrite(dispenser_motor, LOW);
digitalWrite(ledPin, LOW);
}

void hardReset() {
motorOff();
delay(100);
resetFunc();
}

Well, what a luck, i manage to solve it by inserting it inside to a always true if condition. ahaha Thanks DELTA, I’ll be doing the rest of the codes.

Folks get testy when you continue to post sans code tags after having been advised to use them.

Just sayin'.

I don't get testy. I just assume that if they're not smart enough to handle code tags with simple instructions then they probably aren't smart enough to write code. So I move on to someone who actually wants some help. It's ok, lots of people aren't smart enough for this stuff.