Go Down

Topic: Looking to hire a programmer to write an Arduino Sketch for a small project (Read 3734 times) previous topic - next topic

OneGoal

Hello everyone,

I am looking for someone to write a program for my small project.

It is basically a DC motor and a current sensor, where the DC motor is running at a programmed cycles with different speeds and directions, and the current sensor is monitoring the current and sending feedback.

I have the two sketches for the motor and the sensor and I need to combine them together in a way that the sensor sends continues readings while the motor is doing its thing without a delay, and if the sensor value goes above a certain threshold, the motor changes state, ie. stops, rotates in a different direction, changes speed, etc .....

My programming skill (if any) is very basic so I had no luck getting it to work, I tried using arrays, millis, interrupts, etc etc ... but no luck !!

More details when we establish communication.

Isaac96

Post both pieces of code. In code tags, please!
I can help, and others will be more likely to help if you post code.
Do not PM me for help. I will delete immediately.
CONNECT THE GROUNDS!

After Tuesday, even the calendar goes W T F

OneGoal

Post both pieces of code. In code tags, please!
I can help, and others will be more likely to help if you post code.
Thank you for the reply

Here is the codes

Sensor code

Code: [Select]
void setup() {
Serial.begin(9600);
}

void loop() {

int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(500);
}



Motor code

Code: [Select]
 
#define ENA 5  //enable A on pin 5 (needs to be a pwm pin)
#define IN1 2  //IN1 on pin 2 conrtols one side of bridge A
#define IN2 4  //IN2 on pin 4 controls other side of A

