Code running but not accepting switch inputs (candy dispenser)

Hello!

Building a single button operated candy dispenser for Halloween (think motorized gochapon machine), unfortunately the process isn't going as smoothly as originally anticipated.

First time using an Arduino and running into a problem - the code I wrote starts but only seems to be executing the first couple lines, the on-board LED flashes as instructed but the other LED doesn't light/the two microswitch inputs have no effect/motor does not turn.

/* Candy ball dispenser machine V1
 *  Last code update/flash 8_23_2022
 */

const int ledPin = 9;
//assigning the pin for the LED in arcade button
const int arcbuttonPin = 10;
//assigning the pin for the arcade button switch
const int rollswitchPin = 8;
//assigning the pin for the turret position indicator switch
const int msigaPin = 2;
//IN1 on L298N board
const int msigbPin = 3;
//IN2 on L298N board
int rollswitchState = 0;
int arcbuttonState = 0;

void setup() {
  // put your setup code here, to run once:

pinMode(13, OUTPUT);
  
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(rollswitchPin, INPUT);
  // initialize the pushbutton pin as an input:
  pinMode(arcbuttonPin, INPUT);
  pinMode(msigaPin,OUTPUT);
  pinMode(msigbPin,OUTPUT);

rollswitchState = digitalRead(rollswitchPin);
arcbuttonState = digitalRead(arcbuttonPin);

digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
  
do {
  /* Reset turret to start position */
 digitalWrite(msigaPin,LOW);
 digitalWrite(msigbPin,HIGH);
} while (rollswitchState,HIGH);

if (rollswitchState,LOW){
  digitalWrite(ledPin,HIGH);
  delay(75);
  digitalWrite(ledPin,LOW);
  delay(75);
  digitalWrite(ledPin,HIGH);
  delay(75);
  //when button is in place flash led on then off, then maintain on

}

}

void loop() {
  // put your main code here, to run repeatedly:

rollswitchState = digitalRead(rollswitchPin);
arcbuttonState = digitalRead(arcbuttonPin);

if (arcbuttonState,HIGH) {
  digitalWrite(ledPin,LOW);
  
  digitalWrite(msigaPin,HIGH);
  digitalWrite(msigbPin,LOW);
  
  delay(75);
  
  do  {
    digitalWrite(msigaPin,LOW);
    digitalWrite(msigbPin,HIGH);
  } while (rollswitchState,HIGH);
  
  delay(1000);

  digitalWrite(ledPin,HIGH);
  
}

else digitalWrite(ledPin,HIGH); 

}

/* Intended function:
Initiate:

If turret out of place reverse until in place
When in place flash arcade button led twice

Start fuctional loop ->
If button pressed turn light off, start motor
delay .75s (75ms)
when stop switch reengages stop motor
delay 10s (1000ms)
turn light back on
*/

I've adjusted the code and physical version of the circuit to match the inputs auto assigned by the circuito.io diagram to see if that was the problem but that didn't help at all. Had tried to make a mechanical only version with a latching relay but due to the safety delay after the motion is finished that wasn't going to work.

If anyone has a recommendation of what to adjust it would be very helpful, it's been a few years since writing code of any kind so I may be missing something obvious.

Thanks,
Rp

This:

  if (arcbuttonState, HIGH)

Should be:

  if (arcbuttonState == HIGH)

Same issue in the while too.

1 Like
while (rollswitchState,HIGH);

This is not the way to test the value of the rollswitchState variable, assuming that is what you are trying to do

while (rollswitchState == HIGH)

is the correct syntax

1 Like

Thank you, that was definitely going to be a hiccup. Had an error earlier on due to misreading how to write digitalwrite statements and must have swapped those in addition to the others.

I am trying to test the value, thank you for the explanation. I've switched those over but there doesn't seem to be an effect on the current lack of doing anything the system unfortunately.

It looks like you are trying to power the motor from the Arduino. The usual method is that the Arduino controls the motor driver but power is provided by a separate power supply.

1 Like

If you have made changes to the code then please post the revised sketch in a new post so that we can see it in its current state

