I have a motor that should start when an input switch is pulsed high, then the motor should run until f.eks. 4 pulses from the cam sensor has occurred, then the motor should stop.
It is for a coin game, when a coin passes a switch in a win slot, then the motor should start and pay out f.eks. 4 coins, one cam pulse for each coin. the coin switch will be on only for a short time, when the coin passes the switch.
This is what i have done so far with the code, i am new to this, so i need a little help.
const int kr2 = 3;
const int kr3 = 4;
const int kr4 = 5;
const int kr7 = 6;
const int kam = 2;
int motor = 7;
int gteller = 8;
// setter verdien som skal trekkes fra av kam ved gevinst
int kr2value = 2;
int kr3value = 3;
int kr4value = 4;
int kr7value = 7;
int motorstatus = 0;
void setup() {
// put your setup code here, to run once:
pinMode(motor, OUTPUT);
// digitalWrite(motor, LOW);
pinMode(gteller, OUTPUT);
// digitalWrite(gteller, LOW);
pinMode(kr2, INPUT);
pinMode(kr3, INPUT);
pinMode(kr4, INPUT);
pinMode(kr7, INPUT);
pinMode(kam, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(kr2) == HIGH){
motorstatus = 1;
if (motorstatus =1);
digitalWrite(motor, HIGH);
}
if (digitalRead(kam) == HIGH){
(kr2value) = (kr2value - 1);
// digitalWrite(gteller, HIGH);
} else { (kr2value = kr2value);
// digitalWrite(gteller, LOW);
if (kr2value = 0);{
(motorstatus = 0);
}
if (motorstatus = 0);
digitalWrite(motor, LOW);
}