A simple question

sp. "My complex question is.."

@TheMemberFormerlyKnownAsAWOL It does seem like a simple question if you don't know the answer, but you're right. There are two distinct camps on this issue.

Your post was MOVED to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

Hi coolname,
it's easy. You have to connect your UNO to your computer with a USB cable, on which computer you have installed the program called IDE. This program allows you to 1) communicate with your UNO through the USB port (send and receive instructions or data),
and 2) to write yourself (on the computer) the instructions that your UNO will have to execute later.
Start by copying a simple program, for example that makes the LED on your UNO blink.
Then you will make it more and more complicated !

1 Like

I have a "stock" answer for this:

The misunderstanding regarding loop() is essentially that it is not beholden to any one process or decision.

It implements a series of tasks such that in effect all are performed simultaneously - this can be seen as "multi-tasking" or "timesharing". Each task is encapsulated as a "state machine" and essentially "called" one after another in a chain which repeats again and again.

So what do each of these tasks look like? Well, each has a "state" which selects from a number of simple actions such as reading an input or the time (that is, millis()) to determine whether any further action is required. Most of the time, nothing further needs to be done in which case the task simply drops through to the next task in the list.

If there is a need for a further action based on the input, then whatever part of that action can be performed immediately, is executed and if a further action will be required depending on the consequence of this immediate one or on another criterion, the "state" is adjusted so that on successive passes through this task, the new situation will be tested and again, acted on accordingly.

So each of the tasks in turn either does nothing, or does something that takes little or no time to do and sets the state for what needs to happen on the next pass through its own part of the loop.

Does that help?

You are a single thread.

Can you name more than 100 separate tasks you have done today?

I think i did 100 separate tasks just in making lunch.

Tomarrow i will wake up and start all over agaim

The journey of 10,000 miles begins with a single step.

So 'do this' as you said. Get that to work.

Then 'do this other thing'

There are lots of examples in the IDE for both of those things

My Multi-tasking in Arduino tutorial goes in these as well
multitaskingDiagramSmall

Ok thaks for every answer, maybe I misdescribed my problem... My actually problem looks like this: I have an Sphero RVR and i want him to drive and measure the distance between him and an object. But i don't know how to write in the code: messure the distance while you are driving. So that the Sphereo RVR drives slowly and measure the distance, and when he sees something he should react. I know everything else in this code but not how to write: "do this while you do that".

Hello
Well, I think that these tasks can be processed via a FSM.

So, now you do know - the generic answer to your generic question has been explained in several different ways in the posts above.

The next thing to do is show us your code and describe specifically where you are struggling (if you still are). If you make an effort but still run into problems, you'll get loads of help.

How does one contact a field staff member?

My current code looks like this:

#include <Servo.h>
#include <SpheroRVR.h>

const int trigPin=12;
const int echoPin=11;

long duration;
int distance;
int drehung = 0;

Servo s1;

void Turn() {
  if (distance <= 30) {
    drehung = drehung + 60;
  }
}

void Drive() {
      if (distance <= 30) {
    rvr.driveWithHeading(0, 60, static_cast<uint8_t>(DriveFlags::none));
    delay(300);
    }

    else 
     {
      rvr.driveWithHeading(30, drehung, static_cast<uint8_t>(DriveFlags::none));
      delay(300);
     }
    }

int calDist()
{
   digitalWrite(trigPin, LOW);
   delayMicroseconds(2);
                                                           
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW); 
                                                            
  duration = pulseIn(echoPin, HIGH);
                                                           
  distance= duration*0.034/2;                             

  return distance;
}


void setup() {
  
  Serial.begin(9600);
  pinMode(trigPin,OUTPUT);
  pinMode(echoPin,INPUT);

  s1.attach(9);

  rvr.configUART(&Serial);
  rvr.resetYaw ();
}

void loop() 
{
    for(int i=15;i<=165;i++){            
    s1.write(i);
    delay(30);
    distance = calDist();

    Serial.print(i);          
    Serial.print(",");                 
    Serial.print(distance);             
    Serial.print(".");                 
      }

    for(int i=165;i>15;i--){  
    s1.write(i);
    delay(30);
    distance = calDist();
    Serial.print(i);
    Serial.print(",");
    Serial.print(distance);
    Serial.print(".");
      }
    Turn();
    Drive(); 
 }

And my problem is that the Sphero RVR drives then stop and then the Arduino meassures the distance and say the Sphero RVR what he must do. But i want it to drive and meassure at the same time. And I don't know how.:sweat_smile:
Here is a link to the Sphero RVR Sphero RVR

You seem to have your code posted in two pieces, only one of which is appropriately marked up as code. :woozy_face:

Hi,
To add code please click this link;

A circuit diagram and link to "Sphero RVR" would also help.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

You may find this helpful

Well, first at all thanks for every answer but if I do it with an fsm I have the same problem, the robot drives, stops, measures the distance and the react. But like I said i want him to drive and measures at the same time. my current code looks like this:

#include <Servo.h>
#include <SpheroRVR.h>

const int trigPin=12;
const int echoPin=11;

long duration;
int distance;
int drehung = 0;

Servo s1;

int calDist()
{
   digitalWrite(trigPin, LOW);
   delayMicroseconds(2);
                                                           
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW); 
                                                            
  duration = pulseIn(echoPin, HIGH);
                                                           
  distance= duration*0.034/2;                             

  return distance;
}

enum vtlStates {Measurement, Turn, Drive};
vtlStates tlState;

int timings[] = {300, 250, 250};
long int timeStart, timeElapsed;

void setup() {
  
  Serial.begin(9600);
  pinMode(trigPin,OUTPUT);
  pinMode(echoPin,INPUT);

  s1.attach(9);

  rvr.configUART(&Serial);
  rvr.resetYaw ();

  tlState = Measurement;
  timeStart = millis();
             } 


 void loop() 
 {
  switch (tlState) {
    case Measurement : {
          for(int i=15;i<=165;i++){            
    s1.write(i);
    delay(30);
    distance = calDist();

    Serial.print(i);          
    Serial.print(",");                 
    Serial.print(distance);             
    Serial.print(".");                 
      }

    for(int i=165;i>15;i--){  
    s1.write(i);
    delay(30);
    distance = calDist();
    Serial.print(i);
    Serial.print(",");
    Serial.print(distance);
    Serial.print(".");
      } 
    timeElapsed = millis() - timeStart;
    if (timeElapsed > timings[Measurement]) {
    timeStart = millis();
    tlState = Turn;
    }
   }
   break;

   case Turn: {
      if (distance <= 30) {
      drehung = drehung + 60;
      }
    timeElapsed = millis() - timeStart;
    if (timeElapsed > timings[Turn]) {
    timeStart = millis();
    tlState = Drive;
    }
   }
   break;

   case Drive: {
    if (distance <= 30) {
    rvr.driveWithHeading(0, 60, static_cast<uint8_t>(DriveFlags::none));
    delay(300);
    }

    else 
     {
      rvr.driveWithHeading(30, drehung, static_cast<uint8_t>(DriveFlags::none));
      delay(300);
     }
    timeElapsed = millis() - timeStart;
    if (timeElapsed > timings[Drive]) {
    timeStart = millis();
    tlState = Measurement;
    }
   }
  }
 }
 

In which case, you've implemented it incorrectly.

Uhm jea I try to make the robot drive and meausures the distance at the same time, but at the moment the robot does one thing after an other.

because you have coded it to do so.

See - your "measurement" state takes (165-15) * 2 * 30msec = 600 * 30 msec = 18 seconds. The delay(30) means the Arduino CANNOT do anyhting ele in that time. Its locked into the measurement state.