void setup()
{
  //set all of the outputs
  pinMode(ENA, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
}

void loop()
{
  motor(1, 50);  //have motor turn clockwise at 50% speed
  delay(5000);
  motor(3, 100);  //brake motor with 100% braking power
   delay(4000);
  motor(1, 100);  //have motor turn clockwise at 100% speed
  delay(4000);
  motor(3, 100);  //brake motor A with 100% braking power
   delay(1000);
  //have motor turn counter-clockwise at 100% speed
  motor(2, 100); 
  delay(2000);
  motor(3, 100);  //brake motor  with 100% braking power
   delay(2000);
 
}

//******************   Motor A control   *******************
void motor(int mode, int percent)
{
 
  //change the percentage range of 0 -> 100 into the PWM
  //range of 0 -> 255 using the map function
  int duty = map(percent, 0, 100, 0, 255);
 
  switch(mode)
  {
    case 0:  //disable/coast
      digitalWrite(ENA, LOW);  //set enable low to disable A
      break;
     
    case 1:  //turn clockwise
      //setting IN1 high connects motor lead 1 to +voltage
      digitalWrite(IN1, HIGH);   
     
      //setting IN2 low connects motor lead 2 to ground
      digitalWrite(IN2, LOW); 
     
      //use pwm to control motor speed through enable pin
      analogWrite(ENA, duty); 
     
      break;
     
    case 2:  //turn counter-clockwise
      //setting IN1 low connects motor lead 1 to ground
      digitalWrite(IN1, LOW);   
     
      //setting IN2 high connects motor lead 2 to +voltage
      digitalWrite(IN2, HIGH); 
     
      //use pwm to control motor speed through enable pin
      analogWrite(ENA, duty); 
     
      break;
     
    case 3:  //brake motor
      //setting IN1 low connects motor lead 1 to ground
      digitalWrite(IN1, LOW);   
     
      //setting IN2 high connects motor lead 2 to ground
      digitalWrite(IN2, LOW); 
     
      //use pwm to control motor braking power
      //through enable pin
      analogWrite(ENA, duty); 
     
      break;
  }
}
//**********************************************************




I am using Arduino UNO, L298N H bridge motor driver, ACS712 current sensor, a 12v DC motor.

All connected together via a breadboard and running nicely with no connection issues, and it is working, but not the way I want it as it has a delay() function.



Nishant_Sood

Quote
, but not the way I want it as it has a delay() function.
You need to take inspiration from the "Blink without Delay" example within the Arduino IDE.
"Real Men can Accomplish  Anything"

- skype : nishants5  
ਫ਼ਤੇਹ ਕਰੁਂ!
www.winacro.com

OneGoal

You need to take inspiration from the "Blink without Delay" example within the Arduino IDE.
Thats what I have been trying to do for the past few days with no success, because the blink without delay examples use a single pin to turn it on and off where in my case I am using 3 pins for the motor driver and 1 pin for the sensor and I couldn't get it to work even with some expert help from the project guidance in the forum :( 

here is the link to my post there

https://forum.arduino.cc/index.php?topic=341603.0

OneGoal

And Here is a code from Robin2, who was kind enough to help, but due to my limited programming knowledge I failed to use it in a correct way, as there is a number of variables which to be declared, and few changes be made.

When you open it in the Arduino software and verify it, it will give some errors, so if someone can help with it, that would be highly appreciated, and as I mentioned in the post title, I am willing to pay a programmer if he/she can make it work.

Please PM me if you are interested.

The link to the original post in the project guidance section is in the code for details about the problem.

Code: [Select]

// evolved from http://forum.arduino.cc/index.php?topic=341603.msg2356572#msg2356572

// --------CONSTANTS (won't change)---------------


const int ENA = 5; //enables A on pin 5 (needs to be a pwm pin) for motor driver H bridge
const int IN1 = 2; //enables side 1 of motor driver
const int IN2 = 4; //enables side 2 of motor driver
const int SensorPin = A0; //enables the output pin for current sensor

//------------ VARIABLES (will change)---------------------

byte ENAState = LOW; //to record whether its on or off
byte IN1 = LOW;
byte IN2 = LOW;
byte SensorPin = LOW;
int sensorValue = 0;



//**********************************************************

unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
unsigned long intervalMillis = 0;

// these make it convenient to identify the items in the array
byte mode = 0;
byte pwr = 1;
byte mtime = 2;

byte rowNum = 0; // the current row in the array

int motorMoves[4][3] = {
// the columns are mode, pwr, mtime
{1, 100, 2000} ,  // mode 1, power 100, 2000 millisecs
{0, 0, 1000} ,    // I'm assuming mode 0 = stop
{3, 100, 3000} , 
{0, 0, 5000} 
};

//=================================================

void setup() {

Serial.begin(9600);
int sensorValue = analogRead(A0);
Serial.println(sensorValue);

pinMode (ENA, OUTPUT);
pinMode (IN1, OUTPUT);
pinMode (IN2, OUTPUT);
pinMode (SensorPin, INPUT);
 
}

//============================================

void loop()   {

currentMillis = millis();
 
if (currentMillis - prevMillis > = intervalMillis) {
// update the timing values
prevMillis += intervalMillis;
moveTime = motorMoves[rowNum][mtime];
intervalMillis = moveTime;
// get the latest motor values
motorMode = motorMoves[rowNum][mode];
motorPwr = motorMoves[rowNum][pwr];
// call the motor function
motor(motorMode, motorPwr];
// update the rowNum ready for the next time
rowNum += 1;
if (rowNum > rowMax) {
rowNum = 0;
}
}

//******************   Motor control   *******************
void motor(int mode, int percent)
{
 
//change the percentage range of 0 -> 100 into the PWM
//range of 0 -> 255 using the map function
int duty = map(percent, 0, 100, 0, 255);
 
switch(mode)
{
case 0:  //disable/coast
digitalWrite(ENA, LOW);  //set ENAble low to disable the Motor
break;

case 1:  //turn clockwise
//setting IN1 high connects motor lead 1 to +voltage
digitalWrite(IN1, HIGH);   

//setting IN2 low connects motor lead 2 to ground
digitalWrite(IN2, LOW);

//use pwm to control motor speed through ENAble pin
analogWrite(ENA, duty);

break;

case 2:  //turn counter-clockwise
//setting IN1 low connects motor lead 1 to ground
digitalWrite(IN1, LOW);   

//setting IN2 high connects motor lead 2 to +voltage
digitalWrite(IN2, HIGH);

//use pwm to control motor speed through ENAble pin
analogWrite(ENA, duty);

break;

case 3:  //brake motor
//setting IN1 low connects motor lead 1 to ground
digitalWrite(IN1, LOW);   

//setting IN2 high connects motor lead 2 to ground
digitalWrite(IN2, LOW);

//use pwm to control motor braking power
//through ENAble pin
analogWrite(ENA, duty);

break;
}
}

//=====================================================







PaulS

Code: [Select]
const int IN1 = 2; //enables side 1 of motor driver
const int IN2 = 4; //enables side 2 of motor driver
const int SensorPin = A0; //enables the output pin for current sensor

So, you declare some variables and give them values.

Code: [Select]
byte IN1 = LOW;
byte IN2 = LOW;
byte SensorPin = LOW;

Now, what are you doing? These look like they should all have State in the name (like ENA/ENAState).

I made what look like reasonable changes, and the code now compiles:
Code: [Select]

// evolved from http://forum.arduino.cc/index.php?topic=341603.msg2356572#msg2356572

// --------CONSTANTS (won't change)---------------


const int ENA = 5; //enables A on pin 5 (needs to be a pwm pin) for motor driver H bridge
const int IN1 = 2; //enables side 1 of motor driver
const int IN2 = 4; //enables side 2 of motor driver
const int SensorPin = A0; //enables the output pin for current sensor

//------------ VARIABLES (will change)---------------------

byte ENAState = LOW; //to record whether its on or off
byte IN1State = LOW;
byte IN2State = LOW;
byte SensorPinState = LOW;
int sensorValue = 0;

//**********************************************************

unsigned long currMillis = 0;
unsigned long prevMillis = 0;
unsigned long interval = 0;

unsigned long moveTime;
int motorMode;
int motorPwr;

// these make it convenient to identify the items in the array
byte mode = 0;
byte pwr = 1;
byte mtime = 2;

byte rowNum = 0; // the current row in the array

int motorMoves[4][3] =
{
  // the columns are mode, pwr, mtime
  {
    1, 100, 2000  }
  ,  // mode 1, power 100, 2000 millisecs
  {
    0, 0, 1000  }
  ,    // I'm assuming mode 0 = stop
  {
    3, 100, 3000  }
  , 
  {
    0, 0, 5000  } 
};

int rowMax = 4;

//=================================================

void setup()
{
  Serial.begin(9600);
  int sensorValue = analogRead(A0);
  Serial.println(sensorValue);

  pinMode (ENA, OUTPUT);
  pinMode (IN1, OUTPUT);
  pinMode (IN2, OUTPUT);
  pinMode (SensorPin, INPUT);
}

//============================================

void loop()
{
  currMillis = millis();

  if (currMillis - prevMillis >= interval)
  {
    // update the timing values
    prevMillis += interval;
    moveTime = motorMoves[rowNum][mtime];
    interval = moveTime;
    // get the latest motor values
    motorMode = motorMoves[rowNum][mode];
    motorPwr = motorMoves[rowNum][pwr];
    // call the motor function
    motor(motorMode, motorPwr);
    // update the rowNum ready for the next time
    rowNum += 1;
    if (rowNum > rowMax)
    {
      rowNum = 0;
    }
  }
}

//******************   Motor control   *******************
void motor(int mode, int percent)
{
  //change the percentage range of 0 -> 100 into the PWM
  //range of 0 -> 255 using the map function
  int duty = map(percent, 0, 100, 0, 255);

  switch(mode)
  {
  case 0:  //disable/coast
    digitalWrite(ENA, LOW);  //set ENAble low to disable the Motor
    break;

  case 1:  //turn clockwise
    //setting IN1 high connects motor lead 1 to +voltage
    digitalWrite(IN1, HIGH);   

    //setting IN2 low connects motor lead 2 to ground
    digitalWrite(IN2, LOW);

    //use pwm to control motor speed through ENAble pin
    analogWrite(ENA, duty);

    break;

  case 2:  //turn counter-clockwise
    //setting IN1 low connects motor lead 1 to ground
    digitalWrite(IN1, LOW);   

    //setting IN2 high connects motor lead 2 to +voltage
    digitalWrite(IN2, HIGH);

    //use pwm to control motor speed through ENAble pin
    analogWrite(ENA, duty);

    break;

  case 3:  //brake motor
    //setting IN1 low connects motor lead 1 to ground
    digitalWrite(IN1, LOW);   

    //setting IN2 high connects motor lead 2 to ground
    digitalWrite(IN2, LOW);

    //use pwm to control motor braking power
    //through ENAble pin
    analogWrite(ENA, duty);

    break;
  }
}

//=====================================================


There are unused global variables. I have no idea whether this does what you want/expect.

OneGoal

I made what look like reasonable changes, and the code now compiles:
You are a star, yes it is making more sense now and works perfectly, thank you very much.


There are unused global variables. I have no idea whether this does what you want/expect.
Can you please point them out ?
And yes the code now is doing what i want it to do, so i have made some modifications to suit my plan and it is working very nicely.

Now the question is, after adding the:

Code: [Select]
int sensorValue = analogRead(A0);
  Serial.println(sensorValue);


to the loop, it is sending the reading but it is too fast, i managed to add a delay(500); in the loop and it slowed down as it should do, but is that going to affect anything?, to my knowledge i am not supposed to use delays in this sketch as it stops the whole thing, which i didnt notice as it stayed running and doing what it is suppose to do !!
I tried to declare the timing as a global variable, and also as a local variable "sensorValueInterval = 500;' in different places (int, const int, and unsigned long) but it didnt change anything, the only way i had luck with is the delay function !!


I also need the motor to change state when the sensor value goes above a certain threshold

i tried in the loop:

Code: [Select]
if (sensorValue >= 513)

         //then it should turn counter-clockwise @100% power for 10 seconds



which in the array it should be {2, 100, 10000}
So i tried creating a new array:
Code: [Select]
int motorReact[3] = {2, 100, 10000};

then called in the loop after the if statment "motorReact:"
but it didnt work !

I have read about interrups and watched few videos, but it is very confusing, and i failed to get it to work with my sketch as almost all the examples are for LEDs and Bottons, not DC motors !

I just need it to read the sensor value at 500 millis intervals,a and if the value is >= 513, the motor turns counter-clockwise @100% power for 10 seconds.

Code: [Select]


// evolved from http://forum.arduino.cc/index.php?topic=341603.msg2356572#msg2356572

// --------CONSTANTS (won't change)---------------


const int ENA = 5; //enables A on pin 5 (needs to be a pwm pin) for motor driver H bridge
const int IN1 = 2; //enables side 1 of motor driver
const int IN2 = 4; //enables side 2 of motor driver
const int SensorPin = A0; //enables the output pin for current sensor

//------------ VARIABLES (will change)---------------------

byte ENAState = LOW; //to record whether its on or off
byte IN1State = LOW;
byte IN2State = LOW;
byte SensorPinState = LOW;
int sensorValue = 0;


//**********************************************************

unsigned long currMillis = 0;
unsigned long prevMillis = 0;
unsigned long interval = 0;
unsigned long moveTime;
int motorMode;
int motorPwr;

// these make it convenient to identify the items in the array
byte mode = 0;
byte pwr = 1;
byte mtime = 2;

byte rowNum = 0; // the current row in the array

int motorMoves[8][3] =
{
  // the columns are mode, pwr, mtime
 
  { 1, 100, 5000  } ,  // mode 1, turns motor clockwise @100% pwr, for 5 seconds
  { 3, 0, 2000  } ,    // mode 3, stops motor, for 2 seconds
  { 2, 100, 5000  } ,  // mode 2, turns motor counter-clockwise  @100% pwr, for 5 seconds
  { 3, 0, 2000  } ,
  { 1, 50, 3000 } ,
  { 3, 0, 5000 } ,
  { 2, 75, 4000 } ,
  { 3, 0, 3000 }
};

int rowMax = 8;

//=================================================

void setup()
{
  Serial.begin(9600);

  pinMode (ENA, OUTPUT);
  pinMode (IN1, OUTPUT);
  pinMode (IN2, OUTPUT);
  pinMode (SensorPin, INPUT);
}

//============================================


void loop()
{

  int sensorValue = analogRead(A0);
  Serial.println(sensorValue);
 
 
  currMillis = millis();

 
  if (currMillis - prevMillis >= interval)
  {
    // update the timing values
    prevMillis += interval;
    moveTime = motorMoves[rowNum][mtime];
    interval = moveTime;
    // get the latest motor values
    motorMode = motorMoves[rowNum][mode];
    motorPwr = motorMoves[rowNum][pwr];
    // call the motor function
    motor(motorMode, motorPwr);
    // update the rowNum ready for the next time
    rowNum += 1;
    if (rowNum > rowMax)
    {
      rowNum = 0;
    }
  }
}

//******************   Motor control   *******************
void motor(int mode, int percent)
{
  //change the percentage range of 0 -> 100 into the PWM
  //range of 0 -> 255 using the map function
  int duty = map(percent, 0, 100, 0, 255);

  switch(mode)
  {
  case 0:  //disable/coast
    digitalWrite(ENA, LOW);  //set ENAble low to disable the Motor
    break;

  case 1:  //turn clockwise
    //setting IN1 high connects motor lead 1 to +voltage
    digitalWrite(IN1, HIGH);   

    //setting IN2 low connects motor lead 2 to ground
    digitalWrite(IN2, LOW);

    //use pwm to control motor speed through ENAble pin
    analogWrite(ENA, duty);

    break;

  case 2:  //turn counter-clockwise
    //setting IN1 low connects motor lead 1 to ground
    digitalWrite(IN1, LOW);   

    //setting IN2 high connects motor lead 2 to +voltage
    digitalWrite(IN2, HIGH);

    //use pwm to control motor speed through ENAble pin
    analogWrite(ENA, duty);

    break;

  case 3:  //brake motor
    //setting IN1 low connects motor lead 1 to ground
    digitalWrite(IN1, LOW);   

    //setting IN2 high connects motor lead 2 to ground
    digitalWrite(IN2, LOW);

    //use pwm to control motor braking power
    //through ENAble pin
    analogWrite(ENA, duty);

    break;
  }
}

//=====================================================




 




PaulS

Quote
Can you please point them out ?
I don't see that
Code: [Select]
byte ENAState = LOW; //to record whether its on or off
byte IN1 = LOW;
byte IN2 = LOW;
byte SensorPin = LOW;

are used anywhere.

Quote
I just need it to read the sensor value at 500 millis intervals,a and if the value is >= 513, the motor turns counter-clockwise @100% power for 10 seconds.
So, where are you actually using the value read from the sensor?

Quote
So i tried...but it didnt work !
Hard to figure out what went wrong with code you deleted...

OneGoal

Sorry for the late reply, the notification function doesn't seem to work in the forum for some reason !

So, where are you actually using the value read from the sensor?
From the Serial port, it is averaging between 510 and 512, and when I try to slow the motor down with my hand the number increases, so I added (if) statement to call a function to rotate the motor counter-clockwise for 10 second, but it didn't work.

Hard to figure out what went wrong with code you deleted...
sorry I posted the wrong code by mistake


Code: [Select]


// evolved from http://forum.arduino.cc/index.php?topic=341603.msg2356572#msg2356572

// --------CONSTANTS (won't change)---------------


const int ENA = 5; //enables A on pin 5 (needs to be a pwm pin) for motor driver H bridge
const int IN1 = 2; //enables side 1 of motor driver
const int IN2 = 4; //enables side 2 of motor driver
const int SensorPin = A0; //enables the output pin for current sensor



//------------ VARIABLES (will change)---------------------

byte ENAState = LOW; //to record whether its on or off
byte IN1State = LOW;
byte IN2State = LOW;
byte SensorPinState = LOW;
int sensorValue = 0;


//**********************************************************

unsigned long currMillis = 0;
unsigned long prevMillis = 0;
unsigned long interval = 0;
unsigned long moveTime;
int motorMode;
int motorPwr;


// these make it convenient to identify the items in the array
byte mode = 0;
byte pwr = 1;
byte mtime = 2;

byte rowNum = 0; // the current row in the array

int motorMoves[9][3] =
{
  // the columns are mode, pwr, mtime
 
  { 1, 100, 3000  } ,  // mode 1, turns motor clockwise @100% pwr, for 5 seconds
   
  { 3, 100, 2000} ,    // mode 3, stops motor, for 2 seconds
  { 2, 100, 5000  } ,  // mode 2, turns motor counter-clockwise  @100% pwr, for 5 seconds
  { 3, 0, 2000  } ,
  { 1, 75, 3000 } ,
  { 3, 0, 5000 } ,
  { 2, 100, 3000 } ,
  { 3, 0, 3000 }
};

int rowMax = 9;

int motorReact[1][3] =
{
  // the columns are mode, pwr, mtime
 
  { 2, 50, 10000  } ,  // mode 2, turns motor counter-clockwise @50% pwr, for 10 seconds
   
   
};



//=================================================

void setup()
{
  Serial.begin(9600);
 
 

  pinMode (ENA, OUTPUT);
  pinMode (IN1, OUTPUT);
  pinMode (IN2, OUTPUT);
  pinMode (SensorPin, INPUT);
 
}

//============================================


void loop()
{

 
   int sensorValue = analogRead(A0);
  Serial.println(sensorValue);
 
 
  currMillis = millis();


 
  if (sensorValue >= 513)
  {
    React(); //call the function to rotate the motor counter-clockwise for 10 seconds
  }
 
       
  if (currMillis - prevMillis >= interval)
  {
    // update the timing values
    prevMillis += interval;
    moveTime = motorMoves[rowNum][mtime];
    interval = moveTime;
    // get the latest motor values
    motorMode = motorMoves[rowNum][mode];
    motorPwr = motorMoves[rowNum][pwr];
    // call the motor function
    motor(motorMode, motorPwr);
    // update the rowNum ready for the next time
    rowNum += 1;
    if (rowNum > rowMax)
    {
      rowNum = 0;
    }
  }

}

//************** Reaction when sensor value changes ***************
void React() {


if (currMillis - prevMillis >= interval)
  {
    // update the timing values
    prevMillis += interval;
    moveTime = motorReact[rowNum][mtime];
    interval = moveTime;
    // get the latest motor values
    motorMode = motorReact[rowNum][mode];
    motorPwr = motorReact[rowNum][pwr];
    // call the motor function
    motor(motorMode, motorPwr);
    // update the rowNum ready for the next time
    rowNum += 1;
     if (rowNum > rowMax)
    {
      rowNum = 0;
    }
   
  }
}

//******************   Motor control   *******************
void motor(int mode, int percent)
{
  //change the percentage range of 0 -> 100 into the PWM
  //range of 0 -> 255 using the map function
  int duty = map(percent, 0, 100, 0, 255);

  switch(mode)
  {
  case 0:  //disable/coast
    digitalWrite(ENA, LOW);  //set ENAble low to disable the Motor
    break;

  case 1:  //turn clockwise
    //setting IN1 high connects motor lead 1 to +voltage
    digitalWrite(IN1, HIGH);   

    //setting IN2 low connects motor lead 2 to ground
    digitalWrite(IN2, LOW);

    //use pwm to control motor speed through ENAble pin
    analogWrite(ENA, duty);

    break;

  case 2:  //turn counter-clockwise
    //setting IN1 low connects motor lead 1 to ground
    digitalWrite(IN1, LOW);   

    //setting IN2 high connects motor lead 2 to +voltage
    digitalWrite(IN2, HIGH);

    //use pwm to control motor speed through ENAble pin
    analogWrite(ENA, duty);

    break;

  case 3:  //brake motor
    //setting IN1 low connects motor lead 1 to ground
    digitalWrite(IN1, LOW);   

    //setting IN2 high connects motor lead 2 to ground
    digitalWrite(IN2, LOW);

    //use pwm to control motor braking power
    //through ENAble pin
    analogWrite(ENA, duty);

    break;
  }
}

//=====================================================






PaulS

Code: [Select]
    rowNum += 1;
     if (rowNum > rowMax)
    {
      rowNum = 0;
    }

So, rowNum is the index into which array? motorMoves and motorReact are not the same length.

OneGoal

So, rowNum is the index into which array? motorMoves and motorReact are not the same length.
Ah ok, I got it now !
I would like to think that I am starting to make less stupid mistakes and more educational ones :)


So I have decided to get rid of the motorReact array and use a "unused" row in the motorMoves which is restricted by the rowMax, so I added the mode I wanted to use as a reaction to the sensorValue change in the end of the array, and it will not be used in the loop until called.

And it is working fine and reacting to the if statement, but there is a problem !
It has to wait for the current mode to finish first then it takes its turn !!
In other words, it doesn't react immediately !! So if the current mode is going for 5 seconds, it will only react after 5 seconds are over !

Code: [Select]


// evolved from http://forum.arduino.cc/index.php?topic=341603.msg2356572#msg2356572

// --------CONSTANTS (won't change)---------------


const int ENA = 5; //enables A on pin 5 (needs to be a pwm pin) for motor driver H bridge
const int IN1 = 2; //enables side 1 of motor driver
const int IN2 = 4; //enables side 2 of motor driver
const int SensorPin = A0; //enables the output pin for current sensor



//------------ VARIABLES (will change)---------------------

byte ENAState = LOW; //to record whether its on or off
byte IN1State = LOW;
byte IN2State = LOW;
byte SensorPinState = LOW;
int sensorValue = 0;


//**********************************************************

unsigned long currMillis = 0;
unsigned long prevMillis = 0;
unsigned long interval = 0;
unsigned long moveTime;
int motorMode;
int motorPwr;

// these make it convenient to identify the items in the array
byte mode = 0;
byte pwr = 1;
byte mtime = 2;

byte rowNum = 0; // the current row in the array

int motorMoves[9][3] =
{
  // the columns are mode, pwr, mtime
 
  { 1, 100, 3000  } ,  // mode 1, turns motor clockwise @100% pwr, for 5 seconds 
  { 3, 100, 2000} ,    // mode 3, stops motor, for 2 seconds
  { 2, 100, 5000  } ,  // mode 2, turns motor counter-clockwise  @100% pwr, for 5 seconds
  { 3, 0, 2000  } ,
 
  //{ 1, 75, 3000 } , //temporarily disabled
  //{ 3, 0, 5000 } , //temporarily disabled
  //{ 2, 100, 3000 } , //temporarily disabled
  //{ 3, 0, 3000 } , //temporarily disabled
 
  { 2, 50, 10000 } ,  //in this example it is rowNum 4, and since rowMax = 3, it is not used in the loop untill called.
};

int rowMax = 3;







//=================================================

void setup()
{
  Serial.begin(9600);
 
 

  pinMode (ENA, OUTPUT);
  pinMode (IN1, OUTPUT);
  pinMode (IN2, OUTPUT);
  pinMode (SensorPin, INPUT);
 
}

//============================================


void loop()
{

 
   int sensorValue = analogRead(A0);
  Serial.println(sensorValue);
 
 
  currMillis = millis();



  if (sensorValue >= 513)
  {

   rowNum = 4;
  }
   

 
   if (currMillis - prevMillis >= interval)
  {
    // update the timing values
    prevMillis += interval;
    moveTime = motorMoves[rowNum][mtime];
    interval = moveTime;
    // get the latest motor values
    motorMode = motorMoves[rowNum][mode];
    motorPwr = motorMoves[rowNum][pwr];
    // call the motor function
    motor(motorMode, motorPwr);
    // update the rowNum ready for the next time
    rowNum += 1;
    if (rowNum > rowMax)
    {
      rowNum = 0;
    }
  }

}


//******************   Motor control   *******************
void motor(int mode, int percent)
{
  //change the percentage range of 0 -> 100 into the PWM
  //range of 0 -> 255 using the map function
  int duty = map(percent, 0, 100, 0, 255);

  switch(mode)
  {
  case 0:  //disable/coast
    digitalWrite(ENA, LOW);  //set ENAble low to disable the Motor
    break;

  case 1:  //turn clockwise
    //setting IN1 high connects motor lead 1 to +voltage
    digitalWrite(IN1, HIGH);   

    //setting IN2 low connects motor lead 2 to ground
    digitalWrite(IN2, LOW);

    //use pwm to control motor speed through ENAble pin
    analogWrite(ENA, duty);

    break;

  case 2:  //turn counter-clockwise
    //setting IN1 low connects motor lead 1 to ground
    digitalWrite(IN1, LOW);   

    //setting IN2 high connects motor lead 2 to +voltage
    digitalWrite(IN2, HIGH);

    //use pwm to control motor speed through ENAble pin
    analogWrite(ENA, duty);

    break;

  case 3:  //brake motor
    //setting IN1 low connects motor lead 1 to ground
    digitalWrite(IN1, LOW);   

    //setting IN2 high connects motor lead 2 to ground
    digitalWrite(IN2, LOW);

    //use pwm to control motor braking power
    //through ENAble pin
    analogWrite(ENA, duty);

    break;
  }
}

//=====================================================





PaulS

Code: [Select]
   if (currMillis - prevMillis >= interval)
  {

If it is time to do something OR there is  something else to do...

OneGoal

If it is time to do something OR there is  something else to do...
Not sure if you mean this :

Code: [Select]
if (sensorValue >= 513)
  {

  if (currMillis - prevMillis >= interval)
  {
  rowNum = 4;
  }
  }
 


But it didn't react at all with this change !

PaulS

What I meant was that you want to do something different when the time is up OR when there is something different to do (because the mode switch was pressed). Just changing rowNum is not sufficient.

Inside the else block, you'll still need to determine what to do.

Go Up