Looking to hire a programmer to write an Arduino Sketch for a small project

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.

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

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.

Thank you for the reply

Here is the codes

Sensor code

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

void loop() {

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

Motor code

#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.

, 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.

NI$HANT:
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 :frowning:

here is the link to my post there

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

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.

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

//=====================================================
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.

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:

// 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.

PaulS:
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.

PaulS:
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:

 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:

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:

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.

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

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

Can you please point them out ?

I don't see that

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

are used anywhere.

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?

So i tried...but it didnt work !

Hard to figure out what went wrong with code you deleted...

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

PaulS:
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.

PaulS:
Hard to figure out what went wrong with code you deleted...

sorry I posted the wrong code by mistake

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

//=====================================================
    rowNum += 1;
     if (rowNum > rowMax)
    {
      rowNum = 0;
    }

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

PaulS:
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 :slight_smile:

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 !

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

//=====================================================
   if (currMillis - prevMillis >= interval) 
  {

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

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

Not sure if you mean this :

if (sensorValue >= 513)
  {

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

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

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.

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.

Ok, got it.

 if (sensorValue >= 513)
 
  {
    moveTime = motorMoves[rowNum][mtime];
    interval = moveTime;
    motorMode = motorMoves[rowNum][mode];
    motorPwr = motorMoves[rowNum][pwr];
    motor(motorMode, motorPwr);
    rowNum = 4;
   
  }
  
   else 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;   
    }
  }  
}

Now it reacts immediately which is what I want.

But after few tests, something happened ... while reacting, if the sensor values becomes >= 513 it reacts again which extends the reaction time (reacting while reacting) ... and this is understandable because it was told to react whenever the value is >= 513.

That brings up two curious questions :

1- Is there a way to stop it doing that?

2- Is there a way to to make it do something if something happens over a period of time?
for example :

if (sensorValue >= 513 for more than 3 seconds)

{
 //React

 }

The problem with your code now is that you are thinking that if(){}/else if() {} is equivalent to if(){if(){} else {}}.

They are not. There is point in your code where you want to do something if some condition is now true OR if what needs to be done has changed. Once you have determined that one, or both, of those conditions is true, you still need to decide which one is true.

PaulS:
The problem with your code now is that you are thinking that if(){}/else if() {} is equivalent to if(){if(){} else {}}.

Ok, so now I tried this

   if(sensorValue >= 513)
 {   
    moveTime = motorMoves[rowNum][mtime];
    interval = moveTime;
    motorMode = motorMoves[rowNum][mode];
    motorPwr = motorMoves[rowNum][pwr];
    motor(motorMode, motorPwr);
    rowNum = 4;   
 }

  
 
 else {
     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;   
    }
  } 
}
}

and I have noticed that it is much better now than when I used the "else if", so when I interrupt it while it is reacting it carries on until the interval is over ( sometimes +2-3 seconds), but sometimes the motor gets confused for under a second and acts strange ( turning back and forth for a split of a second ) and looks like it is deciding which way to go, then it returns to normal !
I am suspecting it is the delay in the Serial port ??!!
Or is it something else??

PaulS:
There is point in your code where you want to do something if some condition is now true OR if what needs to be done has changed. Once you have determined that one, or both, of those conditions is true, you still need to decide which one is true.

Not sure I fully understand that !! ...
If its not too much trouble to modify this part of the code so I can compare it to what you said and try to understand it, that would be appreciated :slight_smile:

And the second question of the previous post remains.

2- Is there a way to to make it do something if something happens over a period of time?
for example :

You need to record when the event of interest starts.

Periodically, you see if the event of interest is no longer happening. If not, you clear the start time.

Periodically, you need to see if the event of interest is still happening, and, if so, whether enough time has passed. If so, do whatever needs doing.

As for the other issue, I can't keep track of what you are trying to do, and how well that is (or isn't) working. It's time for you to post some more snippets, at http://snippets-r-us.com, and summarize the problems here.

PaulS:
It's time for you to post some more snippets, at http://snippets-r-us.com, and summarize the problems here.

I followed the link but unfortunately I didn't understand the difference between posting here and there !
it is a different website, plus I didn't find a link to where I can post my problem there !

But whether this means the help stops here or not, I cant thank you enough for all the help and support, i have learned a lot in the last few days and started to understand some of the concepts.

Thank you very much.