Article Index

 

 Page 137

class UniMotor {
private:
  int gpio;
  int duty;
public:
  UniMotor(int pin) {
    gpio = pin;
  }
  void setSpeed(float s) {
    duty = s * 255;
    analogWrite(gpio, duty);
  }
};

UniMotor *motor1;
void setup() {
  motor1 = new UniMotor(2);
  motor1->setSpeed(0.5);
}

void loop() {
}

 

Page 142

class BiMotor {
private:
  int gpio1;
  int gpio2;
  int duty;
  bool forward;
public:
  BiMotor(int pin1, int pin2) {
    gpio1 = pin1;
    gpio2 = pin2;
  }
  void setSpeedDirection(float s, bool f) {
    duty = s * 255;
    forward = f;
    if (forward) {
      analogWrite(gpio1, 0);
      analogWrite(gpio1, duty);
    } else {
      analogWrite(gpio2, 0);
      analogWrite(gpio2, duty);
    }
  }
};
BiMotor *motor1;
void setup() {
  motor1 = new BiMotor(2, 4);
  motor1->setSpeedDirection(0.5, false);
}

void loop() {}

 

Page 145 

The LEDC library is not currently supported by the  Arduino NANO ESP32 

class Servo {
private:
  int gpio;
  int max;
  int min;
public:
  Servo(int pin) {
    gpio = pin;
    max = (2 << 9) * 12.5 / 100;
    min = (2 << 9) * 2.5 / 100;
    ledcAttach(pin, 50, 10);
  }
  void setAngle(float a) {
    int duty = (float)(max - min) * a + min;
    ledcWrite(2, duty);
  }
};
void loop() {
  servo1->setAngle(1.0);
  delay(100);
  servo1->setAngle(0.5);
  delay(100);
  servo1->setAngle(0.0);
  delay(100);
}

 

Page 153

Use GPIO 5, 6, 7 and 8 for the Arduino NANO ESP32 

#include <Stepper.h>

Stepper stepMot1 = Stepper(200, 16, 17, 18, 19);

void setup() {
  stepMot1.setSpeed(1);
  stepMot1.step(100);
}

void loop() {
}
 

Page 157 

Use GPIO 5, 6, 7 and 8 for the Arduino NANO ESP32 

change Stepperbi stepper1=Stepperbi(16); to Stepperbi stepper1=Stepperbi(5);

class Stepperbi {

private:
  int gpio;
  int stepTable[8] = { 1, 0, 2, 1, 3, 2, 0, 3 };
  int state = 0;
public:
  Stepperbi(int pin1) {
    gpio = pin1;
    for (int i = 0; i < 4; i++) {
      pinMode(gpio + i, OUTPUT);
      digitalWrite(gpio + i, 0);
    }
    digitalWrite(gpio, 1);
  }
  void step(int step) {
    if (step > 0) {
      digitalWrite(gpio + stepTable[state], (state + 1) % 2);
      state = (state + 1) % 8;
    } else {
      state = (state +7) % 8;
      digitalWrite(gpio + stepTable[state], state % 2);
    };
  }
};
Stepperbi stepper1=Stepperbi(16);
void setup(){};
void loop() {
  stepper1.step(1);
  delay(10);
}

 

Page  157

Use GPIO 5, 6, 7 and 8 for the Arduino NANO ESP32 

change Stepperbi stepper1=Stepperbi(16); to Stepperbi stepper1=Stepperbi(5);

class Stepperbi {
private:
  int gpio;
  int stepTable[4] = { 0,2,1,3 };
  int state = 0;
public:
  Stepperbi(int pin1) {
    gpio = pin1;
    for (int i = 0; i < 8; i++) {
      pinMode(gpio + i, OUTPUT);
      digitalWrite(gpio + i, 0);
    }
    digitalWrite(gpio, 1);
    digitalWrite(gpio+2, 1);
  }
  void step(int step) {
    if (step > 0) {
      digitalWrite(gpio + stepTable[(state+2) % 4],0);
      digitalWrite(gpio + stepTable[state],1);
      state = (state + 1) % 4;
    } else {
      state = (state + 3) % 4;
      digitalWrite(gpio + stepTable[(state+2) % 4],1);
      digitalWrite(gpio + stepTable[state],0);
     
    };
  }
};
Stepperbi stepper1=Stepperbi(16);
void setup(){};
void loop() {
  stepper1.step(1);
  delay(10);
}