Controlling a motor for a project

Hey,

I am working on a university project where I need a motor to turn in one direction for a certain amount of time when triggered and then again in the other direction when triggered the same way.

The motor is hooked up too an H-bridge, so it turns in one direction when pin5 is LOW and pin6 is HIGH and in the other one vice versa.

Here is my code so far, the trigger is a capacity sensor that will give a certain frequeny (using a library):

#include <FreqMeasure.h>  //include the FreqMeasure library

void setup() {
  Serial.begin(57600);
  FreqMeasure.begin();
  
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
}

double sum=0;
int count=0;
int extracted=0;
int frequency;

void loop() {
  if (FreqMeasure.available()) // average several readings together
  { 
    sum = sum + FreqMeasure.read();
    count = count + 1;
    if (count > 30) 
    {
      frequency = FreqMeasure.countToFrequency(sum / count);
      Serial.println(frequency);
      sum = 0;
      count = 0;
     }
    } 
  if (frequency < 480.00 && extracted == 0) //if streched
    {
      digitalWrite(5,LOW); //power motor
      delay(4000); //time motor will run
      digitalWrite(5,HIGH);
      extracted=1;
    }
    else
    {
      digitalWrite(5,HIGH);
      digitalWrite(6,HIGH);
     }
     
  if (frequency < 480.00 && extracted == 1) //if not streched
      {
        digitalWrite(6,LOW); //motor turns other direction
        delay(4000);
        digitalWrite(6,HIGH);
        extracted=0;
      }
     else
    {
      digitalWrite(5,HIGH);
      digitalWrite(6,HIGH);
     }
   
  }

With this code the motor just spins with alternating directions for ever, ignoring the sensor and the frequency does not get printed on the serial monitor anymore. My idea was to set an "extracted" state to prevent the motor from running forever even if the thing he should extract is already extracted, but it doesnt seem to work.

Is there a mistake I have missed? Or is there a command I could use that I have not heard of?

I have little experience with arduino (and electronics in general to be fair) and everything I have done so far is stuff I looked up and learned during this project, so dont expect a lot of expertease from me, if you have questions I will do my best! :wink:

Thanks in advance!
-Lythor

Can post the whole code?

That IS the whole code^^

When u power on the motor .. you should write data to both of the pins...
digitalWrite(5,LOW) ;
digitalWrite(6,HIGH);
...
that the first thing i see that doesn't look right..

I see that you set extracted=1; at the end of the first IF which means that the second IF runs also - which may not be what you want.

And there is the same issue at the end of the second IF.

...R

I tried to set that variable to prevent the motor from spinning when the piston I use is already extracted...
Maybe I need an additional sensor to see if it is extracted or not, but I´d like to do it within the programm somehow. So when it extracts, it sets "extracted" to "true/1" and when the frequency triggers again it retracts and sets it to "false/0".

Lythor:
I tried to set that variable to prevent the motor from spinning when the piston I use is already extracted...

Your comment suggests that you do not see the point I was trying to make - that you seem to be setting the variable in the wrong place - or at least in the wrong place relative to the subsequent code.

I suggest you work through the code line by line with a pencil and paper as though your brain is the Arduino.

It is also a good idea to write down the project logic in plain language (not code) as a series of steps with one step on each line. See planning and implementing a program.

And, another thought ...

You have this IF
if (frequency < 480.00 && extracted == 0) //if streched
followed by
else

When is the else supposed to happen?
It will happen if frequency >= 480 regardless of the value of extracted
It will also happen if extracted != 0 regardless of the value of frequency

Is that what you want?

Maybe you should have something like this (but keep in mind I don't know what you are trying to achieve)

if (frequency < 480) {
  if (extracted == 0) {
     // do stuff
  }
  else {  // extracted != 0
     // do other stuff
  }
}
else (  // frquency >= 480
  // do stuff
}

...R

Okay

first of all thanks for the help :wink: I am not very competent with programming, so please dont be annoyed when I dont get everything you say^^

I know changed it into this:

void loop() {
  if (FreqMeasure.available()) // average several readings together
  { 
    sum = sum + FreqMeasure.read(); //calculate frequency measured on pin 8
    count = count + 1;
    if (count > 30)
    {
      frequency = FreqMeasure.countToFrequency(sum / count);
      Serial.println(frequency);
      sum = 0;
      count = 0;
       if (frequency < 480.00) //if streched
    {
      if (extracted == 0) {
        digitalWrite(5,LOW); //power motor
        digitalWrite(6,HIGH);
        delay(4000); //time motor will run
        digitalWrite(5,HIGH);
        extracted=1;
      }
      if (extracted == 1) {
        digitalWrite(6,LOW);
        digitalWrite(5,HIGH);
        delay(4000);
        digitalWrite(6,HIGH);
        extracted=0;
      }
      }
   }
    } 

      }

The problem now is that when I trigger the sensor, it spins in both direction each time.
I want it to soin in the one direction when I trigger the first time and in the other one when I trigger the second time...

Okay, works fine with this now! :slight_smile:

#include <FreqMeasure.h>  //include the FreqMeasure library

void setup() {
  Serial.begin(57600);
  FreqMeasure.begin();
  
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
}

double sum=0;
int count=0;
int extracted=0;
int frequency;

void loop() {
  if (FreqMeasure.available()) // average several readings together
  { 
    sum = sum + FreqMeasure.read(); //calculate frequency measured on pin 8
    count = count + 1;
    if (count > 30)
    {
      frequency = FreqMeasure.countToFrequency(sum / count);
      Serial.println(frequency);
      sum = 0;
      count = 0;
       if (frequency < 500.00) //if streched
      {
        if (extracted == 0) {
          extracted=1;
          digitalWrite(5,LOW); //power motor
          digitalWrite(6,HIGH);
          delay(4000); //time motor will run
          digitalWrite(5,HIGH);
          }
        else {
          extracted=0;
          digitalWrite(6,LOW);
          digitalWrite(5,HIGH);
          delay(4000);
          digitalWrite(6,HIGH);
          }
       }
       else{
         digitalWrite(5,HIGH);
         digitalWrite(6,HIGH);
         }
       }  
    } 
      }

Thanks again for the help!

Split the code into several functions. One does the frequency measurement, and nothing else.

One drives the motor in a given direction for a given time, and nothing else.

Another calls these to implement your particular business logic - this is much more
abstract than the others and knows nothing of the details of the sensor or the H-bridge.

This will automatically make the code easier to read and mistakes will be both more
obvious and more obvious how to fix.

Separate concerns into separate functions whenever possible.