Motor controlled by button state

Hi all,

I am needing help in regards to controlling my 12v motor with a button. The first step (successful) was to code in a soft-start for my motor so that it gradually gets to full speed over 2.5 seconds. Then I am needing to include a function where each time I press the button it changes the state of the motor i.e. if on it turns motor off, or if off it initiates the soft-start of the motor function.

I have included how I have wired this up as well as the code I have written.
Any tips would be great thankyou!

code:

int motorPin = 3;
int s = 0;
int motorSpeed = s;
int buttonPin = 2;
int oldButtonState;
int buttonState;
int lastState;
int state = 1;

void setup()
{
pinMode(motorPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
lastState = digitalRead(buttonPin);
}

void loop()
{
if (s < 255) { // if motor is not at full speed
analogWrite(motorPin, s); //write to the motor the value of s (initally s == 0)
delay (10);
s == (s++); //each loop, add 1 to the value of s -- every 10 milliseconds this will keep increasing until 255
}

if (s > 0 && (buttonState != oldButtonState)) { //if the new button state doesnt = the previous button state & s is over 0 i.e. motor is on
state = 1; //call on case 1 (motor is on, button pressed, turn motor off)
}

else if ((buttonState != oldButtonState) && s == 0) { //if the button changes state and motor is off
state = 2; // call on case 2 (motor is off, turn motor on slowly i.e. call on void loop function.
}
}

void buttonPress(int state) {
switch (state) {
case 1: //if motor is on and button is pressed, turn motor off

buttonState = lastState;// read new button state
if (buttonState != oldButtonState) //if the button changes state
s == 0; //turn motor speed off
break;

case 2: //if motor is off and button is pressed, start loop again
buttonState = lastState;// read new button state
if (buttonState != oldButtonState) //if the button changes state
loop(); //if button changes state, call on void loop function
break;

}
}

motor soft start wiring (2).png

updated code (still not working but makes much more sense...)

int motorPin = 3;
int buttonPin = 2;
int s = 0;
int motorSpeed = s;
int buttonState;
int lastState;
int state = 0;

void setup()
{
pinMode(motorPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
buttonPress(state);
}

void loop()
{
digitalRead(buttonPin);

if ((buttonState != lastState) && s == 0) { //if button changes state and motor is OFF:
state = 1; //soft-start motor
}
else if ((buttonState != lastState) && s > 0) { //if button changes state and motor is ON:
state = 2;
}

}
void buttonPress(int state) {
switch (state) {
case 1: //MOTOR ON

if (s < 255) { // if motor is not at full speed
analogWrite(motorPin, s); //write to the motor the value of s (initally s == 0)
delay (10);
s == (s++); //each loop, add 1 to the value of s -- every 10 milliseconds this will keep increasing until 255
}
break;

case 2: // MOTOR OFF
s == 0;
break;
}
}

You need to add a line

buttonPress(state);

into loop()

And please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

...R