Newbee, first sketch

I am trying to make a simple sketch that uses two switches to control the speed of a small 5v dc motor.

So far I have been successful in getting the witches to work independently to turn on an led.

However, when I added the serial readout only one switch successfully activated the led.

I decided to push on and see what happens.

I tried mapping both pins to the same outpin but it wont compile.

Spooler_Control.ino (2.06 KB)

OK so I managed to get it to upload but the led only turns fully on and doesn't turn off now.

I need it to act like a potentiometer.

Spooler_Control.ino (2.01 KB)

Hello!

If you could paste your code in it would mean people can quickly look at what you have without needing to download anything.

In your Arduino software press ctrl-A to select all then ctrl-C to copy all. Press reply and click the </> button, paste your code in between the bracket (it should auto focus the cursor to the middle). Ctrl-V to paste :slight_smile:

  if (FastPinState == LOW) {
    // turn LED on:
    digitalWrite(OutPin, -5);
  }
  if (SlowPinState == LOW) {
    // turn LED on:
    digitalWrite(OutPin, +5);
  }

I think your logic here is a bit confused. If both buttons are not pressed, then first you write the OutPin LOW and then you immediately write it HIGH. These are the only digitalWrites in your code, so it is the only place capable of changing the state of the LED. So I'm guessing if you push the fast pin button, the LED comes on for as long as you hold the button? If you push the slow pin button the LED goes off? And if you push neither button, the LED comes on?

What do you want to happen in the above three scenarios, and the additional scenario if you push both buttons at the same time?

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

OPs Code.

/*
  Button

 Turns on and off a light emitting diode(LED) connected to digital
 pin 13, when pressing a pushbutton attached to pin 2.


 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground

 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.


 created 2005
 by DojoDave <http://www.0j0.org>
 modified 30 Aug 2011
 by Tom Igoe

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/Button
 */

// constants won't change. They're used here to
// set pin numbers:
const int FastPin = 6;     // the number of the pushbutton pin
const int SlowPin = 3;     // the number of the pushbutton pin
const int OutPin =  13;      // the number of the LED pin

