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.

It compiles but doesnt control the LED at all I need it to act like a potentiometer.

Spooler_Control.ino (2.01 KB)

/*
  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, 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(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:
    analogWrite(OutPin, -5);
  }
  if (SlowPinState == LOW) {
    // turn LED on:
    analogWrite(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
  }   }

That is where this started, I am not trying to control an LED in the end I want variable control of a 5v dc motor.

(Update) I not can turn the LED on and off but still cant control it like a POT

Spooler_Control.ino (2 KB)

For starters…

 // map it to the range of the analog out:
  OutPinState = map(FastPinState, 0, 1023, 0, 255);
  OutPinState = map(SlowPinState, 0, 1023, 0, 255);

Your taking a 1 or 0 and telling it to map out an analog value.

Umm… How to say this?

You tell your helper. “I’m going to give you $0…$1023 ok?”

He says “Ye$!.”

You say, “If I give you $0, you don’t need to give me anything back. Fair?”

“Yes…”

“If I give you $1023 you only have to give back $255. OK?” That sounds good to him too.

Then you hand him Zero dollars. What do you get back huh? Big fat Zero.

No problem. Now give him $1. What do you get back? We’re talking whole dollars here, no change.

You get back Zero again!

So, what those two lines of code are doing is setting both pinState variables to zero no matter what you do.

-jim lee

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);

It now looks like this, and it actually does turn the light fully on and fully off. How can I tell it I want a variable out put>

Look, you’re going to get snapped at.

A) Post your COMPLETE sketch.
B) Post it in code tags. Looks like </> up there on the upper left corner.

People get real salty around here if you don’t do that.

-jim lee

/*

 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 =  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, 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
  }   }
  FastPinState = digitalRead(FastPin);
  SlowPinState = digitalRead(SlowPin);

  // map it to the range of the analog out:
  OutPinState = map(OutPin, FastPinState, SlowPinState, 0, 255);

FastPinState will be 0 or 1. SlowPinState will be 0 or 1.

You are then trying to map a pin number (13) from the range 0 to 1, or 0 to 0, or 1 to 1, or 1 to 0, to the range 0 to 255. That makes NO sense.

Why do you even have the call to map()? What are you trying to do?

  // read the input pin:
  {int OutPinState = digitalRead(OutPin);

It seems stupid to name an input pin OutPin.

In an attempt to not break the rules I will ask you to post any further responses to this posting.

https://forum.arduino.cc/index.php?topic=455340.0

Yes it is pretty dumb to name it that but it made since when I started the sketch. If I upload any more code it will be changed to "motor".

For the other part I really don't know what I'm doing accept that I'm not looking for a variable I'm looking for the state of the switch and am using it as the high and low (1 or 0).