Need Help

Hey to everybody,

currently I have to program a Zumo 32U4.

This is a school-project and we have to make a "battlebot".

The problem is I am really bad at programming... I really tried it and at first I was excited but quickly I realized that this will not work fine.

Please could someone be so kind to post a sample code, which I can use to edit and work on?

You would totally save my ass!

Have a great day

Greets
Tommy

Lmao, good luck.

Please could someone be so kind to post a sample code, which I can use to edit and work on?

void setup() 
{
  //code here runs once
}

void loop() 
{
  //code here runs repeatedly
}

I really tried it

So what did you try ?
What is the specification for the bot ?

After you install the Zumo library you'll find a lot of helpful example code at File > Examples > Zumo32U4. I don't think anyone here is going to do much better than that. The way to approach programming is to start very simple and gradually work your way to more complex programs. Don't move on to the next program until you fully understand the current one. If you try to jump right into a complex battlebot program as a beginner then of course you're going to feel like you're bad at programming. But it's not that you're bad at it, it's just that you skipped over some of the fundamentals.

#include <Wire.h>
#include <Zumo32U4.h>
#include "TurnSensor.h"

Zumo32U4Motors motors;
Zumo32U4ProximitySensors proxSensors;
Zumo32U4LineSensors lineSensors;
Zumo32U4ButtonA buttonA;
Zumo32U4LCD lcd;
L3G gyro;

  int speedleft=250;
  int speedright=speedleft*1;

void setup()
{
  lineSensors.initFiveSensors();
  lineSensors.calibrate();
  proxSensors.initFrontSensor();

  turnSensorSetup();
  delay(500);
  turnSensorReset();
  
  lcd.clear();
 // lcd.print(F("Press A"));
 // buttonA.waitForButton();
  delay(1000);

  motors.setLeftSpeed(speedleft);
  motors.setRightSpeed(speedright);
}


