Coding Help


  • Global Moderator

    A user needed help with the following as it was returning errors:

    #include <SmartInventor.h>
    int a = 0;
    int b = 1 / a;
    void setup() {
      // put your setup code here, to run once:
      Serial.begin(57600);
      SmartInventor.DCMotorUse();
      int left = anologRead(19);
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
    
      while (a < 10) {
        a = a + 1;
        b = a * a + a;
        Serial.println(a);
        Serial.println(b); Serial.println("test");
        delay(1000);
        if (Serial.available() > 0) {
          Serial.println((char)Serial.read());
        }
      }; 
      
      for (int c = 0; c < 10; c++) {
        Serial.println(c);
        delay(1000);
        SmartInventor.DCMotor(M1, CCW, 100);
      }
    
      while (left < 500) {
        Serial.println(left);
        SmartInventor.DCMotor(M1, CCW, 100);
      }
      SmartInventor.DCMotor(M1, CW, 1000);
      Serial.println("end");
      SmartInventor.DCMotor(M1, STOP, 10);
      delay(5000);
    }
    

  • Global Moderator

    The solution to this error is as follows:

    The problem regarding this error, is actually that the code declares a variable "int left". This cannot be used as the library <SmartInventor.h> uses this variable already. As a result the user has to rename the variable so that the code can function. something like left_ir_sensor or l_sensor will suffice/not interfere with the library that the user has added.


  • Global Moderator

    A user had an issue with the following code:

    #include <SmartInventor.h>
    int a = 0;
    int b = 1 / a;
    void setup()
    {
      // put your setup code here, to run once:
      Serial.begin(57600);
      SmartInventor.DCMotorUse();
      int LEDPIN11 = 11;
      int LEDPIN12 = 12;
      int LEDPIN13 = 13;
      int LEDPIN14 = 14;
      int LEDPIN15 = 15;
      int LEDPIN16 = 16;
      int LEDPIN17 = 17;
      int LEDPIN18 = 18;
      pinMode(LEDPIN11, OUTPUT);
      pinMode(LEDPIN12, OUTPUT);
      pinMode(LEDPIN13, OUTPUT);
      pinMode(LEDPIN14, OUTPUT);
      pinMode(LEDPIN15, OUTPUT);
      pinMode(LEDPIN16, OUTPUT);
      pinMode(LEDPIN17, OUTPUT);
      pinMode(LEDPIN18, OUTPUT);
    }
    
    void loop()
    {
      // put your main code here, to run repeatedly:
      int leftsensor = 600;
      while (a < 10) {
        a = a + 1;
        b = a * a + a;
        Serial.println(a);
        Serial.println(b); Serial.println("test");
        delay(1000);
        if (Serial.available() > 0) {
          Serial.println((char)Serial.read());
        }
      }; for (int c = 0; c < 10; c++) {
        Serial.println(c);
        delay(1000);
        SmartInventor.DCMotor(M1, CCW, 100);
      }
    
      while (leftsensor > 500) {
        leftsensor = analogRead(19);
        Serial.println("left");
        SmartInventor.DCMotor(M1, CCW, 100);
        delay(1000);
      }
    
      //declares global variables used in the code
      int LEDPIN11 = 11;
      int LEDPIN12 = 12;
      int LEDPIN13 = 13;
      int LEDPIN14 = 14;
      int LEDPIN15 = 15;
      int LEDPIN16 = 16;
      int LEDPIN17 = 17;
      int LEDPIN18 = 18;
    
      void setup() {
    
        pinMode(LEDPIN11, OUTPUT);
        pinMode(LEDPIN12, OUTPUT);
        pinMode(LEDPIN13, OUTPUT);
        pinMode(LEDPIN14, OUTPUT);
        pinMode(LEDPIN15, OUTPUT);
        pinMode(LEDPIN16, OUTPUT);
        pinMode(LEDPIN17, OUTPUT);
        pinMode(LEDPIN18, OUTPUT);
      }
    
      void loop() {
    
        //sets the LED state to HIGH
        digitalWrite(LEDPIN11, HIGH);
        digitalWrite(LEDPIN12, HIGH);
        digitalWrite(LEDPIN13, HIGH);
        digitalWrite(LEDPIN14, HIGH);
        digitalWrite(LEDPIN15, HIGH);
        digitalWrite(LEDPIN16, HIGH);
        digitalWrite(LEDPIN17, HIGH);
        digitalWrite(LEDPIN18, HIGH);
        delay(1000);
    
        //sets the LED state to LOW
        digitalWrite(LEDPIN11, LOW);
        digitalWrite(LEDPIN12, LOW);
        digitalWrite(LEDPIN13, LOW);
        digitalWrite(LEDPIN14, LOW);
        digitalWrite(LEDPIN15, LOW);
        digitalWrite(LEDPIN16, LOW);
        digitalWrite(LEDPIN17, LOW);
        digitalWrite(LEDPIN18, LOW);
        delay(1000);
      }
    
      delay(1000);
    
      SmartInventor.DCMotor(M1, CW, 1000);
      Serial.println("end");
      SmartInventor.Buzz(500, 8);
      SmartInventor.DCMotor(M1, STOP, 10);
      delay(5000);
    }
    

    Please note that you are only able to have "1" void setup, and 1 void loop method.


  • Global Moderator

    The following is the code corrected

    #include <SmartInventor.h>
    int a = 0;
    int b = 1 / a;
      int LEDPIN11 = 11;
      int LEDPIN12 = 12;
      int LEDPIN13 = 13;
      int LEDPIN14 = 14;
      int LEDPIN15 = 15;
      int LEDPIN16 = 16;
      int LEDPIN17 = 17;
      int LEDPIN18 = 18;
      
    void setup()
    {
      // put your setup code here, to run once:
      Serial.begin(57600);
      SmartInventor.DCMotorUse();
    
      pinMode(LEDPIN11, OUTPUT);
      pinMode(LEDPIN12, OUTPUT);
      pinMode(LEDPIN13, OUTPUT);
      pinMode(LEDPIN14, OUTPUT);
      pinMode(LEDPIN15, OUTPUT);
      pinMode(LEDPIN16, OUTPUT);
      pinMode(LEDPIN17, OUTPUT);
      pinMode(LEDPIN18, OUTPUT);
    }
    
    void loop()
    {
      // put your main code here, to run repeatedly:
      int leftsensor = 600;
      while (a < 10) {
        a = a + 1;
        b = a * a + a;
        Serial.println(a);
        Serial.println(b); Serial.println("test");
        delay(1000);
        if (Serial.available() > 0) {
          Serial.println((char)Serial.read());
        }
      };
      for (int c = 0; c < 10; c++) {
        Serial.println(c);
        delay(1000);
        SmartInventor.DCMotor(M1, CCW, 100);
      }
    
      while (leftsensor > 500) {
        leftsensor = analogRead(19);
        Serial.println("left");
        SmartInventor.DCMotor(M1, CCW, 100);
        delay(1000);
      }
    
      //sets the LED state to HIGH
      digitalWrite(LEDPIN11, HIGH);
      digitalWrite(LEDPIN12, HIGH);
      digitalWrite(LEDPIN13, HIGH);
      digitalWrite(LEDPIN14, HIGH);
      digitalWrite(LEDPIN15, HIGH);
      digitalWrite(LEDPIN16, HIGH);
      digitalWrite(LEDPIN17, HIGH);
      digitalWrite(LEDPIN18, HIGH);
      delay(1000);
    
      //sets the LED state to LOW
      digitalWrite(LEDPIN11, LOW);
      digitalWrite(LEDPIN12, LOW);
      digitalWrite(LEDPIN13, LOW);
      digitalWrite(LEDPIN14, LOW);
      digitalWrite(LEDPIN15, LOW);
      digitalWrite(LEDPIN16, LOW);
      digitalWrite(LEDPIN17, LOW);
      digitalWrite(LEDPIN18, LOW);
      delay(1000);
    
      delay(1000);
    
      SmartInventor.DCMotor(M1, CW, 1000);
      Serial.println("end");
      SmartInventor.Buzz(500, 8);
      SmartInventor.DCMotor(M1, STOP, 10);
      delay(5000);
    }
    
    

  • Global Moderator


Log in to reply
 

Looks like your connection to Robolink community was lost, please wait while we try to reconnect.