Hi Arduino forum members, starting with Arduino and programming,
tried to find answer for this easy task (I hope so).
I have two servo motors, IR remote and want every case to run for example 20 times and then loop to stop.
What I need to do with my code?
thanks
Dragos
Here is my code:
#include <Servo.h>
#include <IRremote.h>
Servo servoLeftRight; // servo 1
Servo servoUpDown; // servo 2
int pos = 0;
int IRpin = 11; // pin for the IR sensor
IRrecv irrecv(IRpin);
decode_results results;
void setup()
{
// initialize serial communication:
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
servoLeftRight.attach(10); // servo 1 - pin 10
servoUpDown.attach(9); // servo - 2 pin 9
}
void loop()
{
if (irrecv.decode(&results))
{
irrecv.resume(); // Receive the next value
}
switch(results.value)
{
case 1:
Serial.println("PR 16 - L10-S");
servoLeftRight.write(0);
delay(1500);
servoLeftRight.write(10);
delay(1500);
servoUpDown.write(0);
break;
case 2:
Serial.println("PR 17 - L10-D10");
servoLeftRight.write(0);
delay(1500);
servoLeftRight.write(20);
delay(1500);
servoUpDown.write(0);
break;
case 3:
Serial.println("PR 20 - Slow 0-20");
{
for(pos = 0; pos < 21; pos += 2) // servo 0 - 20
{
Serial.println("PR 20 - Slow 0-20");
servoLeftRight.write(pos); // servo go to 'pos'
delay(1500);
}
for(pos = 21; pos>=1; pos-=2) // servo go 20 - 0
{
Serial.println("PR 20 - Slow 20-0");
servoLeftRight.write(pos);
delay(1500);
}
servoUpDown.write(0);
delay(1500);
}
break;
default:
Serial.println("Middle position");
servoLeftRight.write(10);
delay(10000);
}
}
What I need to do with my code?
Nothing until you explain what this means:
and want every case to run for example 20 times
Each case should run when you press the appropriate switch on the remote control. If you want a particular case to run only n times, you need to count the number of times it runs, and then not execute the case code if the count has reached n.
The loop() function will always continue to run. You can make in not accomplish anything. But, why?
Here is why:
On remote control I want to choose case and run it 20 or 200 times, motors to stop, and then again to choose another case or repeat the same.
I am making ping pong robot... player need to rest or change drill (case).
On remote control I want to choose case and run it 20 or 200 times
You want to execute the case statement(s) multiple times for ONE switch press? Then, a for loop is needed.
I am making ping pong robot... player need to rest or change drill (case).
You can put
while(1)
{
}
at the end of the case, which will keep loop() busy, accomplishing nothing, until you reset the Arduino. But, I don't understand why you want to do that.
First, Paul thanks for helping me, sorry I really like Arduino thing, but my programming skills are very limited for now, and my English is also not so strong... so I have problem to understand you, can you use plain English... really do not get it why U ask me Why I need this...?
I know in near future there will be better and final solution, but for now I need something to start with ( some experiments, measurments, angles...)
Are u familiar with table tennis? Best sport ever.
Once again scenario is basic and simple
- Player on remote conrole choose drill (case)
2. After 200 loops of case, arduino stops (waiting for another input on remote control).
- Player rests.
- Player repeat the same drill or choose another one.
Look, I am not programmer, so when you suggest
while(1)
{
}
I have problem with that, can you be nice and put it in my code on proper place, please
Dragos
waiting for another input on remote control
Then, the Arduino can't be in a useless while loop. It must continue to execute loop() over and over, looking for input.
can you be nice and put it in my code on proper place, please
The proper place is not in your code. You do not want the Arduino to stop.
- After 200 loops of case
No. After 200 iterations of a loop in the case statement,...
case someValue:
for(byte i=0; i<200; i++)
{
// Do something - serve, volley, whatever
}
break;
Do you read what I wrote? Can you think outside of box?
Paul If I do not want arduino to stop let him do waiting for 1000000 miliseconds I do not care, just want motor no.5 to stop feeding mashine.
This robot will have 6 motors, over 100 cases and you giving me hard times here with some things which are not important for now and testing (waiting for parts to test it)
This code
for(byte i=0; i<200; i++)
I have found after 1 min searching Google few days back, but damn thing do not work when I do the coding.
So trivial problem I think.
Do you read what I wrote?
Of course I did.
Can you think outside of box?
Can YOU think inside the box? There are things that the Arduino can do, such as follow your instructions EXACTLY. There are things it can't do - like read your mind. I can't either, so I'm bowing out of this thread.
OK Paul, thanks anyway.
Look If I can think inside the box as U said I will not ask for help here. I am not programmer and my mind is not thinking as yours, so we can not understand.
But, everybody who wants to help me can go from the scratch and do the new coding.
In plain English I gave what I need.robot to do (and there is simple library I gave it is so clear for novice like me).
On other hand if this forum can not help me I can always pay programmer 5€/hour to do it.