Show Posts
|
|
Pages: [1] 2 3 ... 5
|
|
3
|
Using Arduino / Programming Questions / Run 2 pieces of code at the same time.
|
on: August 30, 2012, 07:55:01 am
|
I am making a homemade printer that draws shapes and I'm trying to make a circle but the stepper motors are not working good. They aren't as fluid as they should. Here is the code: void loop() { for (int angle = 0; angle <360; angle ++) { float angleRad = angle*2*PI/360; x = int(xInicial + cos(angleRad) * radi); y = int(yInicial + sin(angleRad) * radi); if (x <0) { digitalWrite(dirPinX, LOW); } if (x >0) { digitalWrite(dirPinX, HIGH); } x = abs(x); if (y<0) { digitalWrite(dirPinY, LOW); digitalWrite(dirPinY2, LOW); } if (y>0) { digitalWrite(dirPinY, HIGH); digitalWrite(dirPinY2, HIGH); } x = abs(x); y = abs(y)*2; Serial.println(x); for (int i=0; i<x; i++) { digitalWrite(stepPinX, LOW); digitalWrite(stepPinX, HIGH); delayMicroseconds(200); } for (int j=0; j<y; j++) { digitalWrite(stepPinY, LOW); digitalWrite(stepPinY, HIGH); digitalWrite(stepPinY2, LOW); digitalWrite(stepPinY2, HIGH); delayMicroseconds(200); } } while(true); } The I thought that if I could run the "X axis" FOR and the "Y axis" FOR at the same time I will solve the problem. Is there anyway to do it? Thanks 
|
|
|
|
|
6
|
Using Arduino / Programming Questions / Re: Help in Split String into Array
|
on: June 28, 2012, 10:17:11 am
|
Ok, now I understood your code and I've modified a bit but I'm still getting wrong values in the serial monitor. Here is the code void loop() { if (Serial.available() >=7) { char c_Temp[3] = {0,0,0}; c_Temp[0] = Serial.read(); c_Temp[1] = Serial.read(); c_Temp[2] = Serial.read(); if (c_Temp[2] = ',') { myArray[0] = c_Temp; } c_Temp[0] = Serial.read(); if(c_Temp[0] !=0) { c_Temp[1] = Serial.read(); if (c_Temp[1] !=0) { c_Temp[2] = Serial.read(); } myArray[1] = c_Temp; } else { myArray[1] = 0; } Serial.println(myArray[0]); delay (50); } Serial.println prints: R,...... B,,.... RB,.... R,....
,.... 0 ,.... 10,.... ,1,.... B,,.... RB,....
|
|
|
|
|
7
|
Using Arduino / Programming Questions / Re: Help in Split String into Array
|
on: June 28, 2012, 09:18:48 am
|
if you are expecting a certain sequence it is better to wait for enough data rather than do stuff when anything is available. The problem is that I don't know how long is going to be the value 'cause it may vary from 0 to 255. And I am still having the same trouble parsing the string into array. Sorry these gets quite complicated for me.
|
|
|
|
|
8
|
Using Arduino / Programming Questions / Re: Help in Split String into Array
|
on: June 28, 2012, 08:56:15 am
|
Thank you for reply, I tried to use your improvements, and I am getting bad values: ,1 RB This is the code: if (Serial.available() > 0) { /*int h=Serial.available(); // if you are getting escape -characters try h--; here for (int i=0;i<h;i++){ //inData += (char)Serial.read(); }*/ char c_Temp[3] = { 0, 0, 0 }; c_Temp[0] = Serial.read(); c_Temp[1] = Serial.read(); Serial.read(); myArray[0] = c_Temp; c_Temp[0] = Serial.read(); c_Temp[1] = Serial.read(); myArray[1] = c_Temp; } Serial.println(myArray[0]); Serial.println(myArray[1]); Using if(Serial.available()>0) I am losing 0 values, for example if I send value 10, I just get back 1. I don't know if you understand me.
|
|
|
|
|
9
|
Using Arduino / Programming Questions / Help in Split String into Array
|
on: June 28, 2012, 06:47:07 am
|
Hey guys, I thought that splitting strings was easier than in fact it is. Here is my code: String inData; String myArray[2];
void loop() { inData = ""; if (Serial.available() > -1) { int h=Serial.available(); for (int i=0;i<h;i++){ inData += (char)Serial.read(); } }
From serial, I am getting and string like: RB,35 the inData var stores the string and than it has to be split into an array like: myArray[0] = RB ; myArray[1] = 35; So the first data is a word and the second data is a value from 0 to 255. I had a look at this topic http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1247911854 but I don't know how to implement it in my sketch because they are using char instead of string. thank you.
|
|
|
|
|
10
|
Using Arduino / Programming Questions / Re: Help Rotary encoder
|
on: May 23, 2012, 01:39:55 pm
|
|
Yes, in fact the code is working good, the encoder is counting from 0 to 255, if I turn the knob clockwise and it reverses if I go counterclockwise. First problem is that "almost" ,and I say almost 'cause is not always, every time it counts +4 steps or -4 depending the direction. Using this code I could know if the weather vane is turning clockwise or counter clockwise, but I can't get the actually position of the knob to determinate north, south, east, west.
|
|
|
|
|
11
|
Using Arduino / Programming Questions / Help Rotary encoder
|
on: May 23, 2012, 12:52:02 pm
|
Hey guys, I bought a rotary encoder from sparkfun ( http://www.sparkfun.com/products/9117). I have never worked with rotary, first I thought that it was like a potentiometer, but it doesn't. So my question is: Is it possible to know the direction of the encoder? I want to built a weather vane using this encoder as a way to know the direction of the wind. (N,S,E,W). I've this piece of code from http://www.circuitsathome.com/mcu/programming/reading-rotary-encoder-on-arduino, but I don't understand almost anything, even reading the website which is explained. Maybe it is not the best way to start. Thank you. /* Rotary encoder read example */ #define ENC_A 14 #define ENC_B 15 #define ENC_PORT PINC void setup() { /* Setup encoder pins as inputs */ pinMode(ENC_A, INPUT); digitalWrite(ENC_A, HIGH); pinMode(ENC_B, INPUT); digitalWrite(ENC_B, HIGH); Serial.begin (115200); Serial.println("Start"); } void loop() { static uint8_t counter = 0; //this variable will be changed by encoder input int8_t tmpdata; /**/ tmpdata = read_encoder(); if( tmpdata ) { Serial.print("Counter value: "); Serial.println(counter, DEC); counter += tmpdata; } } /* returns change in encoder state (-1,0,1) */ int8_t read_encoder() { static int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0}; static uint8_t old_AB = 0; /**/ old_AB <<= 2; //remember previous state old_AB |= ( ENC_PORT & 0x03 ); //add current state return ( enc_states[( old_AB & 0x0f )]); }
|
|
|
|
|
13
|
Using Arduino / General Electronics / proximity sensor + electret microphone = no compatible??
|
on: May 14, 2012, 01:14:55 pm
|
Hey guys, I am having a trouble when I connect and infrared proximity sensor ( http://www.sparkfun.com/products/242) and an preamplified electret microphone ( http://www.sparkfun.com/products/9964). When I connect the sensors separately, they work perfect, but when I connect both together the electret microphone reads signal of 1023 (5V) at all time. Do I have to get the voltage from different power sources? That would be very weird. Thanks, Here I leave you the electret mic sketch to read the signal if its necessary. const int micPin = A2; int valueMic = 0; int valueMicMax = 512; int valueMicMin = 512;
void setup() { Serial.begin(9600); }
void loop() { for (int i =0; i < 100; i++){ valueMic = analogRead(micPin); if (valueMic > valueMicMax){ valueMicMax = valueMic; } else { if (valueMic < valueMicMin){ valueMicMin = valueMic; } } } valueMic = valueMicMax-valueMicMin; Serial.println(valueMic); valueMicMax = 512; valueMicMin = 512; }
|
|
|
|
|