HELP!! AYUDA!!

I need some help.
I'm with an Arduino project. I must control a DC motor with pushbuttons. One of these must turn the engine on and off (In the code it is the pin 2). Another push button must accelerate the speed (pin 24) and the other one must decelerate (pin26). But my code does not work.

(Estoy con un proyecto de Arduino . Debo controlar un motor DC con pulsadores. Uno de estos debe encender y apagar el motor (En el codigo es el pin 2). Otro pulsador debe acelerar la velocidad (pin 24) y el otro debe desacelerar (pin26). Pero mi codigo no funciona)

const int control = 9; //motor
const int detener = 2; //Stop
const int moreSpeed = 24;
const int lessSpeed = 26;

volatile int Speed=0;
volatile int Estado=0;
long T0 = 0;

void Controlar();

void setup()
{
pinMode(control, OUTPUT);
pinMode(detener, INPUT);
pinMode(moreSpeed, INPUT);
pinMode(lessSpeed, INPUT);

attachInterrupt(0,Controlar, RISING);
}

void loop()
{

if(Estado == 1)
{
Speed = 128;
analogWrite(control, Speed);
delay(30);
}
if(Estado == 0)
{
Speed = 0;
analogWrite(control, Speed);
delay(30);
}

if(digitalRead(moreSpeed) == HIGH)
{
Speed = Speed + 26;
if(Speed > 230)
{Speed = 230;}
analogWrite(control, Speed);
delay(30);
}

if(digitalRead(lessSpeed) == HIGH)
{
Speed = Speed - 26;
if(Speed < 26)
{Speed = 26;}
analogWrite(control, Speed);
delay(30);
}

}

void Controlar()
{
if(millis() > T0 + 150)
{
if(Estado == 0)
{
Estado=1;
}
else if(Estado == 1)
{
Estado=0;
}
T0=millis();
}
}

I would appreciate the help! (Apreciaría mucho la ayuda!)

Arduinista:
But my code does not work.

What does it actually do?

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

In general if you want a responsive program do not use delay(). Have a look at how millis() is used to manage timing without blocking in Several things at a time

I suspect you need to read all the buttons and save their values in variables. Then your function to control the motor can take account of the saved values. It may also hep to have a variable to record the state of the motor - Stopped, Accelerating, MaxSpeed, Decelerating.

...R
Planning and Implementing a Program

long T0 = 0;

. . .

T0=millis();

Change to:

unsigned long T0 = 0;

.

Change to:

unsigned long T0 = 0;

Better still, pretend to know, and demonstrate, what you're doingunsigned long T0;

   if(digitalRead(moreSpeed) == HIGH)
    {
      Speed = Speed + 26;
      if(Speed > 230)
        {Speed = 230;}
      analogWrite(control, Speed);
      delay(30);
    }
]

Your speed ranges from 26 to 230.
You step your speed in increments of 26. So there's 7 steps from slow to fast.
You delay by 30ms each time the speed steps. So is will take 210ms - a fifth of a second - to go from 26 to 230.
That's pretty fast. Maybe if you press and release your speed control button really fast, you'll see a value that isn't either the maximum or the minimum.

But that isn't your problem. Estado is always either 0 or 1 - you never set it to any other value. Your loop() therefore always sets the speed to either 128 or 0 every time it runs.

Oh - you have if() statements to constrain the speed to the correct range. The arduino API supplies a funbction named 'constrain' which is built for this purpose.