i am making a mini sumo robot for my electronics class. it is using two sharp sensors and two dc motors.
#include <SparkFun_TB6612.h>
float F_SHARP(int pin) {
float volts = analogRead(pin);
if (volts >= 80 && volts <= 530 )
{
float distance = (2076)/(volts - 11);
return distance;
}else{
return 0;
}
}
int PINSHARP1 = A0;
int PINSHARP2 = A1;
#define AIN1 2
#define BIN1 6
#define AIN2 3
#define BIN2 7
#define PWMA 13
#define PWMB 12
#define STBY 9
const int offsetA = 1;
const int offsetB = 1;
Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY);
Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY);
void setup() {
// put your setup code here, to run once:
pinMode (PINSHARP1,INPUT);
pinMode (PINSHARP2,INPUT);
}
motor1.drive(255);
motor1.drive(-255);
motor2.drive(255);
motor2.drive(-255);
float DisSharp1 = F_SHARP(PINSHARP1);
float DisSharp2 = F_SHARP(PINSHARP2);
if( DisSharp1 >= 4 && DisSharp1 <=30 ){
left(motor1, motor2, 100);
delay(1000);
forward(motor1, motor2, 150);
}
if(DisSharp2 >= 4 && DisSharp2 <= 30){
right(motor1, motor2, 100);
delay(1000);
forward(motor1, motor2, 150);
}
}
when i verify my code, i keep getting this error: ‘motor1’ does not name a type. please help.
i have attached the libray i have used.
SparkFun_TB6612FNG_Arduino_Library-master.zip (10.1 KB)
system
September 15, 2017, 8:24pm
#2
You have code that doesn't appear to be in a function.
C++ doesn't like that
MorganS
September 15, 2017, 8:24pm
#3
You have code outside the setup(){} and loop(){} functions. It looks like maybe the line that says “void loop(){” was deleted.
MorganS:
You have code outside the setup(){} and loop(){} functions. It looks like maybe the line that says "void loop(){" was deleted.
the thing is that i think void loop is not gonna help me. you see, i have made lots of codes for the robot that verify but the robot wont move an inch. i believe since void loop makes a series of instructions
repeat forever, it wont work for what i want. what i want th robot to do is to move according to the environment and i think void loop is blocking my other functions. also setup is not requiered cause im using a libray that does that for me
system
September 15, 2017, 8:41pm
#5
the thing is that i think void loop is not gonna help me
OK.
Get back to us when that's worked out for you.
i have made lots of codes for the robot that verify but the robot wont move an inch
Let us see one or two of them.
MorganS
September 15, 2017, 10:40pm
#7
void setup(){
doStuff();
}
void loop() {
//leave this empty
}
void doStuff() {
//put all your code here
}
Easy: it only does stuff once. But loop() must exist and you can’t put executable code outside a function.