c++ - Arduinosumo bot with rotatory motor -
i making sumo bot project using arduino. have no experience arduino code, have java experience. said bot has edge detector prevent falling off, l298 driver, , forth. lack of experience, don't know how arduino's code works methods , etc. said, main question how executing methods delays? getting stuck in methods no return? not entering? executing ending later? seems run motors forward without switch on or off. have checked wiring. ( apologize poor questioning- stackedoverflow virgin).
int motor_forward = 10; int motor_reverse = 9; int motor2_forward = 13; int motor2_reverse = 12; int edgedec1 = 7; //int edgedec2 = 6; //the setup routine runs once when press reset; void setup(){ //initialize digital pin output. pinmode(motor_forward, output); pinmode(motor_reverse,output); pinmode(motor2_forward,output); pinmode(motor2_reverse,output); pinmode(edgedec1,input); } //the loop routine runs on , on again forever void loop(){ int indicator = random(2); delay(5000);//5 second delay //loop prevent 5 second delay while(true){ if (indicator = 0){ for(int x = 0; x < random(200); x++){ forward(); edgedec(); delay(10); } } else if ( indicator = 1){ for(int x = 0; x < random(200); x++){ turnleft(); edgedec(); delay(10); } } else if ( indicator = 2){ for(int x = 0; x < random(200); x++){ turnright(); edgedec(); delay(10); } } } } void forward(){ //right motor digitalwrite(motor_forward,1);//terminal d1 high digitalwrite(motor_reverse,0);//terminal d2 low //left motor digitalwrite(motor2_forward,1);//terminal d1 high digitalwrite(motor2_reverse,0);//terminal d2 low } //going in reverse void reverse(){ //right motor digitalwrite(motor_forward,0);//terminal d1 low digitalwrite(motor_reverse,1);//terminal d2 high //left motor digitalwrite(motor2_forward,0);//terminal d1 low digitalwrite(motor2_reverse,1);//terminal d2 high } //rotating left void turnleft(){ //right motor digitalwrite(motor_forward,0);//terminal d1 high digitalwrite(motor_reverse,1);//terminal d2 low //left motor digitalwrite(motor2_forward,1);//terminal d1 high digitalwrite(motor2_reverse,0);//terminal d2 low } void turnright(){ //right motor digitalwrite(motor_forward,1);//terminal d1 high digitalwrite(motor_reverse,0);//terminal d2 low //left motor digitalwrite(motor2_forward,0);//terminal d1 high digitalwrite(motor2_reverse,1);//terminal d2 low } void edgedec(){ if(edgedec1 == 1){ reverse(); delay(700); turnleft(); delay(1000); forward(); } }
you have couple of errors/misunderstandings in code, i've added bit of simple debugging works opening tools>serial monitor after you've downloaded code. in edgedec() function (function = java method) had if(edgedec = 1), edgedec int value of 7. wanted read value of pin numbered 7 - if(digitalread(edgedec1) == low).
also, reversed logic high (1) low (0) because hadn't tied down digitalread pins either low (using external resistors) or high (using arduino's internal resistors) - read comments in code. not sure doing while(true) loop - maybe further debugging? anyway, hope helps...
int motor_forward = 10; int motor_reverse = 9; int motor2_forward = 13; int motor2_reverse = 12; int edgedec1 = 7; //int edgedec2 = 6; int indicator; // pulled out of loop - askchipbug string repeatstring; //debugging variable, stops debug string repeating in serial monitor when watching loop - askchipbug //the setup routine runs once when press reset; void setup(){ serial.begin(9600); // set serial library @ 9600 bps, using serialmonitor debug - askchipbug //initialize digital pin output. pinmode(motor_forward, output); pinmode(motor_reverse,output); pinmode(motor2_forward,output); pinmode(motor2_reverse,output); pinmode(edgedec1,input_pullup); // set high doesn't float - askchipbug // can use internal 20k pullups input_pullup means 1 = off, 0 = on // or have use external pulldown resistors have 1 = on, 0 = off pr("setup completed"); // askchipbug } //the loop routine runs on , on again forever void loop(){ randomseed(analogread(0)); // seeds random() function different number each time loop runs - askchipbug indicator = random(2); pr("indicator: " + string(indicator)); // askchipbug pr("5 second delay"); delay(5000);//5 second delay //loop prevent 5 second delay while(true){ if (indicator == 0){ //you had set indicator = 0 - askchipbug pr("indicator: " + string(indicator)); // askchipbug for(int x = 0; x < random(200); x++){ forward(); edgedec(); delay(10); } } else if ( indicator = 1){ pr("indicator: " + string(indicator)); //askchipbug for(int x = 0; x < random(200); x++){ turnleft(); edgedec(); delay(10); } } else if ( indicator = 2){ pr("indicator: " + string(indicator));// askchipbug for(int x = 0; x < random(200); x++){ turnright(); edgedec(); delay(10); } } } } void forward(){ //right motor pr("forward"); //askchipbug digitalwrite(motor_forward,1);//terminal d1 high digitalwrite(motor_reverse,0);//terminal d2 low //left motor digitalwrite(motor2_forward,1);//terminal d1 high digitalwrite(motor2_reverse,0);//terminal d2 low } //going in reverse void reverse(){ pr("reverse"); //askchipbug //right motor digitalwrite(motor_forward,0);//terminal d1 low digitalwrite(motor_reverse,1);//terminal d2 high //left motor digitalwrite(motor2_forward,0);//terminal d1 low digitalwrite(motor2_reverse,1);//terminal d2 high } //rotating left void turnleft(){ pr("turn left"); //askchipbug //right motor digitalwrite(motor_forward,0);//terminal d1 high digitalwrite(motor_reverse,1);//terminal d2 low //left motor digitalwrite(motor2_forward,1);//terminal d1 high digitalwrite(motor2_reverse,0);//terminal d2 low } void turnright(){ pr("turn right"); //askchipbug //right motor digitalwrite(motor_forward,1);//terminal d1 high digitalwrite(motor_reverse,0);//terminal d2 low //left motor digitalwrite(motor2_forward,0);//terminal d1 high digitalwrite(motor2_reverse,1);//terminal d2 low } void edgedec(){ pr("edge detector: " + string(digitalread(edgedec1))); // read pin 7 - askchipbug if(digitalread(edgedec1) == low){ // remember we're using internal pullups low = on - askchipbug pr("edge detected!"); // askchipbug reverse(); delay(700); turnleft(); delay(1000); forward(); } } //simple debug technique - use pr("something"); in code - askchipbug void pr(string txt){ if(repeatstring != txt){ //if debug text different, print serial.println(txt); //prints text , adds newline serial.flush(); //waits data printed delay(1000); //just pauses scrolling text 1 second, make bigger if want longer pause repeatstring = txt; } }
Comments
Post a Comment