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
