Getting back into Arduino, don't know why this code doesn't work.

Hello everyone. I used to be quite active here a few years back, and then school got more difficult and I had to take a break from Arduino. Anyways, I had some free time and I needed to make a quick project. Below is the code I wrote, and I can’t figure out what I did wrong.

const int motor = 13;
const int sensor = 12;
const int button = 11;

int bstate;
int sstate;
int psstate;
int count;
int pcount;

void setup() {
  pinMode(motor, OUTPUT);
  pinMode(sensor, INPUT);
  pinMode(button, INPUT);
  
  digitalWrite(motor, LOW);
  
  count = 0;
  pcount = 0;
}

void loop() {
  if(motor == LOW){
    bstate = digitalRead(button);
  }
  
  sstate = digitalRead(sensor);
  
  if(bstate == HIGH && count < 50) {
    digitalWrite (motor, HIGH);
    if(sstate == LOW && psstate == HIGH){
      count = pcount + 1;
    }
    else{
    }  
  }
  else if(count == 50){
    digitalWrite(motor, LOW);
    count = 0;
  }
  pcount = count;
  psstate = sstate;
}

Some comments would be nice.

You mean as far as what the code is supposed to do? I'm basically building a machine to count out 50 coins and then stop.

The "button" is the trigger that a person would press, and once it is pressed, the machine needs to dispense 50 coins. The "motor" is the motor in the coin dispenser, and the "sensor" is basically an IR transmitter and receiver that is blocked by the passing coins, and can be used to count the coins.

I don't want to tell you how to write your program but I will point out that if you put Serial.print debug statements throughout your program you will be able to isolate the part that doesn't work, probably before anyone has replied to your post. I'm a hardware person so the only thing I could say is if your button has the proper pullup resistor and you have a meter, it shouldn't take very long to find the problem by commenting out all but one section and testing it one section at a time. If you can't read a button then you can forget about the money...if you know what I mean. Maybe the problem is in the counting code which would be easy to debug with serial print statements. It's up to you but that' the way I woiuld go.

I tried that at one point, and the thing that confused me is the readout in Serial Print is a 1 or 0, not high or low. Should I be using 1 and 0 in my variables instead?

No. If you know which of the two is HIGH and which is LOW then you should be able to debug using the 0's and 1's. How is the switch (button) wired (exactly) ?

raschemmel: No. If you know which of the two is HIGH and which is LOW then you should be able to debug using the 0's and 1's. How is the switch (button) wired (exactly) ?

The button is just connected to 5v on one side and the input pin on the other. I also have a pull down resistor on the input pin side.

I also have a pull down resistor on the input pin side.

What value ?

 int psstate;

What does this mean ? (bstate=button, sstate=sensor, pstate= ?)

Here's your problem...

  if(motor == LOW){

motor is a constant, it is equal to 13 and will never equal LOW.

psstate = previous sensor state, because I have to compare what the sensor is NOW compared to what it was on the previous loop.

And I think david is right. I made a new variable to store the state of the motor (1 or 0), and it seems to be working fine now. The weird thing is it works when plugged into my computer, but not when plugged into my wall wart.

If your intent was to see if the motor was on then

digitalWrite (motor, HIGH);

If you had another digital pin setup as an input and did a

int mstate;
mstate =digitalRead(motor);

Then you could test that pin for high or low but it makes more sense to use a boolean flag that you set and clear whenever you change the motor state.

What works ?

Here’s the working code. I used the variable “mval” to control the motor status instead of “high” or “low”.

int motor = 13;
int sensor = 12;
int button = 11;

int buttonstate;
int sensorstate;
int prevsensorstate;
int count;
int prevcount;
int mval;

void setup() {
  pinMode(motor, OUTPUT);
  pinMode(sensor, INPUT);
  pinMode(button, INPUT);
  
  mval = 0;
  digitalWrite(motor, mval);
  
  count = 0;
  prevcount = 0;
}

void loop() {
  if(mval == 0){
    buttonstate = digitalRead(button);
  }
  
  sensorstate = digitalRead(sensor);
  
  if(buttonstate == 1 && count < 50) {
    mval = 1;
    digitalWrite (motor, mval);
    if(sensorstate == 0 && prevsensorstate == 1){
      count = prevcount + 1;
    }  
  }
  else if(count == 50){
    if(sensorstate == 1){
      mval = 0;
      digitalWrite(motor, mval);
      count = 0;
    }
  }
  prevcount = count;
  prevsensorstate = sensorstate;
  delay(10);
}

The weird thing is it works when plugged into my computer, but not when plugged into my wall wart.

How are you driving the motor ? Is it just a transistor ?

Right now the motor is actually simulated by an LED (with a resistor of course). On the coin hopper I bought, there is apparently a trigger pin, which only takes a very small amperage. I think the hopper might have its own relay or transistor inside.