Need Help In classes

Hello.

I was trying to understand the code of online tutorials, but I got stuck at the class parts. So, it will be great if someone helps me on this.

enum speedSettings 
{
  SLOW = 190,
  NORMAL = 225,
  FAST=255
};

class Car 
{
  private:
  //motor 1 connections
  int in1 = 16;
  int in2 = 17;
  //motor 2 connections
  int in3 = 32;
  int in4 = 33;

  const int SPEED_CONTROL_PIN_1 = 21;
  const int SPEED_CONTROL_PIN_2 = 22;

  const int freq  = 3000;
  const int channel_0 = 0;
  const int channel_1 = 1;
  const int resolution = 8;

  speedSettings currentSpeedSettings;

  public:
  Car() 
  {
    pinMode(in1, OUTPUT);
    pinMode(in2, OUTPUT);
    pinMode(in3, OUTPUT);
    pinMode(in4, OUTPUT);
    pinMode(SPEED_CONTROL_PIN_1, OUTPUT);
    pinMode(SPEED_CONTROL_PIN_2, OUTPUT);


    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);

    ledcAttachPin(SPEED_CONTROL_PIN_1, channel_0);
    ledcAttachPin(SPEED_CONTROL_PIN_2, channel_1);


    ledcSetup(channel_0, freq, resolution);
    ledcSetup(channel_1, freq, resolution);

    // initialize default speed to SLOW

    //PENDING


  };
  void setCurrentSpeed(speedSettings newSpeedSettings)
    {
      Serial.println("car is changing speed...");
      currentSpeedSettings = newSpeedSettings;
    }





};

So, as far as I can tell, he created a new variable here called currentSpeedSettings.

  speedSettings currentSpeedSettings;

Before going forward the enum stores value as int

SLOW = 190,
  NORMAL = 225,
  FAST=255

But this function inside class

void setCurrentSpeed(speedSettings newSpeedSettings)
    {
      Serial.println("car is changing speed...");
      currentSpeedSettings = newSpeedSettings;
    }

I am not able to understand the function parameter

setCurrentSpeed(speedSettings newSpeedSettings)

So later in the code they have used this function like this

setCurrentSpeed(speedSettings::NORMAL);

So speedSettings is the enum and another parameter is newSpeedSettings, so basically I am not getting these two.

You are almost there - newSpeedSettings is a parameter of type speedSettings (like a typical function).

The line

is passing the enum value NORMAL. The :: is a scope resolution operator essentially telling the compiler that NORMAL in defined in the enum speedSettings.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.