Jump to content
ELFORUM - Forumul Electronistilor
Sign in to follow this  
Vlad Mihai

Problema bucla for Arduino

Recommended Posts

Salut, lucrez  la un proiect ce consta in controlul unui motor pas cu pas unipolar folosind LabVIEW. Problema e ca  atunci cand ii dau comanda sa faca un anumit numar de pasi si dupa sa se opreasca  nu vrea sa faca asta.... in rest  celelalte functii merg. Atasez  codul,  poate cineva imi explica unde e buba:

int motorPin1 = 5;int motorPin2 = 6;int motorPin3 = 8;int motorPin4 = 7;int delayTime=5;int Rx = 0;void setup() {  pinMode(motorPin1, OUTPUT);  pinMode(motorPin2, OUTPUT);  pinMode(motorPin3, OUTPUT);  pinMode(motorPin4, OUTPUT);  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps}void loop() {     Rx = Serial.read();     if(Rx=='W'){  forward();  }    if(Rx=='A'){  backward();  }   if(Rx=='D'){        ///aici nu vrea sa mearga cum vreau eu, in sensul ca atunci cand primeste D pe serial                          sa  execute de 8 ori functia forward si dupa sa se opreasca for(i=0;i<8;i++){        forward(); }   }    }void  forward(){  digitalWrite(motorPin1, HIGH);  digitalWrite(motorPin2, LOW);  digitalWrite(motorPin3, LOW);  digitalWrite(motorPin4, LOW);  delay(delayTime);  digitalWrite(motorPin1, LOW);  digitalWrite(motorPin2, HIGH);  digitalWrite(motorPin3, LOW);  digitalWrite(motorPin4, LOW);  delay(delayTime);  digitalWrite(motorPin1, LOW);  digitalWrite(motorPin2, LOW);  digitalWrite(motorPin3, HIGH);  digitalWrite(motorPin4, LOW);  delay(delayTime);  digitalWrite(motorPin1, LOW);  digitalWrite(motorPin2, LOW);  digitalWrite(motorPin3, LOW);  digitalWrite(motorPin4, HIGH);  delay(delayTime);}void backward(){ digitalWrite(motorPin1, LOW);  digitalWrite(motorPin2, LOW);  digitalWrite(motorPin3, LOW);  digitalWrite(motorPin4, HIGH);  delay(delayTime);  digitalWrite(motorPin1, LOW);  digitalWrite(motorPin2, LOW);  digitalWrite(motorPin3, HIGH);  digitalWrite(motorPin4, LOW);  delay(delayTime);  digitalWrite(motorPin1, LOW);  digitalWrite(motorPin2, HIGH);  digitalWrite(motorPin3, LOW);  digitalWrite(motorPin4, LOW);  delay(delayTime);  digitalWrite(motorPin1, HIGH);  digitalWrite(motorPin2, LOW);  digitalWrite(motorPin3, LOW);  digitalWrite(motorPin4, LOW);  delay(delayTime);}void stop_motor(){  digitalWrite(motorPin1, LOW);  digitalWrite(motorPin2, LOW);  digitalWrite(motorPin3, LOW);  digitalWrite(motorPin4, LOW);}

Share this post


Link to post
Share on other sites

cand compilezi sketch-ul in arduino IDE iti da eroare? la mine spune ca variabila i nu e declarata.

dupa " void loop() {", pune int i=0;

sau 

for (int i=0;i<8;i++);

Edited by radu_bc

Share this post


Link to post
Share on other sites

da  era int i=0; am sters eu  probabil............... deci cu tot cu int i=0 nu imi da eroare la upload, dar   nu functioneaza cum trebuie.

Share this post


Link to post
Share on other sites

Dupa ce l-am declarat pe i, am definit portul 10 ca iesire, am conectat un led la el si am modificat if(Rx=='D') ca sa aprind led-ul cand merge motorul si sa-l sting cand nu merge.

Led-ul se stinge asa cum era de asteptat. Cum comunici cu arduino, LabView sau cu terminalul din ide-ul arduino? Esti sigur ca nu trimiti mai multi de D?

 

Codul modificat:

int motorPin1 = 5;int motorPin2 = 6;int motorPin3 = 8;int motorPin4 = 7;int ledPin = 10;int delayTime=5;int Rx = 0;int i = 0;void setup() {  pinMode(motorPin1, OUTPUT);  pinMode(motorPin2, OUTPUT);  pinMode(motorPin3, OUTPUT);  pinMode(motorPin4, OUTPUT);  pinMode(ledPin, OUTPUT);  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps}void loop() {   Rx = Serial.read();      if(Rx=='W'){      forward();   }      if(Rx=='A'){      backward();  }   if(Rx=='D'){             for(i=0;i<8;i++){             forward();        digitalWrite(ledPin, HIGH); //aprind un led     }  } else {     digitalWrite(ledPin, LOW); //sting ledul  }}void  forward(){...}void backward(){...}void stop_motor(){...}
Edited by Liviu M

Share this post


Link to post
Share on other sites

Am incercat eu acum cu 4 LED-uri si pare ok.  :84

Face tot ce trebuie sa faca...doar ca eu am schimbat porturile...am folosit 2,3,4,5...

Share this post


Link to post
Share on other sites

Am adaugat un "ecou" al datelor receptionate pe seriala, ca sa verifici ca nu e de acolo.

void loop() {   Rx = Serial.read();      if((Rx >= '0') && (Rx <= 'z')){       Serial.write(Rx);   }...
LE Testeaza si fara motor (cu niste LED-uri, ceva), sa nu ai probleme cu "zgomotul". Edited by Liviu M

Share this post


Link to post
Share on other sites

Salut, cred ca din LabVIEW problema, trimitea  vreo 5 de D, comunic direct  pe serial cu atmega din  LabView, arduino al meu e de fapt  o placuta cu un atmega smd si  cu  pinii de iesire. testez noul cod dupa ce rezolv aplicatia  problema cu aplicatia. multumesc.  testez indata si revin.

LE:Am testat acum merge, era din LabVIEW, trimitea  date incontinuu. si de asta    executa incontinuu . Multumesc din nou Liviu! si Radu! O seara placuta!

Edited by Vlad Mihai

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.