1 Like

One of the sites I had looked at used the Vin port to power the motor board, would that not work as a bypass to power the separate board? Unfortunately the second 12v power supply I had walked off so I've been attempting to find a work around for that.

Updated code:

/* Candy ball dispenser machine V1
 *  Last code update/flash 8_23_2022
 */

const int ledPin = 11;
//assigning the pin for the LED in arcade button
const int arcbuttonPin = 10;
//assigning the pin for the arcade button switch
const int rollswitchPin = 8;
//assigning the pin for the turret position indicator switch
const int msigaPin = 2;
//IN1 on L298N board
const int msigbPin = 3;
//IN2 on L298N board
int rollswitchState = 0;
int arcbuttonState = 0;

void setup() {
  // put your setup code here, to run once:

pinMode(13, OUTPUT);
  
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(rollswitchPin, INPUT);
  // initialize the pushbutton pin as an input:
  pinMode(arcbuttonPin, INPUT);
  pinMode(msigaPin,OUTPUT);
  pinMode(msigbPin,OUTPUT);

rollswitchState = digitalRead(rollswitchPin);
arcbuttonState = digitalRead(arcbuttonPin);

digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);

digitalWrite(11, HIGH);
delay(200);
digitalWrite(11, LOW);
delay(200);
digitalWrite(11, HIGH);
delay(200);
digitalWrite(11, LOW);
  
do {
  /* Reset turret to start position */
 digitalWrite(msigaPin,LOW);
 digitalWrite(msigbPin,HIGH);
} while (rollswitchState == HIGH);

if (rollswitchState == LOW){
  digitalWrite(ledPin,HIGH);
  delay(75);
  digitalWrite(ledPin,LOW);
  delay(75);
  digitalWrite(ledPin,HIGH);
  delay(75);
  //when button is in place flash led on then off, then maintain on

}

}

void loop() {
  // put your main code here, to run repeatedly:

rollswitchState = digitalRead(rollswitchPin);
arcbuttonState = digitalRead(arcbuttonPin);

if (arcbuttonState == HIGH) {
  digitalWrite(ledPin,LOW);
  
  digitalWrite(msigaPin,HIGH);
  digitalWrite(msigbPin,LOW);
  
  delay(75);
  
  do  {
    digitalWrite(msigaPin,LOW);
    digitalWrite(msigbPin,HIGH);
  } while (rollswitchState == HIGH);
  
  delay(10000);

  digitalWrite(ledPin,HIGH);
  
}

else digitalWrite(ledPin,HIGH); 

}

/* Intended function:
Initiate:

If turret out of place reverse until in place
When in place flash arcade button led twice

Start fuctional loop ->
If button pressed turn light off, start motor
delay .75s (75ms)
when stop switch reengages stop motor
delay 10s (10000ms)
turn light back on
*/

Secondary LED now turns on, with what Wildbill said it may be that there needs to be a second power supply to run just the l298n board.

It depends on what current the motor pulls. Anything other than a tiny motor is likely to need more than the Arduino can provide. If you're powering through the USB, you've got 500mA to play with. The barrel jack uses the on board regulator which can manage 150mA. I am not sure what the story is in your configuration where both appear to be hooked up.

The L298 motor driver board will cost you power too and drop your voltage by a couple. It may explain why nothing is happening. See if you can find where your other power supply walked off to, or scour the house for unused wall warts.

1 Like

That make sense, forgot about the amperage drop. I've been using an external wall power supply to make sure I have the same setup as when the device gets put into use. Time to go hunting for another power supply.

Update:
Attached a second power supply and now the lights function and the motors start, unfortunately the motors won't stop running once started. There seems to be something up where the While loops don't exit when they should, I'm not 100% sure how to have the code recheck if the button state has changed since the initialization of the code. Tried looking it up but the use cases seemed different and the errors people had seemed to originate from things other than the while loop itself. Attached is the code as it stands now, if anyone has any ideas on why the l298n doesn't stop/the while loop doesn't break it would be greatly appreciated.

