Let's Make Robots!

My Arduino code question

I have all the makings for my bot and as I assemble it I have been trying to write the code myself instead of copying and pasting but have run into a problem.  Could someone explain why this bit of code doesnt work like I think it should. = )

int motorPin = 4;
int inPin = 2;  
int val = 0;    

void setup() {
  pinMode(motorPin, OUTPUT); 
  pinMode(inPin, INPUT);   

void loop(){
  val = digitalRead(inPin); 
  if (val == HIGH) {        
    digitalWrite(motorPin, LOW); 
  } else {
    digitalWrite(motorPin, HIGH);
    delay (3000); 

I have a switch circuit based on http://www.arduino.cc/en/Tutorial/Button  and a simple DC motor circuit with an NPN transistor with the base pin connected to Pin 4 on Arduino.  When I press the button the motor only runs for as long as I press the button.  Based on the above code I would assume I could press and release the button and the motor would run for 3 seconds.  This is my attempt at understanding how to program a bumper switch for the bot so I can obviously have it reverse and change direction. 

 Any help is appreciated.  

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Thank you all.  I couldnt get the Serialprintln to output any data to the console, I'm sure I had the syntax wrong or I just dont know enough to tell it what variable to print.  However, what CowGod said about the voltage dropping was possible since I was using the 5V out of the Arduino and even though it is a very small motor that must have been the problem.  I put the motor on its own power supply, shared the ground and it works. 

 I will definitely be powering my peripherals with a seperate supply on the actual bot but was just trying to test the code and I should have known better.. = P

Awesome, glad that solved the problem. To use the serial output, you need to put the Arduino program into serial monitor mode -- there's a button for that in the toolbar next to the button for uploading your code to the Arduino. You need to put a 'Serial.begin(9600);' in your setup() function (and be sure the serial monitor in the Arduino software is also set to 9600 baud), and then you can print regular text by simply doing:

Serial.println("Here's some text. I got to this line of the program!");

There's no way to concatenate strings, so if you want to print variables, you have to use a separate print statement for them, like:

Serial.print("Here's some text. Right now, counter = ");
Serial.println(". I hope that's helpful!");

But in this case, I don't think you needed to print any variables, just simple text statements to let you know how many times the different sections of code are being executed would help to track down resets. If you put a println() statement in the setup() function, then normally, it should only print that line once, when the chip first starts. If you see it print that line again, you know that it rebooted.


The delay() command appears to have no space between the y and the in-paren ( in the Arduino Code Reference. The delay above has a space between y and (3000) which is probably tripping up the interpretation.

Doncha just love micros that do exactly what you tell them, not what you mean?

Good catch, but in this case, that wasn't a problem -- the C compiler doesn't care about whitespace for the most part. It's fine to put spaces between the function name and the parenthesis. Ditto with things like if() and for() statements -- you can say:



if ( this  ==  that )

I prefer something more like the second, for readability's sake, but the compiler doesn't care how many spaces you use.


Good to know Dan, been tripped up by typos and other screwy things before. Just made me think of it as a possible.

I gotta get an Arduino sometime to play with.

First double check your positve and ground connections on your switch, through the resistor, and to the Arduino.  If you have them reversed, then you will get a HIGH signal when you push the button, not a LOW.

Also, you can output messages to the serial line and view them in the serial console to make sure your circuit is in the section of code you think it should be in.

Huh. I don't have any spare Arduinos so I haven't tried actually running your code, but just from looking at it, it does seem like it should do what you're shooting for -- keep checking the button as long as the button isn't pressed, but as soon as it's pressed, it'll run the motor for 3 seconds without making any checks for button status.

The only thing I can think of is that the Arduino is being reset by a voltage dip when you let go of the button, which makes it start from the beginning of the code again, and go straight back to motor off. You should probably put in a few debug statements (Serial.println()) to get a feel for exactly what's going on. I'd put one in each of the two sections of the if() statement, and definitely one in your setup() function (after the Serial.begin() of course) so you can see if it's being reset.


I don´t think you need delay, keep the loop function always running

sorry, I haven´t read the lines below.. 

I presume if you want the motor to run you should set the digitalPin (motorPin) to HIGH instead of LOW


The digitalPin LOW is at the beginning of the IF statement so the motor doesnt run when the switch is not pressed.  I set the digitlPin High after the ELSE. 

By keep the loop function always running how I do I do that or what do I need to do differently in the code?