by Carlos Cuevas
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = A0; // analog pin used to connect the potentiometer
int val;
int motorPin = 3; // define the pin the motor is connected to
// (if you use pin 9,10,11 or 3you can also control speed)
const int buttonPin = 7; // the pin that the pushbutton is attached to
//const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
/*
* setup() – this function runs once when you turn your Arduino on
* We set the motors pin to be an output (turning the pin high (+5v) or low (ground) (-))
* rather than an input (checking whether a pin is high or low)
*/
void setup()
{
myservo.attach(9);
pinMode(motorPin, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
/*
* loop() – this function will start after setup finishes and then repeat
* we call a function called motorOnThenOff()
*/
void loop() // run over and over again
{
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
// Serial.println(“on”);
//Serial.print(“number of button pushes: “);
// Serial.println(buttonPushCounter);
}
else {
// if the current state is LOW then the button
// wend from on to off:
// Serial.println(“off”);
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(5);
if (buttonPushCounter % 2 == 0)
{
// digitalWrite(ledPin, HIGH);
//analogWrite(motorPin, 150); //sets the new speed
int delayTime = 500; //milliseconds between each speed step
//Accelerates the motor
for(int i = 180; i < 186; i++)
{ //goes through each speed from 0 to 255
analogWrite(motorPin, i); //sets the new speed
delay(delayTime); // waits for delayTime milliseconds
}
//motorOnThenOff();
//motorOnThenOffWithSpeed();
// motorAcceleration();
}
else
{
analogWrite(motorPin, 0); //sets the new speed
}
}