// variables will change:
int FastPinState = 0;         // variable for reading the pushbutton status
int SlowPinState = 0;         // variable for reading the pushbutton status
int OutPinState = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(OutPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(FastPin, INPUT);
  pinMode(SlowPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  FastPinState = digitalRead(FastPin);
  SlowPinState = digitalRead(SlowPin);

  // map it to the range of the analog out:
  OutPinState = map(FastPinState, 0, 1023, 0, 255);
  OutPinState = map(SlowPinState, 0, 1023, 0, 255);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (FastPinState == LOW) {
    // turn LED on:
    digitalWrite(OutPin, -5);
  }
  if (SlowPinState == LOW) {
    // turn LED on:
    digitalWrite(OutPin, +5);
  }

  
  // read the input pin:
  {int OutPinState = digitalRead(OutPin);
  // print out the state of the button:
  Serial.println(OutPinState);
  delay(100);        // delay in between reads for stability
  }   }

Tom... :slight_smile:

Hi,
You need to realise that
digitalRead(FastPin) and digitalRead(SlowPin) can only be 1 or 0

digitalWrite(OuputPin, HIGH) or digitalWrite(OutputPin,LOW)

Also your input buttons need to be wired correctly.

If your buttons are switching digital input to 5V, then you need on each digital input a 10K resistor from the digital input to gnd.

This makes sure the digital input goes to 0V when the button is not on.

So you can see posting a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png is very important.

Please do not use a Fritzy image.....

Thanks.. Tom.. :slight_smile:
PS Look in the IDE and go to Examples, Digital, Button.

/*

 Spooler Control


 */
// constants won't change. They're used here to
// set pin numbers:
const int FastPin = 6;     // the number of the pushbutton pin
const int SlowPin = 3;     // the number of the pushbutton pin
const int OutPin =  9;      // the number of the LED pin

// variables will change:
int FastPinState = 0;         // variable for reading the pushbutton status
int SlowPinState = 0;         // variable for reading the pushbutton status
int OutPinState = (0, 225);        // value output to the PWM (analog out)

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(OutPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(FastPin, INPUT);
  pinMode(SlowPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  FastPinState = digitalRead(FastPin);
  SlowPinState = digitalRead(SlowPin);

  // map it to the range of the analog out:
  OutPinState = map(OutPin, FastPinState, SlowPinState, 0, 255);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (FastPinState == LOW) {
    // turn LED on:
    analogWrite(OutPin,OutPinState-5);
  }
  if (SlowPinState == LOW) {
    // turn LED on:
    analogWrite(OutPin,OutPinState+5);
  }

  
  // read the input pin:
  {int OutPinState = digitalRead(OutPin);
  // print out the state of the button:
  Serial.println(OutPinState);
  delay(100);        // delay in between reads for stability
  }   }

sj_baird:
So far I have been successful in getting the witches to work independently

Well done witches are notorious for working in a coven.

As Tom said how have you wired up the switches, without that vital information we can't tell if the code is right.
why are you still ignoring ever bit of advice you have been given? If you don't understand anything the ask, do not ignore it.

sj_baird:
I am trying to make a simple sketch that uses two switches to control the speed of a small 5v dc motor.

So far I have been successful in getting the witches to work independently to turn on an led.

However, when I added the serial readout only one switch successfully activated the led.

I decided to push on and see what happens.

I tried mapping both pins to the same outpin but it wont compile.

What do you want to run?

Two buttons, one LED, one DC motor.

Maybe something like that:
First button controls the LED (ON//BLINKING/OFF)?

Second button controls the motor (SLOW/FAST/OFF)?

Or different?

Please describe as detailed as possible the working logic and use plain words to decribe what you want!

My imagination is not enough to guess what you want. Possibly LED fading up and down, or maybe blinking LED, controlling motor fast and slow and maybe also rotating clockwise and anti-clockwise, or running in intervalls like five seconds motor running and five seconds motor stop in each 10-second-interval.

With a detailed description, perhaps someone could help with the sketch.

OK thank you, I want trying to be rude I just ran out of energy and needed sleep. Also the sight kept messing up and I couldn't get the things posted that I was asked for.

This is the current state of my sketch.

/*

 Spooler Control


 */
// constants won't change. They're used here to
// set pin numbers:
const int FastPin = 6;     // the number of the pushbutton pin
const int SlowPin = 3;     // the number of the pushbutton pin
const int OutPin =  A0;      // the number of the LED pin

// variables will change:
int FastPinState = 0;         // variable for reading the pushbutton status
int SlowPinState = 0;         // variable for reading the pushbutton status
int OutPinState = (0, 255);        // value output to the PWM (analog out)

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // initialize the OutPin pin as an output:
  pinMode(OutPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(FastPin, INPUT);
  pinMode(SlowPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  FastPinState = digitalRead(FastPin);
  SlowPinState = digitalRead(SlowPin);

  // map it to the range of the analog out:
  OutPinState = map(OutPin, FastPinState, SlowPinState, 0, 255);  
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:

  if (FastPinState == LOW) {
    // decrease speed:
   // for(int OutPinState = 0; OutPinState <= 255; OutPinState-=5)
    analogWrite(OutPin,OutPinState+5);
  }
  if (SlowPinState == LOW) {
    // increase speed
    // for(int OutPinState = 255; OutPinState >= 0; OutPinState+=5)
    analogWrite(OutPin,OutPinState-5);
  }

  
  // read the input pin:
  {int OutPinState = analogRead(OutPin);
  // print out the state of the button:
  Serial.println(OutPinState);
  delay(1000);        // delay in between reads for stability
  }   }

I am trying to use one push-button (end-stop) to increase the speed while pressed, and the second push-button (end-stop) to decrease the speed while pressed. I have a 5v servo that has been modified to act as a high torch motor, however, I am using an LED to test my sketch.

In short I want to replace the potentiometer that I manually control with position switches that adjust the speed for me.

I finally figured out that my photo size was the problem.

  1. You should never connect a motor directly to Arduino pin (except for very small motors maybe). You should have something to drive it.
  2. Your sketch is full of mistakes. I.e. you can analogWrite only pins with PWM (A0 is not). It won't be true analog value but quickly changing HIGH and LOW. If you analogRead pin doing PWM you should get 0 or 1023 nearly randomly. Also you clearly don't understand how variables work. You should look into it and try to play with them. Try to Serial.print everything to see if it is doing what you expect. It will not. Do something like

Serial.print("OutPinState is now: ");
Serial.println(OutPinState);

each time you think a variable should be changed (not just this one) and see what is happening.

Hi,
OPs Pics


Tom..... :slight_smile:
PS.Please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

OK, Took out the LED, and now have my motor controller and servo in play. Ill be trying to figure out how to get it hooked to Pin9 and get ti to turn on full and off.

I also made some of the changes that I think you were saying to do. (Please excuse the variable change the originals seemed to be confusing.

/*

 Spooler Control


 */
// constants won't change. They're used here to
// set pin numbers:
const int FastSwitch = A0;     // the number of the pushbutton pin
const int SlowSwitch = A1;     // the number of the pushbutton pin
const int Motor =  9;      // the number of the LED pin

// variables will change:
int FastSwitchState = 0;         // variable for reading the pushbutton status
int SlowSwitchState = 0;         // variable for reading the pushbutton status
int MotorState = (0, 255);        // value output to the PWM (analog out)

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // initialize the Motor pin as an output:
  pinMode(Motor, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(FastSwitch, INPUT);
  pinMode(SlowSwitch, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  FastSwitchState = analogRead(FastSwitch);
  SlowSwitchState = analogRead(SlowSwitch);

  // map it to the range of the analog out:
  float  MotorState = map(Motor, 0, 1023, 0, 255);  
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:

  if (FastSwitchState == LOW) {
    // decrease speed:
   // for(int MotorState = 0; MotorState <= 255; MotorState=-.05)
    analogWrite(Motor,MotorState=MotorState-.05);
  }
  else if (SlowSwitchState == LOW) {
    // increasse speed
    // for(int MotorState = 255; MotorState >= 0; MotorState=+.05)
    analogWrite(Motor,MotorState=MotorState+.05);
  }


  
  // read the input pin:
  {int MotorState = analogRead(Motor);
  // print out the state of the button:
  Serial.print("MotorState is now: ");
  Serial.println(MotorState);
  delay(1000);        // delay in between reads for stability
  }   }

OK Tom I'll work on it.

Here is the schematic.

Hi,
What is the motor controller?
OPs Circuit

Thanks.. Tom.. :slight_smile:

You don't need a motor controller to drive a servo. You should not have anything in the ground of the servo motor, what are those things anyway. Why is the pulse input to the servo shorted to the servo's 5V.

In short the circuit is not viable at all.

sj_baird:
I am trying to make a simple sketch that uses two switches to control the speed of a small 5v dc motor.

So far I have been successful in getting the witches to work independently to turn on an led.

However, when I added the serial readout only one switch successfully activated the led.

Let go back to two buttons and an LED, forget the motor for the moment.

Tom... :slight_smile:

Hi,
Try this circuit and this code, one switch should turn the LED ON, the other OFF.

int OnPin = 3;
int OffPin = 6;
int LEDPin = 9;
bool OnSw = false;
bool OffSw = false;


void setup() {
  pinMode(OnPin, INPUT);
  pinMode(OffPin, INPUT);
  pinMode(OnPin, INPUT_PULLUP);
  pinMode(OffPin, INPUT_PULLUP);
  pinMode(LEDPin, OUTPUT);

}

void loop() {
  OnSw = digitalRead(OnPin);
  OffSw = digitalRead(OffPin);
  if ( OnSw == false)
  {
    digitalWrite(LEDPin, HIGH);
  }
  if ( OffSw == false)
  {
    digitalWrite(LEDPin, LOW);
  }
}

Tom... :slight_smile:
PS, I haven't got any arduino gear with me to try it but it should work. :o