Robin2:
Gimme a break now ... you have to do some of the work... ![:slight_smile: :slight_smile:](https://emoji.discourse-cdn.com/twitter/slight_smile.png?v=12)
How many hours (and I mean hours) have you spent trying to figure out the causes of the problems ?
The first few things come from your own code. It tells you what the problem is - you are declaring the same variable twice.
...R
Sorry, I have posted the reply too soon, meanwhile I was working on the problem and trying to figure it out, so here is what I came up with, but it is still not working, only the sensor is sending the readings, but the motor is not moving at all !!
When I click on verify its all green, and uploads to Arduino fine, but the only thing I get is the readings from the sensor with no motor movement !
project_test:14: error: conflicting declaration 'byte IN1'
project_test:8: error: 'IN1' has a previous declaration as 'const int IN1'
Replaced with IN1State
project_test:15: error: conflicting declaration 'byte IN2'
project_test:9: error: 'IN2' has a previous declaration as 'const int IN2'
replaced with IN2State
project_test:16: error: conflicting declaration 'byte SensorPin'
project_test:10: error: 'SensorPin' has a previous declaration as 'const int SensorPin'
replaced with SensorPinState
project_test.ino: In function 'void loop()':
project_test:64: error: 'prevMillis' was not declared in this scope
replaced with previousMillis
project_test:64: error: expected primary-expression before '=' token
A space was between > and =, changed to be >=
project_test:67: error: 'moveTime' was not declared in this scope
replaced with mtime
project_test:70: error: 'motorMode' was not declared in this scope
replaced with mode
project_test:71: error: 'motorPwr' was not declared in this scope
replaced with pwr
project_test:73: error: expected ')' before ']' token
project_test:73: error: expected ';' before ']' token
It was motor(motorMode, motorPwr];
**Replaced with motorMoves[mode, pwr]; **
project_test:76: error: 'rowMax' was not declared in this scope
Was added to line 34 below byte rowNum = 0; // the current row in the array
So it is now byte rowNum = 0; // the current row in the array
byte rowMax = 100;
(not sure if it is right or wrong, just assumed the place and the number as 100 is max power !!!)
project_test:83: error: a function-definition is not allowed here before '{' token
project_test:132: error: expected '}' at end of input
Added a missing '}'
conflicting declaration 'byte IN1'
Corrected above
So the new code now is
// 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 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
byte rowMax = 100;
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);
pinMode (ENA, OUTPUT);
pinMode (IN1, OUTPUT);
pinMode (IN2, OUTPUT);
pinMode (SensorPin, INPUT);
}
//============================================
void loop()
{
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
currentMillis = millis();
if (currentMillis - previousMillis >= intervalMillis) {
// update the timing values
previousMillis += intervalMillis;
mtime = motorMoves[rowNum][mtime];
intervalMillis = mtime;
// get the latest motor values
mode = motorMoves[rowNum][mode];
pwr = motorMoves[rowNum][pwr];
// call the motor function
motorMoves[mode, pwr];
// 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(pwr, 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;
}
}
//=====================================================