void loop()
{
    int counter = 2;
    while (1==1)
    {
    
    ledRed(0);
    #define NUM_SENSORS 5
    uint16_t lineSensorValues[NUM_SENSORS];
    lineSensors.read(lineSensorValues);
    proxSensors.read();
    int leftValue = proxSensors.countsFrontWithLeftLeds();
    int rightValue = proxSensors.countsFrontWithRightLeds();
   
    lcd.clear();
    lcd.gotoXY(0, 0);
    lcd.print(lineSensorValues[2]);
    lcd.gotoXY(0, 1);
    lcd.print(((((int32_t)turnAngle >> 16) * 360) >> 16));

 
    turnSensorUpdate();    
 
       
    if ((lineSensorValues[2] < 300 ||  lineSensorValues[0] < 300 || lineSensorValues[4] < 300) && counter % 2 == 0)
    {
      motors.setSpeeds(-100,-100);
      delay(700);  

      while (((((int32_t)turnAngle >> 16) * 360) >> 16) != -180)
      {    
      motors.setLeftSpeed(200);
      motors.setRightSpeed(0);
      turnSensorUpdate(); 
      
      lcd.gotoXY(0, 1);
      lcd.print(((((int32_t)turnAngle >> 16) * 360) >> 16));
      }
      counter = counter+1;
      lineSensors.read(lineSensorValues);
    }
    proxSensors.read();
    leftValue = proxSensors.countsFrontWithLeftLeds();
    rightValue = proxSensors.countsFrontWithRightLeds();
    
    if ((lineSensorValues[2] < 300 ||  lineSensorValues[0] < 300 || lineSensorValues[4] < 300) && counter % 2 == 1)
    {
      motors.setSpeeds(-100,-100);
      delay(700);  
      
      motors.setLeftSpeed(0);
      motors.setRightSpeed(200);
      delay(20);

      while (((((int32_t)turnAngle >> 16) * 360) >> 16) != 0){    
      motors.setLeftSpeed(0);
      motors.setRightSpeed(200);
      turnSensorUpdate(); 
      lcd.gotoXY(0, 1);
      lcd.print(((((int32_t)turnAngle >> 16) * 360) >> 16));
      }
      counter = counter+1;
      lineSensors.read(lineSensorValues);
    }
    proxSensors.read();
    leftValue = proxSensors.countsFrontWithLeftLeds();
    rightValue = proxSensors.countsFrontWithRightLeds();


    if (((leftValue >= 5.9) || (rightValue >=5.9)) && (((((int32_t)turnAngle >> 16) * 360) > -100) || ((((int32_t)turnAngle >> 16) * 360) < 100 )))
    // if-Abfrage bei Gegenstand mit 0 Grad Ausrichtung
    {
        while (((((int32_t)turnAngle >> 16) * 360) >> 16) != 90)
        {    
          motors.setLeftSpeed(0);
          motors.setRightSpeed(200);
          turnSensorUpdate(); 
          lcd.gotoXY(0, 1);
          lcd.print(((((int32_t)turnAngle >> 16) * 360) >> 16));
        }
        
  
        motors.setSpeeds(speedleft,speedright);
        delay(300);
        motors.setSpeeds(0,0);
  
        while (((((int32_t)turnAngle >> 16) * 360) >> 16) != 0)
        {    
          motors.setLeftSpeed(200);
          motors.setRightSpeed(0);
          turnSensorUpdate(); 
          lcd.gotoXY(0, 1);
          lcd.print(((((int32_t)turnAngle >> 16) * 360) >> 16));
        }  
  
        motors.setSpeeds(speedleft,speedright);
        delay(1000);
        motors.setSpeeds(0,0);
  
        while (((((int32_t)turnAngle >> 16) * 360) >> 16) != -90)
        {    
          motors.setLeftSpeed(200);
          motors.setRightSpeed(0);
          turnSensorUpdate(); 
          lcd.gotoXY(0, 1);
          lcd.print(((((int32_t)turnAngle >> 16) * 360) >> 16));
        }  
        
        motors.setSpeeds(speedleft,speedright);
        delay(300);
        motors.setSpeeds(0,0);
  
        while (((((int32_t)turnAngle >> 16) * 360) >> 16) != 0)
        {    
          motors.setLeftSpeed(00);
          motors.setRightSpeed(200);
          turnSensorUpdate(); 
          lcd.gotoXY(0, 1);
          lcd.print(((((int32_t)turnAngle >> 16) * 360) >> 16));
        }               
      }

    proxSensors.read();
    leftValue = proxSensors.countsFrontWithLeftLeds();
    rightValue = proxSensors.countsFrontWithRightLeds();
      
      if (((leftValue >= 5.9) || (rightValue >=5.9)) && (((((int32_t)turnAngle >> 16) * 360) < -100) || ((((int32_t)turnAngle >> 16) * 360) > 100 )))
      {
      // if-Abfrage bei Gegenstand mit 179 Grad Ausrichtung
      ledGreen(1);
      while (((((int32_t)turnAngle >> 16) * 360) >> 16) != -90){    
        motors.setLeftSpeed(0);
        motors.setRightSpeed(200);
        turnSensorUpdate(); 
        lcd.gotoXY(0, 1);
       // lcd.print(((((int32_t)turnAngle >> 16) * 360) >> 16));
       lcd.print('HILFE');
      }
      
      motors.setSpeeds(speedleft,speedright);
      delay(300);
      motors.setSpeeds(0,0);

      while (((((int32_t)turnAngle >> 16) * 360) >> 16) != -180)
      {    
        motors.setLeftSpeed(200);
        motors.setRightSpeed(0);
        turnSensorUpdate(); 
        lcd.gotoXY(0, 1);
        lcd.print(((((int32_t)turnAngle >> 16) * 360) >> 16));
      }  

      motors.setSpeeds(speedleft,speedright);
      delay(1000);
      motors.setSpeeds(0,0);

      motors.setLeftSpeed(200);
      motors.setRightSpeed(0);
      delay(50);
      turnSensorUpdate(); 

      while (((((int32_t)turnAngle >> 16) * 360) >> 16) != 90)
      {    
        motors.setLeftSpeed(200);
        motors.setRightSpeed(0);
        turnSensorUpdate(); 
        lcd.gotoXY(0, 1);
        lcd.print(((((int32_t)turnAngle >> 16) * 360) >> 16));
      }  
      
      motors.setSpeeds(speedleft,speedright);
      delay(300);
      motors.setSpeeds(0,0);

      while (((((int32_t)turnAngle >> 16) * 360) >> 16) != 179)
      {    
        motors.setLeftSpeed(0);
        motors.setRightSpeed(200);
        turnSensorUpdate(); 
        lcd.gotoXY(0, 1);
        lcd.print(((((int32_t)turnAngle >> 16) * 360) >> 16));
      } 
      } 
      ledGreen(0);
  
    motors.setLeftSpeed(speedleft);
    motors.setRightSpeed(speedright);
    }
}

