digitalWrite help needed

Hey guys I am in desperate need of some help. I wrote a small program that will use the outputs of an RC module as inputs that control the direction of a motor. I made it so that the arduino should read digital values as inputs, then write digital values out to a relay circuit which drives the motor. I am measuring the voltage being outputted by output “motor” pins and it only gives me a 0.001 volt instead of five volts. Any suggestions?
/// Start of program

//Switches
int topswitch = 2;
int topswitchout = 3;

int bottomswitch = 5;
int bottomswitchout = 6;

//Control input
int upinput = 7;
int downinput = 8;

//Motor Output
int motorup = 9;
int motordown = 10;

// The setup() method runs once, when the sketch starts

void setup() {
// initialize the digital pin as an output:
pinMode(topswitch, INPUT);
pinMode(topswitchout, OUTPUT);

pinMode(bottomswitch, INPUT);
pinMode(bottomswitchout, OUTPUT);

pinMode(upinput, INPUT);
pinMode(downinput, INPUT);

pinMode(motorup, OUTPUT);
pinMode(motordown, OUTPUT);

}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()

{

digitalWrite(topswitchout, HIGH);
digitalWrite(bottomswitch, HIGH);

//Motorstop control
if (topswitch == HIGH)
{
digitalWrite(motorup, LOW);
}

if (bottomswitch == HIGH)
{
digitalWrite(motordown, LOW);
}

//MotorControl
if (upinput == HIGH)
{
digitalWrite(motorup, HIGH);
}
else
{
digitalWrite(motorup, LOW);
}

if (downinput == HIGH)
{
digitalWrite(motordown, HIGH);
}
else
{
digitalWrite(motordown, LOW);
}

}

You will need to provide more information.

For example: Is the RC module designed to be connected to a servo?

Sorry for my lack of info regarding the RC module. Basically what I did, was stripped the receiving end of an RC car so that I could use the remote to control the outputs running at about 6 volts or so. I cut the leads that would normally go to the RC motors and use those as the controlling inputs for the arduino.

I see no digitalRead() in your code....

The RC module operates at what voltage?

 if (topswitch == HIGH)

I think you need to write:

 if (digitalRead (topswitch) == HIGH)

yes!! haha I knew my code looked peculiar, I did forget the digitalRead command. Well thanks guys, that should help me advance further into my project. I am a newbie, so thank all of you for your help.

I put the digitalRead commands in and it seems to be working fine now, but I do have another problem. I'm only using the up and down controls of the RC module for my project. I basically need a timer that after the "up" command has been energized for a few seconds, the timer counts for about 5 to 10 seconds then energizes the "down" command and lowers the motor and stops waiting for user input again.

Hint: Use millis(), store them at the appropriate moment, and compare....

Thank you deSilva, I read up on the millis() command, I don't quite understand how to use it. As I understand it, the milli timer counts up from the moment of activation. Therefore, If I set a limit of 5000, once the milli timer hits that limit, it will do what I want it to. Once the timer hits the limit, I can use an "equal to " equation to compare the current state of the timer with the preset limit. Then if the condition is deemed "true", the "motor down" output activates which lowers the motor. Is this remotely correct?

You can consider millis() like your own watch. Do whatever you would when you had to use that :-)

Another hint: It sometimes happens that you might not exactly meet the aspired time (same with your time piece - you just looked a little bit too late). So use > to compare, not ==

millis() always counts. loop() is always running. So if you want to count time in your loop(), store the value of millis() and compare it .

if (desiredEventIsTrue) {
   someGlobalVariable = millis()+5000;
  // turn on/off whatever you need to
}

if ((millis() > someGlobalVariable) && (someGlobalVariable>0)) {
  // turn on/off whatever you need to
  someGlobalVariable = 0;
}

On the other hand, if you use subtraction involving millis() and the global variable, the code is even simpler:

