I recently bought an Arduino Uno and it is not running efficiently or accurately with my stepper motor because, apparently, there is no data available. I would like to know why that would be the case, and if there is any way I could fix it?
Thank you so much!
Try here
I tried adding a library with no avail. Is there anything else I can do?
You could tell us what your code looks like, how your hardware is wired, what your perceived problem is...
lcr2139:
... apparently, there is no data available.
How is this apparent? Post your sketch, wiring, link to parts used, and describe how this becomes apparent.
it is not running efficiently or accurately with my stepper motor
Efficiency is a function of converting current into torque and speed and is a function of your motor.
Accuracy or loss of it, is from trying to drive your motors too fast.
None of this has anything to do with data being available.
ARDUINO CODE:
#include "Arduino.h"
#define DIR_PIN 2
#define STEP_PIN 3
void setup() {
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
Serial.begin(9600);
}
int incomingByte = 0; // for incoming serial data
void loop() {
// send data only when you receive data:
//if (Serial.available() > 0) {
// say what you got:
Serial.print("I received: ");
int a=Serial.read();
Serial.print(a);
// Serial.println(incomingByte, DEC);
}
MATLAB CODE:
s = serial('COM3', 'BaudRate', 9600);
fwrite(s, 50)
??? Error using ==> serial.fwrite at 199
OBJ must be connected to the hardware with FOPEN.
fopen(s);
fwrite(s, 50);
fscan(s);
??? Undefined function or method 'fscan' for input
arguments of type 'serial'.
fscanf(s)
Warning: A timeout occurred before the Terminator was
reached.
ans =
I received: -1I received: -1I received: -1I received: -1I received: 50I received: -1I received: -1I received: -1I received: -1I received: -1I received: -1I received: -1I received: -1I received: -1I received: -1I received: -1I received: -1I received: -1I received: -1I received: -1I received: -1I received: -1I received: -1I received: -1I received: -1I received: -1I received: -1I received: -1I received: -1I received: -1I received: -1I received: -1I received: -1
NOTE: In the Arduino code, I took out the IF statement asking if the data was available because it would not work with it. Do to this reason and also the output of the above code, I realize that the problem with my Arduino is that it is not recieving data, so none is available.
I took out the IF statement asking if the data was available because it would not work with it.
And it doesn't work without it does it? So put it back.
Post code using the code tags. Go back and modify that last post. Select the code part, click on the # icon next to the quote and save it again.
Your ardino code has nothing to do with the stepping motor problem you reported at first. Please state what your problem is.
I took out the IF statement asking if the data was available because it would not work with it.
It wouldn't compile with that line present (no matching brace), but that's not the same as not working
From the errors you list it looks like the problem is with MatLab. MatLab problems are not Arduino problems... Can you talk to the Arduino using the Serial Monitor?
No, I cannot talk to the Arduino using the Serial monitor either, so it is not a Matlab problem. I used to be able to write to the arduino and then it would work, but now i am unable to write to the arduino at all. my friend says it works on her mac computer, but it does not work on three windows computers. does anyone know why that is?
has anyone else had this much trouble using arduino in windows?
Try putting in a simple sketch that echoes back whatever it receives.
Use the serial monitor, and don't forget to match the bit rate.
You haven't told us which IDE you're using or which OS.
Edit:
has anyone else had this much trouble using arduino in windows?
OK, so you're using some flavour of Windows.
No, I have not had any trouble using Arduino with Windows XP, Vista or 7.
Haven't tried 8 yet.
Try this:
Simple serial port echo
void setup()
{
// start serial port at 9600 bps:
Serial.begin(9600);
}
void loop()
{
// if we get a character, echo it:
if (Serial.available() > 0) {
// get incoming char:
Serial.write((char)Serial.read());
}
}
Run the attached matlab script and see what happens... The arduino should ask if you can hear it.
First things first, your Matlab code is incomplete. It gives you an error after the second line because you havent actually opened the serial port, just created an empty object. Which you corrected in the second attempt by using fopen()
The second issue is fopen() causes the DTR line to go low. This will trigger the arduino to autoreset, which is a pain as the computer wont bother to wait, which means that the data it sends is never recieved as it is busy restarting. I added a 5 second delay after fopen() to correct for this. There is a way I think to disable the DTR line, but I cant remember it.
After that it is plane sailing. It will send a string to the arduino which will echo it back. Then it will read in what has been echoed and print it out.
Notice that you did get the 50 back, it is just burried deep in a series of no responses, as it takes some time for the arduino to respond (remember your computer is going at GHz, the arduino on 16MHz).
Lastly, always remember to close (fclose(s)) and delete (clear s) the serial port when you are finished, else it will be locked by MATLAB until you exit the program, or issue the close and delete commands.
Tom.
Serial.m (484 Bytes)