Problem with PWM

Hi all,

I am newbie to arduino programming. My arduino program consists of 2 buttons with power supply through the relay. The program should be able to power two linear actuator to achieve two targeted position and run at half and full speed respectively with PWM. Also, there are extending and retracting functions for both actuators. I am not sure whether the code is correct or not, can anyone help me to verify?

Thanks.

seeloonusethis.txt.ino (4.93 KB)


Notice that servo motors are used to drive the linear actuator. Here is the corrected version.

seeloonusethis.txt.ino (4.87 KB)

In future, please post your code rather than attaching it, so we don’t have to download it and open it in the IDE to view it. And when you do, please enclose it within [code]code tags[/code].
Anyway, here it is:-

//define global variables

// constants won't change. They're used here to set pin numbers:
const int button1Pin = 2;     // the number of the pushbutton1 pin
const int button2Pin = 4;     // the number of the pushbutton2 pin
const int relay1Pin =  6;      // the number of the Realy1 pin
const int relay2Pin =  7;      // the number of the Relay2 pin
const int relay3Pin =  8;     // the number of the Relay3 pin
const int relay4Pin =  9;     // the number of the Relay4 pin
const int sensorPin = 0;    // select the input pin for the potentiometer

// variables will change:
int button1State = 0;         // variable for reading the pushbutton status
int button2State = 0;         // variable for reading the pushbutton status
int sensorValue = 0;  // variable to store the value coming from the sensor

int goalPosition = 350;
int CurrentPosition = 0;
boolean Extending = false;
boolean Retracting = false;

void setup()
{
    // put your setup code here, to run once:
    //start serial connection
    Serial.begin(9600);

    pinMode(motor_pin, OUTPUT);

    // initialize the pushbutton pin as an input:
    pinMode(button1Pin, INPUT);
    pinMode(button2Pin, INPUT);
    // initialize the relay pin as an output:
    pinMode(relay1Pin, OUTPUT);
    pinMode(relay2Pin, OUTPUT);
    pinMode(relay3Pin, OUTPUT);
    pinMode(relay4Pin, OUTPUT);

    //preset the relays to LOW
    digitalWrite(relay1Pin, LOW);
    digitalWrite(relay2Pin, LOW);
    digitalWrite(relay3Pin, LOW);
    digitalWrite(relay4Pin, LOW);
}

void loop()
{
    for (int i = 0; i < 600; i++)
    {
        Serial.print("i = ");
        Serial.println(i); // number of cycle

        // put your main code here, to run repeatedly:
        // read the value from the sensor:
        CurrentPosition = analogRead(sensorPin);

        // print the results to the serial monitor:
        Serial.print("Current = " );
        Serial.println(CurrentPosition);
        Serial.print("\t Goal = ");
        Serial.println(goalPosition);

        // read the state of the pushbutton values:
        button1State = digitalRead(button1Pin);
        button2State = digitalRead(button2Pin);

        if (button1State == HIGH)
        {
            // set new goal position for smaller size of air-cond panel
            goalPosition = 300;

            if (goalPosition > CurrentPosition)
            {
                Retracting = false;
                Extending = true;
                analogWrite(relay1Pin, 127);  // first actuator extending
                digitalWrite(relay2Pin, LOW);
                digitalWrite(relay3Pin, 127);  // second actuator extending
                digitalWrite(relay4Pin, LOW);
                Serial.println("Extending");

                if (Extending = true && CurrentPosition > goalPosition)
                {
                    //we have reached our goal, shut the relay off
                    digitalWrite(relay1Pin, LOW); // first actuator stop extending
                    digitalWrite(relay3Pin, LOW); // second actuator stop extending
                    boolean Extending = false;
                    Serial.println("IDLE");
                }
            }
            else if (goalPosition < CurrentPosition)
            {
                Retracting = true;
                Extending = false;
                digitalWrite(relay1Pin, LOW);
                analogWrite(relay2Pin, 127);
                digitalWrite(relay3Pin, LOW);
                analogWrite(relay4Pin, 127);
                Serial.println("Retracting");

                if (Retracting = true && CurrentPosition < goalPosition)
                {
                    //we have reached our goal, shut the relay off
                    digitalWrite(relay2Pin, LOW);
                    digitalWrite(relay4Pin, LOW);
                    boolean Retracting = false;
                    Serial.println("IDLE");
                }
            }
        }

        if (button2State == HIGH)
        {
            // set new goal position for bigger size of air-cond panel
            goalPosition = 500;

            if (goalPosition > CurrentPosition)
            {
                Retracting = false;
                Extending = true;
                analogWrite(relay1Pin, 255);
                digitalWrite(relay2Pin, LOW);
                analogWrite(relay3Pin, 255);
                digitalWrite(relay4Pin, LOW);
                Serial.println("Extending");

                if (Extending = true && CurrentPosition > goalPosition)
                {
                    //we have reached our goal, shut the relay off
                    digitalWrite(relay1Pin, LOW);
                    digitalWrite(relay3Pin, LOW);
                    boolean Extending = false;
                    Serial.println("IDLE");
                }
            }
            else if (goalPosition < CurrentPosition)
            {
                Retracting = true;
                Extending = false;
                digitalWrite(relay1Pin, LOW);
                analogWrite(relay2Pin, 255);
                digitalWrite(relay3Pin, LOW);
                analogWrite(relay4Pin, 255);
                Serial.println("Retracting");

                if (Retracting = true && CurrentPosition < goalPosition)
                {
                    //we have reached our goal, shut the relay off
                    digitalWrite(relay2Pin, LOW);
                    digitalWrite(relay4Pin, LOW);
                    boolean Retracting = false;
                    Serial.println("IDLE");
                }
            }
        }
    }

    delay (500);
}

Have you tried running the code to see if it works?

It’s hard for us to know unless you also post a clear, well-labelled schematic diagram.

I don’t quite get what you mean by “Notice that servo motors are used to drive the linear actuator.” either. There’s nothing relating to either in your code, only relays.

First thing - it definitely doesn't compile. 'motor_pin' hasn't been declared.

Edit: Just another quick point - please hit Ctrl-T or ">Tools >Auto format" before posting your code, to format it correctly with decent indenting. Your formatting is shocking. :astonished:

CoulsonYang:
I am not sure whether the code is correct or not, can anyone help me to verify?

Does it work?