Go Down

Topic: if statement reading motor shield current monitoring pin (Read 771 times) previous topic - next topic


Anyone help please.

I'm trying to write code that will enable me to control a motor using stall current.  I have an UNO and motor shield R3. Here's my code i'm working on.  I'm trying to read the pin A1 and if the current is above.4amp i want to shut the motor down.

Code: [Select]
int Bbrake=8;//brake signal for motor B
int Abrake=9;//brake signal for motor A
int motorspdB=11;//PWM speed signal for motor B
int motorspdA=3;//PWM speed signal for motor A
int motorA=12;
int motorB=13;
//int limit=A0

void setup () {
  // int sensorValue=analogRead(A1);
  pinMode(A1,INPUT);//motor current monitor B
  pinMode(A0,INPUT);//motor current monitor A
  pinMode (13, OUTPUT);//motor B enable and direction HIGH or LOW
  pinMode (12, OUTPUT);//motor A enable and direction HIGH or LOW
  pinMode (8, OUTPUT);//Brake motor B
  pinMode (9, OUTPUT);//Brake motor A
//  int Current = 0; //Value of current
  void loop () {

  int sensorValue=analogRead(A1);
int limit =100; //Value that triggers motor to shutoff once it has reached endpoint
  analogWrite  (motorspdB,255);
  digitalWrite (motorB,HIGH);
  digitalWrite (Bbrake,LOW);
  if (sensorValue<limit);

  {digitalWrite (Bbrake,HIGH);
// digitalWrite (Bbrake,LOW);
// delay(5000);
  digitalWrite (motorB,LOW);
  digitalWrite (Bbrake,LOW);
  digitalWrite (Bbrake,HIGH);
  delay (1000);
//  Serial.println(sensorValue);

The motors running backwards and forwards for now but i want to develop the if statement to take into account current.


Code: [Select]
  if (sensorValue<limit);
How often do you suppose an if statement gets followed, meaningfully, be a semicolon? In reality, virtually never.

When you post code and have a statement like "I'm trying...", it's generally a good idea to then talk about what problems you are having. What does that code do? What do you want it to do? How do those two differ?
The art of getting good answers lies in asking good questions.


I'm very new to Arduino and programming, sorry if my post was vague.  I'm building a Replica large scale lightning and am trying to design some lightweight retractable undercarriage.  My previous tests with jets have led to them disintegrating in flight due to lose items entering the airstream at speed.  I want to use secure screwjack undercarriage retracts.  I have engineered the mechanical side of the undercarriage but now need to control the motor driving the gear up and down. 

I've trawled the net and found various older topics on Arduino and retracts and have been trying to utilise the code in these posts but i could do with some simple pointers. 

I can get the motor (just one connected at the moment) to run and brake and reverse, but i'm uncertain of the code i need to make to motor to stop at the end of it's travel.  I was hoping you might correct my if statement i know it's not right but can't see where i've gone wrong.


I was hoping you might correct my if statement i know it's not right but can't see where i've gone wrong.

It is not supposed to be followed be a semicolon. It IS supposed to be followed by a set of curly braces.
The art of getting good answers lies in asking good questions.


Thanks Paul,

Code: [Select]
  if (sensorValue<limit);

  {digitalWrite (Bbrake,HIGH)

Is this correct.  I've tried this but i get an error 'expected ';' before delay'.  i'm trying to get the motor to stop for 5 seconds.


Think i've got it right now

Code: [Select]
if (sensorValue<limit)
    digitalWrite (Bbrake,HIGH);

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131