Motor with cam switch

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);
    }

Welcome to the forum

Do you have a question ?

What are you using for a cam sensor? How fast does the cam turn? Approximately how long are the pulses (in milliseconds)?

about 2-3 pulses per second is the motor speed. In the senter of the motor is a square knob whit a micro switch that reads the 4 corners of the knob.

Ok, your input switch is normally at ground. How is the cam switch wired?

I'm in transit looking through the tiny window and I see a two-fer here.

You probably want comparison == not assignment = in you if statement expressions, and you probably don't mean to finish the if statement off with an empty body.

Which is that semicolon in there. That is syntactically the end of the if.

This, probably:

  if (kr2value == 0) {

In the IDE preferences, dial up the verbosity and warnings, you woukd have been alerted, I think, to both those mistakes which are not errors.

HTH

a7

Post a diagram showing how everything is wired to every other thing.

i made a new topic that includes schematic and a code that is somewhat working

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