UKHeliBob:
So what did you try ?
What is the specification for the bot ?

As you can see I have a "decent" code but.... The Problem ist that every-time the robot tries to curve a obstacle it looses control over the line sensor and it leaves the battleground.

Is there any prioritization?

I don't know the specifications - it is a normal Zumo 32U4

Thank You!

You need to get rid of all the calls to the delay function.
You wouldn't walk down a busy street by taking a look around, starting to walk and closing your eyes for most of the time.

pert:
After you install the Zumo library you'll find a lot of helpful example code at File > Examples > Zumo32U4. I don't think anyone here is going to do much better than that. The way to approach programming is to start very simple and gradually work your way to more complex programs. Don't move on to the next program until you fully understand the current one. If you try to jump right into a complex battlebot program as a beginner then of course you're going to feel like you're bad at programming. But it's not that you're bad at it, it's just that you skipped over some of the fundamentals.

The problem is I have a deadline and not enough time to fully engage with the topic :confused:
Thanks

Victrizz:
The problem is I have a deadline and not enough time to fully engage with the topic :confused:
Thanks

Is there any prioritization?

Oh, the irony.

Victrizz:
The problem is I have a deadline and not enough time to fully engage with the topic :confused:

Sometimes the best way to go fast is to go slow.

You should regularly use the Arduino IDE's Tools > Auto Format feature on your code. With the screwy indentation you have now your code is very difficult to understand. If you're using the Arduino Web Editor you will not have access to this very useful feature. I recommend you to use the real IDE instead, which you can download for free from here:
http://www.arduino.cc/en/Main/Software

Is there any prioritization?

There is no prioritisation as such. The code executes line by line but if it is in a loop during which the line sensor is nor being read then obviously it cannot react to it.

You need to break the actions into small sections so that they each occur repeatedly and rapidly giving the appearance of multitasking. Have a look at Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

void loop()
{
    int counter = 2;
    while (1==1)

Perhaps there's some misunderstanding about the operation of the function "loop()"

TolpuddleSartre:

void loop()

{
    int counter = 2;
    while (1==1)



Perhaps there's some misunderstanding about the operation of the function "loop()"

There is a general misunderstanding problem I guess :smiley:

I know normally one needs to be patient and spent a lot of time in order to get better but as I already told I don't have that time...

For the schoolprojekt only the result counts not really how good I have understand it... =(

For the schoolprojekt only the result counts not really how good I have understand it

With that attitude you won't get very far. If I had given you the project to do I would expect you to be able to explain how and why the bot and the code worked when you presented the result.

Victrizz:
I know normally one needs to be patient and spent a lot of time in order to get better but as I already told I don't have that time...

Then give up this project and use what time you have for something more productive. That would be proper prioritization.

For the schoolprojekt only the result counts not really how good I have understand it... =(

I am certainly not going to help you get marks for something you don't understand and therefore don't deserve.

Would you like to go to a dentist who got marks in his exams for something he did not understand?

...R

Don't schools have teachers anymore?