Newbie needs help

Hi there,

I’m a biomedical engineer and I’m using Arduino Duemilanove connected to an Unipolar motor to rotate a platform.
I’m using VS C++ 2008 Express and the following code to send data through a COM port:

#include “stdafx.h”

using namespace System;
using namespace System::IO::Ports;

int main(array<System::String ^> ^args)

String^ answer;
String^ portName;
int baudRate=9600;
Console::Write(“Arduino COM v1.0 - Nuno Fernandes\n______________________________________________\n”);
Console::WriteLine(“Introduza a porta de comunicação e prima ENTER \n(Exemplo: COM4)”);
// Configurar a ligação ao arduino
SerialPort^ arduino;
arduino = gcnew SerialPort(portName, baudRate);
// abrir a porta de comunicação
Console::WriteLine(“Introduza o número de passos.(Com 3 dígitos: _ _ _)”);
arduino->WriteLine(“X-”+answer); // send 1 to arduino
arduino->WriteTimeout = 100;
Console::WriteLine(“Tentar de novo? s/n”);
// get answer
// clear the screen

// Terminar o programa
Console::Write(“Prima ENTER para fechar a aplicação”);

The problem is that I run the program once, and the motor rotates, but if I run the program again, nothing happens. I have to wait ~5 min to rerun the program and send data to Arduino.

Here is the Arduino code:

#include <Stepper.h>

#define STEPS 48 // change this depending on the number of steps
// per revolution of your motor
#define motorPin1 8
#define motorPin2 10
#define motorPin3 9
#define motorPin4 11
#define laserPin 12
#define ledPin 13

// initialize of the Stepper library:
Stepper motor1(STEPS, motorPin1, motorPin2, motorPin3, motorPin4);

// monta a palavra da serial
char inBytes[5];
int n = 0;

void setup() {
Serial.begin( 9600 ); // Initialize the Serial port
motor1.setSpeed( 60 ); // rpm
pinMode(laserPin, OUTPUT); // set up the LASER pin
pinMode(ledPin, OUTPUT); // set up the LED pin
blink(3); // blink the LED:

// Palavras de controle para a serial:
// 5 letras: 1-Motor (X, Y, Z); 2-Dire[ch65533][ch65533]o(0-Frente, 1-Tr[ch65533]s); 3,4,5-(n[ch65533]mero de passos)
// Exemplo:
// “X122” - Mover motor 1 para frente com 122 passos
// “Y1030” - Mover motor 2 para tr[ch65533]s com 30 passos
// “Z0999” - Mover motor 3 para frente com 999 passos

void loop() {

if ( Serial.available() > 0 && n < 5 )
inBytes[n] =;
n ++;
if ( n > 4 )

if (n > 4)

Serial.print("Recebido: ");
for ( int i = 0; i <= 4; i++ )
Serial.print( inBytes );

  • //Serial.println();*

  • // numero de passos (value) e sentido dos passos*

  • //“X-123”*
    // 1100+210+3 = 123 –> Converter de char para int
    _ int value = (inBytes[2] - 48)*100 + (inBytes[3] - 48)*10 + (inBytes[4] - 48);

  • if ( inBytes[1] == ‘-’ )*

  • value = - value;*

  • Serial.print(" Value: "); *

  • Serial.println(value); //DEC);*

  • // controle do motor*

  • switch (inBytes[0]) {*

  • case ‘X’:*

  • motor1.step( value );*

  • break;*

  • case ‘S’:*

  • motor1.setSpeed( abs(value) ); // rpm*

  • break;*

  • case ‘L’:*

  • if ( inBytes[1] == ‘1’ ) digitalWrite(laserPin, HIGH); // set the LASER ON*

  • if ( inBytes[1] == ‘0’ ) digitalWrite(laserPin, LOW); // set the LASER OFF*

  • break;*

  • }*

  • //Reset palavra*

  • for (int i = 0; i <= 4; i ++)*
    _ inBytes = 0;_

* }*

_ /delay(500);_
Serial.write( inByte1 );_
Serial.write( inByte2 );_
_ /_
// Blink the reset LED:
void blink( int howManyTimes )
* int i;*
* for (i=0; i< howManyTimes; i++) {*
* digitalWrite(ledPin, HIGH);*
* delay(200);*
* digitalWrite(ledPin, LOW);*
* delay(200);*
* }*
Could anyone help me?
Thank you very much and best regards,

Could anyone help me?

Yes, when posting code, use the # button on the toolbar.

Sounds like you're blowing a thermal fuse. Post a circuit diagram.

Hi there, do you want me to rewrite my post with the "#" enabled? sorry for this question, but I can I upload a picture?

I have a few info to add:

  • Arduino receives the data and the LED blinks. Only the command to the motor doesn't work (I'm saying this based on what I see in the board.
  • I believe my application (in C++) doesn't really closes the communication thus disabling the reception of more information by Arduino.
  • I tried to run the application again but it didn't work=/

  • Circuit info:

ARDUINO pin 8 - ULN2804A pin 1 - pin 18 - MOTOR pin 1 ARDUINO pin 9 - ULN2804A pin 2 - pin 17 - MOTOR pin 2 ARDUINO pin 10 - ULN2804A pin 3 - pin 16 - MOTOR pin 3 ARDUINO pin 11 - ULN2804A pin 4 - pin 15 - MOTOR pin 4

do you want me to rewrite my post with the "#" enabled?

You could just edit your original post.

Circuit info:

Not good enough, how have you got the grounds and power connected. Where are these coming from? A schematic is essential so we can see what you have missed.

How do I upload a picture of my circuit??

Arduino receives the data and the LED blinks.

The only call I see to blink the LED is in setup. If the led blinks after receiving serial data, something has caused the Arduino to reset. Nothing in the code appears to do that, so it must be a hardware problem.

How are you powering the Arduino and motors?

To post a picture, you need to host it somewhere, and post a link to the picture,

Hi there,

Here is the link to my circuit:

Thank you all.

As mentioned before:-

Sounds like you're blowing a thermal fuse.

This is probably because the motor windings are taking too much current from the USB line. Do you know the coil's resistance? Use it to calculate the current in one coil, then double it because two coils will be on at the same time.

You can only take 500mA from the USB port and normally stepping motors take more than this. You will probably have to power the motor from some external power supply.

Thank you for your help Grumpy_Mike. At this moment I am using one 1K resistance in series with 1 Led in each exit of the ULN because I don't have the motor with me. I am using this method so I can verify if the electrical current is running through each exit of the ULN as it should. Do you think this may also be a cause to this issue?

When you get round to using real motors connect pin 9 to pin 16.

As you are only using LEDs that puts the fuse blowing theory out of the window. I wish you would have been honest from the start with what you had.

It looks like it is your code that is screwy although I can't spot exactly where it is wrong. I would not go and gather anything from the serial port until all the bytes are in. It looks like you might be leaving something in the buffer (a 5th byte) that is interfering with your sequence in the rest of the code. Remember you can encounter the first if() with say three bytes in you could reset n while still leaving bytes in the buffer. Try re writing this section of code so that you don't do anything until all the bytes are in.