/* Candy ball dispenser machine V1
 *  Last code update/flash 8_24_2022
 */

const int ledPin = 11;
//assigning the pin for the LED in arcade button
const int arcbuttonPin = 10;
//assigning the pin for the arcade button switch
const int rollswitchPin = 9;
//assigning the pin for the turret position indicator switch
const int msigaPin = 4;
//IN1 on L298N board
const int msigbPin = 3;
//IN2 on L298N board
const int ENA = 5;
//gettinng really tired of the L298N extra inputs
int rollswitchState = 0;
int arcbuttonState = 0;

void setup() {
  // put your setup code here, to run once:
  
Serial.begin(9600);
  
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(rollswitchPin, INPUT);
  // initialize the pushbutton pin as an input:
  pinMode(arcbuttonPin, INPUT);
  pinMode(msigaPin,OUTPUT);
  pinMode(msigbPin,OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(13, OUTPUT);
  //Onboard LED

int rollswitchState = digitalRead(rollswitchPin);
int arcbuttonState = digitalRead(arcbuttonPin);
analogWrite(ENA, 255);

digitalWrite(13, HIGH);
delay(150);
digitalWrite(13, LOW);
delay(150);
digitalWrite(13, HIGH);
delay(150);
digitalWrite(13, LOW);
//Flash onboard led

digitalWrite(11, HIGH);
delay(200);
digitalWrite(11, LOW);
delay(200);
digitalWrite(11, HIGH);
delay(200);
digitalWrite(11, LOW);
//Flash external led
 
 while (rollswitchState == HIGH){
//Reset turret to start position
 digitalWrite(msigaPin,LOW);
 digitalWrite(msigbPin,HIGH);
 analogWrite(ENA, 75);
 digitalRead(rollswitchPin);
 if (rollswitchState != HIGH){
 break;
 }
 ;
}
;

if (rollswitchState == LOW){
  delay(2000);
  digitalWrite(ledPin,HIGH);
  delay(750);
  digitalWrite(ledPin,LOW);
  delay(100);
  digitalWrite(ledPin,HIGH);
  delay(750);
  digitalWrite(ledPin,LOW);
  delay(100);
  digitalWrite(ledPin,HIGH);
//when button is in place flash led on then off, then maintain on

}

}

void loop() {
  // put your main code here, to run repeatedly:

rollswitchState = digitalRead(rollswitchPin);
arcbuttonState = digitalRead(arcbuttonPin);

if (arcbuttonState == HIGH) {
  
  digitalWrite(ledPin,LOW);
  
  digitalWrite(msigaPin,HIGH);
  digitalWrite(msigbPin,LOW);
  analogWrite(ENA, 200);
  
  delay(300);
  
   while (rollswitchState == HIGH)
   {
    digitalWrite(msigaPin,HIGH);
    digitalWrite(msigbPin,LOW);
    analogWrite(ENA, 100);
   }
  ;
  
  delay(10000);

  digitalWrite(ledPin,HIGH);
  delay(150);
  digitalWrite(ledPin,LOW);
  delay(150);
  digitalWrite(ledPin,HIGH);
  
}

else digitalWrite(ledPin,HIGH); 

}

/* Intended function:
Initiate:

If turret out of place reverse until in place
When in place flash arcade button led twice

Start fuctional loop ->
If button pressed turn light off, start motor
delay .3s (300ms)
when stop switch reengages stop motor
delay 10s (10000ms)
turn light back on
*/
type or paste code here

Here:

    while (rollswitchState == HIGH)

If rollswitchState is HIGH, that while loop runs forever. You will need to digitalRead the pin, either in the while clause itself or in the loop that follows.

Adding a digitalRead in the end of the while loop doesn't seem to have an effect, does there need to be another variable or is the code able to rewrite the state of the button in the loop? The main loop is behaving oddly to where the motor will constantly run if the start button (arcbutton) is pressed only if the turn checking switch is also pressed (rollswitch). If the start button (arcbutton) is pressed without the other switch already being pressed the motor blips on then off again.

Post your latest version.

/* Candy ball dispenser machine V1
 *  Last code update/flash 8_24_2022
 */

const int ledPin = 11;
//assigning the pin for the LED in arcade button
const int arcbuttonPin = 10;
//assigning the pin for the arcade button switch
const int rollswitchPin = 9;
//assigning the pin for the turret position indicator switch
const int msigaPin = 4;
//IN1 on L298N board
const int msigbPin = 5;
//IN2 on L298N board
const int ENA = 3;
//gettinng really tired of the L298N extra inputs
int rollswitchState = 0;
int arcbuttonState = 0;

void setup() {
  // put your setup code here, to run once:
  
Serial.begin(9600);
  
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(rollswitchPin, INPUT);
  // initialize the pushbutton pin as an input:
  pinMode(arcbuttonPin, INPUT);
  pinMode(msigaPin,OUTPUT);
  pinMode(msigbPin,OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(13, OUTPUT);
  //Onboard LED

  int rollswitchState = digitalRead(rollswitchPin);
  int arcbuttonState = digitalRead(arcbuttonPin);
  analogWrite(ENA, 255);

  digitalWrite(13, HIGH);
  delay(150);
  digitalWrite(13, LOW);
  delay(150);
  digitalWrite(13, HIGH);
  delay(150);
  digitalWrite(13, LOW);
  //Flash onboard led

  digitalWrite(11, HIGH);
  delay(200);
  digitalWrite(11, LOW);
  delay(200);
  digitalWrite(11, HIGH);
  delay(200);
   digitalWrite(11, LOW);
  //Flash external led
 
 while (rollswitchState == HIGH){
    //Reset turret to start position
    digitalWrite(msigaPin,LOW);
    digitalWrite(msigbPin,HIGH);
    analogWrite(ENA, 75);
    digitalRead(rollswitchPin);
      if (rollswitchState != HIGH){
      break;
      }
      ;
   }
;

  if (rollswitchState == LOW){
   delay(2000);
   digitalWrite(ledPin,HIGH);
   delay(750);
   digitalWrite(ledPin,LOW);
   delay(100);
   digitalWrite(ledPin,HIGH);
   delay(750);
   digitalWrite(ledPin,LOW);
   delay(100);
   digitalWrite(ledPin,HIGH);
   //when button is in place flash led on then off, then maintain on

  }

}

void loop() {
  // put your main code here, to run repeatedly:

    int rollswitchState = digitalRead(rollswitchPin);
    int arcbuttonState = digitalRead(arcbuttonPin);

    if (arcbuttonState == HIGH) {
  
      digitalWrite(ledPin,LOW);
  
      digitalWrite(msigaPin,HIGH);
      digitalWrite(msigbPin,LOW);
      analogWrite(ENA, 200);
  
      delay(500);
  
      while(rollswitchState == HIGH)
      {
        digitalWrite(msigaPin,HIGH);
        digitalWrite(msigbPin,LOW);
        analogWrite(ENA, 0);
        delay(200);
        digitalRead(rollswitchPin);
      }
      ;
  
     delay(10000);

     digitalWrite(ledPin,HIGH);
     delay(150);
    digitalWrite(ledPin,LOW);
    delay(150);
    digitalWrite(ledPin,HIGH);
  
  }

  else digitalWrite(ledPin,HIGH); 

}

I don't believe anything major has changed. The attempt to re read the button states doesn't seem to have an effect but I may have written it incorrectly - apologies for being rather lost, I haven't written anything in arduino before

This:

    while (rollswitchState == HIGH)
    {
      digitalWrite(msigaPin, HIGH);
      digitalWrite(msigbPin, LOW);
      analogWrite(ENA, 0);
      delay(200);
      digitalRead(rollswitchPin);
    }

Should be:

    while (rollswitchState == HIGH)
    {
      digitalWrite(msigaPin, HIGH);
      digitalWrite(msigbPin, LOW);
      analogWrite(ENA, 0);
      delay(200);
      rollswitchState =digitalRead(rollswitchPin);
    }
1 Like

Update! It works! At least out of the machine the circuit seems to be performing as intended. Huge thanks to Wildbill for putting up with my lack of knowledge. The functional code is as follows:

/* Candy ball dispenser machine V1
 *  Last code update/flash 8_24_2022
 */

const int ledPin = 11;
//assigning the pin for the LED in arcade button
const int arcbuttonPin = 10;
//assigning the pin for the arcade button switch
const int rollswitchPin = 9;
//assigning the pin for the turret position indicator switch
const int msigaPin = 4;
//IN1 on L298N board
const int msigbPin = 5;
//IN2 on L298N board
const int ENA = 3;
//gettinng really tired of the L298N extra inputs
int rollswitchState = 0;
int arcbuttonState = 0;

void setup() {
  // put your setup code here, to run once:
  
Serial.begin(9600);
  
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(rollswitchPin, INPUT);
  // initialize the pushbutton pin as an input:
  pinMode(arcbuttonPin, INPUT);
  pinMode(msigaPin,OUTPUT);
  pinMode(msigbPin,OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(13, OUTPUT);
  //Onboard LED

  int rollswitchState = digitalRead(rollswitchPin);
  int arcbuttonState = digitalRead(arcbuttonPin);
  analogWrite(ENA, 255);

  digitalWrite(13, HIGH);
  delay(150);
  digitalWrite(13, LOW);
  delay(150);
  digitalWrite(13, HIGH);
  delay(150);
  digitalWrite(13, LOW);
  //Flash onboard led

  digitalWrite(11, HIGH);
  delay(200);
  digitalWrite(11, LOW);
  delay(200);
  digitalWrite(11, HIGH);
  delay(200);
   digitalWrite(11, LOW);
  //Flash external led
 
 while (rollswitchState == HIGH){
    //Reset turret to start position
    digitalWrite(msigaPin,LOW);
    digitalWrite(msigbPin,HIGH);
    analogWrite(ENA, 75);
    rollswitchState = digitalRead(rollswitchPin);
      if (rollswitchState != HIGH){
      digitalWrite(msigaPin,LOW);
      digitalWrite(msigbPin,LOW);
      analogWrite(ENA, 0);
      break;
      }
      ;
   }
;

  if (rollswitchState == LOW){
   delay(2000);
   digitalWrite(ledPin,HIGH);
   delay(750);
   digitalWrite(ledPin,LOW);
   delay(100);
   digitalWrite(ledPin,HIGH);
   delay(750);
   digitalWrite(ledPin,LOW);
   delay(100);
   digitalWrite(ledPin,HIGH);
   //when button is in place flash led on then off, then maintain on

  }

}

void loop() {
  // put your main code here, to run repeatedly:

    int rollswitchState = digitalRead(rollswitchPin);
    int arcbuttonState = digitalRead(arcbuttonPin);

    if (arcbuttonState == HIGH) {
  
      digitalWrite(ledPin,LOW);
      //turn off led once button pushed
      
      digitalWrite(msigaPin,HIGH);
      digitalWrite(msigbPin,LOW);
      analogWrite(ENA, 200);
      //start motor
      delay(200);
      //motor needs time to start moving mechanism
      
      rollswitchState = digitalRead(rollswitchPin);
      while(rollswitchState == HIGH)
      {
        digitalWrite(msigaPin,HIGH);
        digitalWrite(msigbPin,LOW);
        analogWrite(ENA,200);
        delay(100);
        rollswitchState = digitalRead(rollswitchPin);
          if (rollswitchState == LOW){
          digitalWrite(msigaPin,LOW);
          digitalWrite(msigbPin,LOW);
          analogWrite(ENA, 0);
          break;
        }
      }
      ;
  
     delay(10000);

     digitalWrite(ledPin,HIGH);
     delay(150);
     digitalWrite(ledPin,LOW);
     delay(150);
     digitalWrite(ledPin,HIGH);
  
  }

  else digitalWrite(ledPin,HIGH); 

}

If clicking solved doesn't lock out the post I'll try to add photos/a video of the machine working once it's assembled.

As you can see, it doesn't :wink:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.