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