if (desiredEventIsTrue) {
   someGlobalVariable = millis();
  // turn on/off whatever you need to
}

if (millis() - someGlobalVariable >= 5000) {
  // turn on/off whatever you need to
  someGlobalVariable = 0;
}

If millis() rolls over, the subtraction still results in a positive value.

the code is even simpler

IMO it is also more readable, but that is a matter of taste. What's more, it is correct (in contrast to the original code). However few Arduinos will run for days so the bug should stay mostly unnoticed.

There is another pitfall however. It is gladly forgotten that millis() is long, not int. comparing it to int-variables might soon end in some confusing situation..

comparing it to int-variables might soon end in some confusing situation.

Another reason to not use "magic numbers" in a program.

const unsigned long interval = 5000;

if (millis() - someGlobalVariable >= interval)

solves the problem with mixed types in an if statement as well as getting rid of a magic number (presuming that someGlobalVariable is also properly typed).

Thanks guys, You are a great help. I tried to adapt the advice you gave me to the code, let me know if you have any suggestions or if I made mistakes. code below....

/// Start of program

//Switches int topswitch = 2; int topswitchout = 3;

int bottomswitch = 4; int bottomswitchout = 5;

//Control input int upinput = 6; int downinput = 7;

//Motor Output int motorup = 12; int motordown = 13;

const unsigned long interval = 5000; unsigned long time = 0;

// The setup() method runs once, when the sketch starts

void setup() { // initialize the digital pin as an output: pinMode(topswitch, INPUT); pinMode(topswitchout, OUTPUT);

pinMode(bottomswitch, INPUT); pinMode(bottomswitchout, OUTPUT);

pinMode(upinput, INPUT); pinMode(downinput, INPUT);

pinMode(motorup, OUTPUT); pinMode(motordown, OUTPUT);

}

// the loop() method runs over and over again, // as long as the Arduino has power

void loop()

{

//Motorstop control if (digitalRead(topswitch) == HIGH) { digitalWrite(motorup, LOW); }

if (digitalRead(bottomswitch) == HIGH) { digitalWrite(motordown, LOW); }

//MotorControl if (digitalRead(upinput) == HIGH) { digitalWrite(motorup, HIGH); } else { digitalWrite(motorup, LOW); }

if (digitalRead(downinput) == HIGH) { digitalWrite(motordown, HIGH); } else { digitalWrite(motordown, LOW); }

///Timer Control

if (digitalRead(upinput)== HIGH) { time = millis(); // turn on/off whatever you need to }

if (millis() - time >= interval) { digitalWrite(motordown, HIGH); // turn on/off whatever you need to time = 0; }

}

The critical question is does this code do what you want it to do. We can’t answer that, and you didn’t.

If it does, and you are looking for suggestions for improvements, that’s one thing.

If you are looking for help solving a problem, you have to whack us with a clue-by-four.

So just an advanced - some would say: questionable- hint. Most programmers would write this:

 //MotorControl
 if (digitalRead(upinput) == HIGH)
 {
  digitalWrite(motorup, HIGH);
 }
 else
 {
  digitalWrite(motorup, LOW);
 }

as

  digitalWrite(motorup, digitalRead(upinput));

for the reason they find it more readable, and the code is shorter. However there are also reasons to do it the way you did...

Thanks for your help so far, guys. I have one last request for help. The code seems to be working as expected, but now I need a way to use a normally open push button to stop the motor from running. Essentially, when the motor runs it will drag up a piece of plexiglass. I want it so that once the plexiglass closes the switch. the motor stops. The project will have two different switches, one that stops the motor when the piece is raised, and another that stops the motor when it is lowered to the desired position. any suggestions?

  1. Set up a digital input, enable the internal pullup resistor.
  2. Connect a wire from one side of the NO switch to the input pin.
  3. Connect a wire from the other side of the NO switch to ground.
  4. When the switch makes, the input will change from HIGH to LOW.

Regards,

-Mike