Conflict with the "attach" instruction and my code

Hi !, as I said in the title I have a big problem with attach and all my code.
Im doing a follower line robot and I added a turbine to make it strong when it is go faster.
The problem is focused when I add the "attach" instruction which make the behavior of my robot change, I don't know why. When my robot doesn't have that line the car works great but the turbine doesnt turn on. And when I put that line my car doesnt move propperly
I am using Arduino NANO

The code:

// Source: raptor/raptor_nl_pid.ino at master · NicolasU-N/raptor · GitHub
// Arduino Pro Micro, TB6612 driver, Pololu QTR-8 sensor
// Adaptado por: @NicolasU-N Y @OrlandoMurcia1

// TB6612 driver pinout
//const int STBY = 15; // standby
const int PWMA = 3; // speed and direction control motor A (left)
const int AIN1 = 5;
const int AIN2 = 4;
const int PWMB = 9; // speed and direction control motor B (right)
const int BIN1 = 10;
const int BIN2 = 11;
// Pololu QTR-8A analog array readout
#include <ServoTimer2.h>
#include <stdio.h>
#include <string.h>
#include <QTRSensors.h>
#define PIN_ESC 6
ServoTimer2 servo;
QTRSensorsAnalog qtra((unsigned char[]) {7, 6, 5, 4, 3, 2,1,0}, 8,1);
unsigned int IR[8];
// parameters and variables for non linear PID
const int vmin=50; //Estaba 80
const int vmax=80; // 150
const float kp=0.2;
const float ki=0.0001;
const float kd=1;
const float kv=0.07;
int p,d,u,vbase;
long i=0;
int p_old=0;
int V_TURBINA = 100;
int x=vbase+u;

int vel = 1000; //amplitud del pulso
void setup() {
pinMode(AIN1,OUTPUT);
pinMode(AIN2,OUTPUT);
pinMode(PWMA,OUTPUT);
pinMode(PWMB,OUTPUT);
pinMode(BIN1,OUTPUT);
pinMode(BIN2,OUTPUT);
pinMode(PIN_ESC,OUTPUT);
servo.attach(6);
delay(5000);
//
//// ESC.attach(6);
//////
////// //Activar el ESC
////myservo.writeMicroseconds(1000); //1000 = 1ms
////// //Cambia el 1000 anterior por 2000 si
////// //tu ESC se activa con un pulso de 2ms
//// delay(5000); //Esperar 5 segundos para hacer la activacion
//////
////// //Iniciar puerto serial
////Serial.begin(9600);
//// Serial.setTimeout(10);}
//myservo.writeMicroseconds(1000); //1000 = 1ms
//Cambia el 1000 anterior por 2000 si
//tu ESC se activa con un pulso de 2ms
//Esperar 5 segundos para hacer la activacion
servo.write(90);

}
void loop()
{
// myservo.write(100);
//digitalWrite(STBY, HIGH);
// ESC.writeMicroseconds(1500);
// //EDF27.setSpeed(1550);
qtra.read(IR); // read raw sensor values

p = -7IR[0]-5IR[1]-3IR[2]-IR[3]+IR[4]+3IR[5]+5IR[6]+7IR[7];
// Serial.println(p);
i=i+p;
d=p-p_old;
p_old=p;
if ((p*i)<0) i=0; // integral windup

u=kpp+kii+kdd;
vbase=vmin+(vmax-vmin)exp(-kvabs(kp
p));
drive(vbase+u,vbase-u);

//imprimir();

}

void drive(int L, int R) // speed for wheels Left and Right, positive is forward
{
L=constrain(L,-255,255); // avoid PWM overflow
R=constrain(R,-255,255);

digitalWrite(AIN1, L<0); // Estaba < switch < and >= if left wheel doesnt spin as expected
digitalWrite(AIN2, L>=0);// >=
analogWrite(PWMA, abs(L));

digitalWrite(BIN1, R<0); // Estaba < switch < and >= if left wheel doesnt spin as expected
digitalWrite(BIN2, R>=0);// >=
analogWrite(PWMB, abs(R));
}

//void imprimir(){
// Serial.println(p);
// delay(250);
//}

PID-Veloras.ino (2.77 KB)

const int PWMA = 3; // speed and direction control motor A (left)

From the ServoTimer2.h file:

Note that analogWrite of PWM on pins 3 and 11 is disabled when the first servo is attached

See a problem there? It never hurts to read the documentation for libraries that you use.

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask a good question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

From Servo - Arduino Reference

The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega. On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins.

#include <ServoTimer2.h>

He is using the ServoTimer2 library, not Servo. Timer 2 uses pins 11 and 3.

Sorry for the confusion

groundFungus:
Timer 2 uses pins 11 and 3.

pwm328.png

pwm328.png

I may be wrong but the statement in Reply #3 "Timer 2 uses pins 11 and 3" seems to me easier to assimilate than the diagram in Reply #5 :slight_smile:

...R

Robin2:
I may be wrong but the statement in Reply #3 "Timer 2 uses pins 11 and 3" seems to me easier to assimilate than the diagram in Reply #5 :slight_smile:

There are many ways for the representation of knowledge; who knows who likes what! Figure of Post#5 aims at amplifying the information of Post#3 through visualization which "has been an effective way to communicate both abstract and concrete ideas since the dawn of humanity".

GolamMostafa:
There are many ways for the representation of knowledge; who knows who likes what! Figure of Post#5 aims at amplifying the information of Post#3 through visualization which "has been an effective way to communicate both abstract and concrete ideas since the dawn of humanity".

...but usually only when the recipient recognises what the abstraction is.
Or a key is provided.

groundFungus:

const int PWMA = 3; // speed and direction control motor A (left)

From the ServoTimer2.h file:See a problem there? It never hurts to read the documentation for libraries that you use.

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask a good question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

Hi im from Colombia, it's a little bit hard to understand all the rules that this forum has, so please excuse me, i'll try to read them.

I was reading the documentation but i don't understand, is there a way to solve my problem?

Use a different PWM pin than pin 3 or pin 11 (other PWM pins are 5,6,9,10 on an Uno) for the motor speed control.