Example #1
0
void secret_button()
{
    static unsigned long but1;
    but1 = millis();
    Serial.println(F("1"));
    while(!digitalRead(ON_PIN)){
        delay(20);
        if (millis() > but1 + 5000){
            go_sleep();
        }
    }
    but1 = millis();
    while(millis() < but1 + BUTTON1_DELAY){delay(20);
        if(!digitalRead(ON_PIN)){
            Serial.println(F("2"));
            while(!digitalRead(ON_PIN)){delay(20);}
            but1 = millis();
            while(millis() < but1 + BUTTON1_DELAY){delay(20);
                if(!digitalRead(ON_PIN)){
                    Serial.println(F("3"));
                    while(!digitalRead(ON_PIN)){delay(20);}
                    but1 = millis();
                    while(millis() < but1 + BUTTON1_DELAY){delay(20);
                        if(!digitalRead(ON_PIN)){
                            Serial.println(F("4"));
                            while(!digitalRead(ON_PIN)){delay(20);}
                            but1 = millis();
                            while(millis() < but1 + BUTTON1_DELAY){delay(20);
                                if(!digitalRead(ON_PIN)){
                                    Serial.println(F("5"));
                                    but1 = millis();
                                    while(!digitalRead(ON_PIN)){delay(20);
                                        Serial.println(F("6"));
                                        if(millis() > but1 + 800){
                                            Serial.println(F("Release now!"));

                                            lcd.clear();
                                            lcd.setCursor(0, 0);
                                            lcd.print(F("Release now!"));
                                            delay(500);

                                            lcd.clear();
                                            lcd.setCursor(0, 0);
                                            lcd.print(F("Press: open box"));
                                            lcd.setCursor(0,1);
                                            lcd.print(F("Hold: reset game"));

                                            delay(1000);

                                            while(digitalRead(ON_PIN)){}
                                            but1 = millis();
                                            while(!digitalRead(ON_PIN)){delay(20);}

                                            if(millis() - but1 > 1000){
                                                lcd.clear();
                                                lcd.print(F("RESET GAME!"));
                                                delay(3000);
                                                target = 1;
                                                EEPROM.write(EEPROM_TARGET_INDEX, 1);

                                                break;
                                            }
                                            else {
                                                open_box();
                                                delay(15000);
                                                close_box();
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Example #2
0
/**
0123456789012345
��ʼ

-
 Sample data...
  **DFRobot**
-
20:00  -19.9C
 99%RH 1005hPa
-
31th-C  A=-19.9C
H=-19.9C L=-19.9C

31th-RH A=99%
H=99% L=99%

31th-hPa A=1005
H=1005 L=1005

-
30th-C  A=-19.9C
H=-19.9C L=-19.9C

30th-RH A=99%
H=99% L=99%

30th-hPa A=1005
H=1005 L=1005


TEMP(C)

T or TEMP
P or
RH

**/
void WeatherStation::displayNext()
{
    uint8_t i;
    uint8_t dataType;
    uint8_t lcdCurrentRow;
    uint8_t date;
    int16_t data;

    uint8_t &days = displayMgt.days;
    uint8_t &measureIndex = displayMgt.measureIndex;
    bool &dateShowFlag = displayMgt.dateShowFlag;
    uint8_t &nowDataIndex = displayMgt.nowDataIndex;

    String &rowInfo = displayMgt.rowInfo;
    String unitWord;

    /**display current**/

    DBG_PRINTLN_VAR(days,DEC);
    DBG_PRINTLN_VAR(nowDataIndex,DEC);
    DBG_PRINTLN_VAR(measureIndex,DEC);

    lcd.clear();
    lcdCurrentRow = 0;

    /**display date**/
    if ( !dateShowFlag )
    {
        rowInfo = "";
        rowInfo += currentYear;
        rowInfo += "/";
        rowInfo += currentMonth;
        rowInfo += "/";
        rowInfo += currentDate;

        rowInfo += " ";
        rowInfo += currentHour;
        rowInfo += ":";
        rowInfo += currentMinute;

        lcd.setCursor( 0, lcdCurrentRow++ );
        lcd.print( rowInfo );

        rowInfo = " ";  //one space before data

        dateShowFlag = true;
    }


    /**display current data**/
    if ( dateShowFlag && (nowDataIndex < measureNum) )
    {
        for ( i = nowDataIndex; i < measureNum; i++ )
        {
            if ( false == a_measureData[i].valid )
            {
                break;
            }

            dataType = a_measureData[i].dataType;
            data = a_measureData[i].p_sensor->getValue( dataType );

            unitWord = a_measureData[i].p_sensor->formatValue( data, dataType );

            if ( (rowInfo.length() + unitWord.length()) > LCD_COL_NUM )
            {
                lcd.setCursor( 0, lcdCurrentRow++ );
                lcd.print( rowInfo );

                rowInfo = " ";

                if (lcdCurrentRow >= LCD_ROW_NUM)
                {
                    break;
                }
            }

            rowInfo += unitWord;
            rowInfo += " ";

        }


        /*display the last row*/
        if ( rowInfo.length() > 2 )  //valid str > 2
        {
            lcd.setCursor( 0, lcdCurrentRow );
            lcd.print( rowInfo );

            rowInfo = "";
        }

        nowDataIndex = i;
        return;
    }


    /**display previous statistic**/
    if ( nowDataIndex >= measureNum )
    {
        uint8_t number;
        Sensor *p_sensor;

        date = dataHouse.getDate(days);

        if ( date > 31 )
        {
            clrDisplayMgt();

            lcd.print( "  ** no data **" );
        }
        else
        {
            static int8_t strLeftIndex = 0;
            int8_t strRightIndex;

            if ( 0 == rowInfo.length())
            {
                const char * dataName;
                uint8_t hour;

                rowInfo = "";

                rowInfo += date;
                rowInfo +="th ";

                dataType = a_measureData[measureIndex].dataType;

                p_sensor = a_measureData[measureIndex].p_sensor;

                if ( NULL != p_sensor )
                {
                    dataName = p_sensor->getDataName(dataType);
                    rowInfo += dataName;
                    rowInfo += " ";

                    data = dataHouse.getDayAverage( dataType, days, &number );
                    rowInfo += number;
                    rowInfo += "data";
                    rowInfo += '\n';

                    if ( number > 0)
                    {
                        unitWord = p_sensor->formatValue( data, dataType );
                        rowInfo += " Avg:";
                        rowInfo += unitWord;
                        rowInfo += '\n';

                        data = dataHouse.getDayMin( dataType, days, &hour );
                        unitWord = p_sensor->formatValue( data, dataType );
                        rowInfo += " ";
                        rowInfo += unitWord;
                        rowInfo += " ";
                        rowInfo += hour;
                        rowInfo += ":00";
                        rowInfo += "\n";

                        data = dataHouse.getDayMax( dataType, days, &hour );
                        unitWord = p_sensor->formatValue( data, dataType );
                        rowInfo += " ";
                        rowInfo += unitWord;
                        rowInfo += " ";
                        rowInfo += hour;
                        rowInfo += ":00";
                        rowInfo += "\n";
                    }

                }

                strLeftIndex = 0;
            }

            do
            {
                strRightIndex = rowInfo.indexOf('\n', strLeftIndex);

                if ( strRightIndex < 0 )
                {
                    break;
                }

                lcd.setCursor( 0, lcdCurrentRow++ );
                lcd.print( rowInfo.substring( strLeftIndex, strRightIndex ) );
                displayMgt.statisticFlag = true;

                strLeftIndex = (strRightIndex + 1);

                if ( lcdCurrentRow >= LCD_ROW_NUM )
                {
                    break;
                }

            }while(1);


            if ( ((strRightIndex + 1) >= rowInfo.length()) || (strRightIndex < 0) )
            {
                rowInfo = "";

                measureIndex++;

                if ( measureIndex >= measureNum )
                {
                    days++;
                    measureIndex = 0;
                }

                if ( days >= DATA_SAVE_DAYS )
                {
                    clrDisplayMgt();
                }

            }


        }

    }

}
Example #3
0
void loop()
{
    //Serial.print(F("Free RAM1:"));    Serial.println(freeRam());

    // Check secret button
    but = !digitalRead(ON_PIN);
    if(but){
        Serial.println(F("Button pressed"));
        secret_button();
    }

    bool GPS_debug = 0;
    unsigned long t = millis();
    while (ss.available() > 0 || t + 1100 < millis()){
        gps.encode(ss.read());
        GPS_debug = 1;
    }
    if (GPS_debug) {
        Serial.println(F("GPS received"));
        GPS_debug = 0;
    }


    sats_fix = gps.satellites.value();
    hdop = gps.hdop.value();
    position_lat = gps.location.lat();
    position_lon = gps.location.lng();

    //target = 3; // debug
    distance = gps.distanceBetween(position_lat, position_lon, target_lat[target], target_lon[target]);

    //distance = 4; // debug

    static int sim, ok;
#ifdef SIMULATION
    if (but == 1){
        sim = 1;
        delay (1000);
    }
    else {
        sim = 0;
        lcd.setCursor(14, 1);
        lcd.print(target);
    }

    if (distance > 30 && sim == 0){
#else
    if (distance > 30) {
#endif
        lcd_target(lcd, target, distance, sats_fix);
    }
    else {

#ifdef BUTTON_FOR_NEXT_STEP
        wait finish;
        finish.set_time(2500);
        finish.set_steps(2);
        lcd.setCursor(0, 0);

        if(finish.step() == 1){
            lcd.print(F("You are at your "));
            lcd.setCursor(0, 1);
            lcd.print(F("current target."));
        }
        if (finish.step() == 2){
            lcd.setCursor(0, 0);
            lcd.print(F("Press the button"));
            lcd.setCursor(0, 1);
            lcd.print(F("to continue...  "));
        }

        if (but == 1){
            // Go to next target
            target++;
            ok = 1;



        }
#else
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print(F("You are at your "));
        lcd.setCursor(0, 1);
        lcd.print(F("current target."));
        target++;
        delay(5000);
        but = 0;
#endif
        // Open the box at the end of game
        if(target > NUMBER_OF_TARGETS){
            lcd.clear();
            lcd.setCursor(0, 0);
            lcd.print(F("CONRATULATIONS! "));
            delay(5000);
            open_box();
        }
        else {
            EEPROM.write(EEPROM_TARGET_INDEX, target);
        }

    }

    if(millis() > update_time + 1000){
        update_time = millis();

        Serial.println(gps.location.lat(), 6);
        Serial.print(F("LAT="));  Serial.println(gps.location.lat(), 6);
        Serial.print(F("LONG=")); Serial.println(gps.location.lng(), 6);
        Serial.print(F("ALT="));  Serial.println(gps.altitude.meters());
        Serial.print(F("hdop="));  Serial.println(hdop);
        Serial.print(F("sats="));  Serial.println(sats_fix);

        if(hdop < 500 && sats_fix > 4){
            fix = 1;
            distance = gps.distanceBetween(position_lat, position_lon, TARGET_1_LAT, TARGET_1_LON);
        }
        else{
            fix = 0;
            strength = sats_fix;
            //lcd_gps_signal(lcd, strength);
        }
    }

    if(millis() > sleep_time + SLEEP_TIME_MS){
        go_sleep();
    }
}

void go_sleep(void)
{
    lcd.clear();
    lcd.print(F("Going to sleep.."));
    delay(2000);
    pinMode(ON_PIN, OUTPUT);
    digitalWrite(ON_PIN, 0);
}
Example #4
0
int robot_command (uint16_t *cmd, uint16_t *resp, uint8_t *resplen)
{    
 unsigned long start = 0;
 uint8_t infolen = 0;
 int checkdir;
 int motor_state_save;
 int error = -1;
 int ret = SUCCESS;
 
 lcd.clear();     // clear LCD
 reset_leds();    // turn off all leds
 digitalWrite(Led_Blue, HIGH);  // turn on led blue
 
 switch (cmd[0]) {
 
 case CMD_STOP:
     Serial.println("CMD_STOP");
     lcd.print("STOP"); 
     
     stop();
     motor_state = STATE_STOP;
     *resplen = 0;
     break; 
  
 case CMD_START:
     if (cmd[1] == 0)
     {  
           Serial.println("CMD_START");
           lcd.print("START"); 
           
           start_forward();
     }
     else
     {       
           Serial.print("CMD_START_TEST motor: "); Serial.println((int)cmd[1]);
           lcd.print("START motor: "); lcd.print((int)cmd[1]); 
           
           start_forward_test(cmd[1]);           
     }                      
     motor_state = STATE_GO;
     *resplen = 0;
     break; 
 
 case CMD_CHECK_AROUND:
     Serial.println("CMD_CHECK_AROUND");
     lcd.print("CHECK AROUND");
    
     checkdir = check_around();
     
     lcd.setCursor(0,1); 
     if      (checkdir == DIRECTION_LEFT)  lcd.print("LEFT");
     else if (checkdir == DIRECTION_RIGHT) lcd.print("RIGHT");
     else if (checkdir == OBSTACLE_LEFT)   lcd.print("OBSTACLE LEFT");
     else if (checkdir == OBSTACLE_RIGHT)  lcd.print("OBSTACLE RIGHT");
     else if (checkdir == OBSTACLE)        lcd.print("OBSTACLE");
     else                                  lcd.print("?");

     resp[0] = checkdir;
     *resplen = 0+1;
     break;
     
 case CMD_MOVE_TILT_PAN:    
     Serial.print("CMD_MOVE_TILT_PAN: "); Serial.print((int)cmd[1]);Serial.print((int)cmd[2]);Serial.print((int)cmd[3]);Serial.println((int)cmd[4]);    
     if (cmd[2] == 0) HPos = (int)cmd[1] + 90; else HPos = 90 - (int)cmd[1]; 
     if (cmd[4] == 0) VPos = (int)cmd[3] + 90; else VPos = 90 - (int)cmd[3]; 
     Serial.print("CMD_MOVE_TILT_PAN, HPos VPos: "); Serial.print(HPos);Serial.println(VPos);   
     lcd.print("MOVE TILT&PAN");
     lcd.setCursor(0,1); 
     lcd.print("X: ");
     lcd.print(HPos);
     lcd.print(" Y: ");
     lcd.print(VPos);
     
     TiltPan_move(HPos, VPos);
    
     *resplen = 0;
     break; 
                    
 case CMD_TURN:
       if (cmd[1] == 180)
     {       
           Serial.print("CMD_TURN_BACK");
           lcd.print("TURN BACK ");  
         
           ret = turnback (10);  // 10s max
           if (ret != SUCCESS){
           	  Serial.print("turnback error"); Serial.println(ret);
           	  lcd.setCursor(0,1); 
           	  lcd.print("turnback error: "); lcd.print(ret);
           	  error = 1;
           }
     }       
     else if (motor_state == STATE_GO)
     { 
           Serial.print("CMD_TURN, alpha: "); Serial.print((cmd[2] != 1) ? ('+'):('-')); Serial.println((int)cmd[1]); 
           lcd.print("TURN"); lcd.print((cmd[2] != 1) ? ('+'):('-')); lcd.print((int)cmd[1]);lcd.print((char)223); //degree   
         
           ret = turn ((double)((cmd[2] != 1) ? (cmd[1]):(-cmd[1])), 5);  // 5s max        
           if (ret != SUCCESS){
           	  Serial.print("turn error"); Serial.println(ret);
           	  lcd.setCursor(0,1); 
           	  lcd.print(" turn error: "); lcd.print(ret);
           	  error = 1;
           }           
    }
    
    *resplen = 0;
    break;        
     
     
 case CMD_INFOS: 
     Serial.println("CMD_INFOS");

     ret = infos (resp, &infolen);
       
     if (resp[MOTOR_STATE] == STATE_GO) {
         lcd.print("RUNING");
     }    
     else
     {
         lcd.print("STOPPED");
     }
     lcd.setCursor(0,1);   
     lcd.print((int)resp[TEMPERATURE]); lcd.print((byte)lcd_celcius);lcd.write(lcd_pipe);   
     lcd.print((int)resp[DISTANCE]); lcd.print("cm");lcd.write(lcd_pipe); 
     lcd.print((int)resp[DIRECTION]); lcd.print((char)223); //degree    
 
     *resplen = infolen;
     break;
      
 case CMD_PICTURE: 
     Serial.print("CMD_PICTURE, no_picture: ");
     no_picture++;
     Serial.println(no_picture);
     lcd.print("PICTURE ");
     
     motor_state_save = motor_state;
     if (motor_state == STATE_GO) {
        Serial.println("Stop"); 
        stop();
        motor_state = STATE_STOP;
      }
   
     ret = JPEGCamera.makePicture (no_picture);
     if (ret == SUCCESS)
     { 
        lcd.setCursor(0,1);
        lcd.print("picture: "); lcd.print(no_picture);
     }
     else
     {
        Serial.print("makePicture error: "); Serial.println(ret);
        lcd.setCursor(0,1); 
        lcd.print("error: "); lcd.print(ret);       
        error = 1;
     }
       
     if (motor_state_save == STATE_GO) {          
        Serial.println("Start");
        start_forward();                     
        motor_state = STATE_GO;
     }
        
     // byte 0: picture number
     resp[0] = no_picture;
     *resplen = 0+1;    
     break;

 case CMD_ALERT: 
     Serial.println("CMD_ALERT");
     lcd.print("Alert"); 
    
     blink(Led_Blue);  
     buzz(5); 
     
     // If motor_state == STATE_GO => Stop           
     if (motor_state == STATE_GO) {
        Serial.println("Stop"); 
        stop();
        motor_state = STATE_STOP;
     }
   
     // Make 3 pictures left, front and right
     if ((HPos != 90) || (VPos !=90))
     { 
        HPos = 90;
        VPos = 90;
        TiltPan_move(HPos, VPos);
     }
     
     Serial.print("makePicture, no_picture: ");
     no_picture++;
     Serial.println(no_picture);
     lcd.print("PICTURE ");
     
     ret = JPEGCamera.makePicture (no_picture);
     if (ret == SUCCESS)
     { 
        lcd.setCursor(0,1);
        lcd.print("picture: "); lcd.print(no_picture);
     }
     else
     {
        Serial.print("makePicture error: "); Serial.println(ret);
        lcd.setCursor(0,1); 
        lcd.print("error: "); lcd.print(ret);       
        error = 1;
     }
             
     if (ret == SUCCESS)
     { 
        HPos = 0;
        VPos = 90;
        TiltPan_move(HPos, VPos);

        Serial.print("makePicture, no_picture: ");
        no_picture++;
        Serial.println(no_picture);
        lcd.print("PICTURE ");
        
        ret = JPEGCamera.makePicture (no_picture);

        lcd.setCursor(0,1);
        lcd.print("picture: "); lcd.print(no_picture);
     }
     else
     {
        Serial.print("makePicture error: "); Serial.println(ret);
        lcd.setCursor(0,1); 
        lcd.print("error: "); lcd.print(ret);       
        error = 1;
     }
      
     if (ret == SUCCESS)
     { 
        HPos = 180;
        VPos = 90;
        TiltPan_move(HPos, VPos);
     
        Serial.print("makePicture, no_picture: ");
        no_picture++;
        Serial.println(no_picture);
        lcd.print("PICTURE ");
        
        ret = JPEGCamera.makePicture (no_picture);

        lcd.setCursor(0,1);
        lcd.print("picture: "); lcd.print(no_picture);
     }
     else
     {
        Serial.print("makePicture error: "); Serial.println(ret);
        lcd.setCursor(0,1); 
        lcd.print("error: "); lcd.print(ret);       
        error = 1;
     }

     // byte 0: last picture number
     resp[0] = no_picture;
     *resplen = 0+1;
          
     HPos = 90;
     VPos = 90;
     TiltPan_move(HPos, VPos);
              
     break; 
     
 case CMD_CHECK: 
     Serial.println("CMD_CHECK");
     lcd.print("Check"); 
        
     alert_status = check();
     
     if (alert_status != SUCCESS) {
           Serial.print("Alert detected: ");Serial.println(alert_status);
           lcd.setCursor(0,1); 
           lcd.print("Alert: "); lcd.print(alert_status);                
     }
     else
     {
           Serial.print("No alert detected: ");
           lcd.setCursor(0,1); 
           lcd.print(" No Alert");               
     } 
  
     // byte 0: alert
     resp[0] = alert_status;
     *resplen = 0+1;
     break; 
  
 case CMD_GO: 
     Serial.print("CMD_GO, nb seconds: "); Serial.print((int)cmd[1]);
     lcd.print("GO ");lcd.print((int)cmd[1]);lcd.print("secs");
     
     if (motor_state != STATE_GO)
     {  
           Serial.println("start_forward");
           start_forward();
           motor_state = STATE_GO;
     }
     
     error = -1;
     GOtimeout = (unsigned long)cmd[1];
     start = millis();
     while((millis() - start < GOtimeout*1000) && (error == -1)) {
          ret = go(GOtimeout);  
     
          if ((ret != SUCCESS) && (ret != OBSTACLE) && (ret != OBSTACLE_LEFT) && (ret != OBSTACLE_RIGHT))
          {
              stop();
              motor_state = STATE_STOP;
     	      error = 1;
     	                   
              Serial.print("CMD_GO error: "); Serial.println(ret);
              Serial.println("Stop");              
              lcd.setCursor(0,1); 
              lcd.print("error: "); lcd.print(ret);                

          }
          else if ((ret == OBSTACLE) || (ret == OBSTACLE_LEFT) || (ret == OBSTACLE_RIGHT))
          {
              stop();
              motor_state = STATE_STOP;
              
              buzz(3);
              blink(Led_Red);     
              Serial.print("CMD_GO Obstacle: ");Serial.println(ret);
              Serial.println("Stop");
              lcd.setCursor(0,1); 
              if (ret == OBSTACLE_LEFT)        lcd.print("OBSTACLE LEFT");
              else if (ret == OBSTACLE_RIGHT)  lcd.print("OBSTACLE RIGHT");
              else if (ret == OBSTACLE)        lcd.print("OBSTACLE");
              else 

                              
              ret = SUCCESS;            
              checkdir = check_around();
         
              Serial.print("check_around, direction: "); Serial.println(checkdir);
              lcd.clear();
              lcd.print("check around");
              lcd.setCursor(0,1); 
              if      (checkdir == DIRECTION_LEFT)  lcd.print("LEFT");
              else if (checkdir == DIRECTION_RIGHT) lcd.print("RIGHT");
              else if (checkdir == OBSTACLE_LEFT)   lcd.print("OBSTACLE LEFT");
              else if (checkdir == OBSTACLE_RIGHT)  lcd.print("OBSTACLE RIGHT");
              else if (checkdir == OBSTACLE)        lcd.print("OBSTACLE");
              else                             lcd.print("?");;
         
              if (checkdir == DIRECTION_LEFT) {
                   start_forward();
                   motor_state = STATE_GO;
                   ret = turn (-45,  5); // turn  -45 degrees during 5s max
                   if (ret != SUCCESS)
                   {
                      stop();
                      motor_state = STATE_STOP;                   	  
                   	  error = 1;
                   	                     	  
                   	  Serial.print("turn error: "); Serial.println(ret);
                      Serial.println("Stop");                                         	  
                   	  lcd.clear();                   	  
                   	  lcd.print("turn left");
                   	  lcd.setCursor(0,1);
                   	  lcd.print("error: "); lcd.print(ret); 
                   }
                   else
                   {
                      lcd.clear();                   	  
                   	  lcd.print("turn left OK");                  	
                   }
              }
              else if (checkdir == DIRECTION_RIGHT) {
                   start_forward();
                   motor_state = STATE_GO;
                   ret = turn (+45,  5); // turn  +45 degrees during 5s max
                   if (ret != SUCCESS)
                   {
                   	  stop();
                      motor_state = STATE_STOP;  
                   	  error = 1;
                   	                     	 
                   	  Serial.print("turn error: "); Serial.println(ret);
                   	  Serial.println("Stop"); 
                   	  lcd.clear();                   	  
                   	  lcd.print("turn right");
                   	  lcd.setCursor(0,1);
                   	  lcd.print("error: "); lcd.print(ret); 
                   }
                   else
                   {
                      lcd.clear();                   	  
                   	  lcd.print("turn right OK");                  	
                   }                  
              }
              else 
              {
              	   buzz(3);
                   blink(Led_Red);
              	   motor_state = STATE_GO;
              	   ret = turnback (10); // turn back during 10s max
                   if (ret != SUCCESS)
                   {
                      stop();
                      motor_state = STATE_STOP;
                      error = 1; 
                      
                      Serial.print("turnback error"); Serial.println(ret);
                   	  Serial.println("Stop");
                   	  lcd.clear();                   	  
                   	  lcd.print("turnback");
                   	  lcd.setCursor(0,1);
                   	  lcd.print("error: "); lcd.print(ret);                    	                                           	  
                   }
                   else
                   {
                      lcd.clear();                   	  
                   	  lcd.print("turnback OK");                  	
                   }                   
              }                 
          }
          else
          {
              	   Serial.println("GO OK");
              	   lcd.clear();                   	  
                   lcd.print("GO OK");
                   error = 0;
          }           
     } // end while
     
     ret = infos (resp, &infolen);
              
     if (error == 0) {
         if (resp[MOTOR_STATE] == STATE_GO) {
            lcd.print("RUNNING");
          }    
         else
         {
             lcd.print("STOPPED");
         }
         lcd.setCursor(0,1);   
         lcd.print((int)resp[TEMPERATURE]); lcd.print((byte)lcd_celcius);lcd.write(lcd_pipe);   
         lcd.print((int)resp[DISTANCE]); lcd.print("cm");lcd.write(lcd_pipe);
         lcd.print((int)resp[DIRECTION]); lcd.print((char)223); //degree   
     } 
     
     *resplen = infolen;      
     break;
 
 case CMD_PI: 
     Serial.print("CMD_PI activated, type: "); Serial.print((int)cmd[1]);  Serial.print(" - freq: ");Serial.println((int)cmd[2]);
     lcd.print("PI activated ");lcd.print((int)cmd[1]);
 
     PI_activated = (int)cmd[1];
     if (PI_activated <> PI_NO_COMM) PI_freqInfos = (int)cmd[2]* 1000;
        
     Serial.print("PI_activated: "); Serial.println(PI_activated);
     Serial.print("PI_freqInfos: ");Serial.println(PI_freqInfos);
  
     
    *resplen = 0;     
     break;
          
 default:
    Serial.println("invalid command");
    lcd.print("invalid command");
    
    *resplen = 0;      
    break;                     
 
 } //end switch
    
    
 if (error == 1) {
    blink(Led_Red);
    blink(Led_Red);
    buzz(7);
    *resplen = 0; 
 }
                        
 reset_leds();    // turn off all leds
 return ret;
}
Example #5
0
int robot_begin()
{
  int ret = SUCCESS;
  int ivalue = 0;
  
  ret = motor_begin(); 
  if (ret != SUCCESS) return ret;

  Serial.println("Begin Robot Init");
  Serial.println("****************");
  
  lcd.clear();
  lcd.print("Begin Robot Init");
   
  pinMode(Led_Green, OUTPUT);      // set the pin as output
  blink(Led_Green); 
  pinMode(Led_Red, OUTPUT);      // set the pin as output
  blink(Led_Red);  
  pinMode(Led_Blue, OUTPUT);     // set the pin as output
  blink(Led_Blue);
  Serial.println("Init Leds OK");
    
  // initialize the buzzer
  pinMode(buzzPin, OUTPUT); 
  buzz(3);       
  Serial.println("Init Buzzer OK");
   
  // initialize the Tilt&Pan servos  
  TiltPan_begin(HSERVO_Pin, VSERVO_Pin);
  Serial.println("Init Tilt&Pan servos OK");

  // initialize the camera
  Serial.println(" ");
  Serial.println("Begin Init Camera...");
  ret=JPEGCamera.begin();
  if (ret != SUCCESS)
  {  
        Serial.print("Error Init Camera, error: ");
        Serial.println(ret);
        lcd.setCursor(0,1); 
        lcd.print("Init Camera KO  ");
  }        
  else
  {
        Serial.println("Init Camera OK");
        lcd.setCursor(0,1); 
        lcd.print("Init Camera OK  ");
  } 
  delay(5*1000);lcd.clear();    
  
  // initialize the SD-Card    
  ret = initSDCard();
  if (ret != SUCCESS)
  {  
        Serial.print("Error Init SD-Card, error: ");
        Serial.println(ret);
        lcd.print("Init SD-Card KO ");
  }                                                                    
  else
  {
        Serial.println("Init SD-Card OK");
        lcd.print("Init SD-Card OK ");
  }
    
  // get infos from SD-Card  
  ret=infoSDCard();
  if (ret < 0)
  {  
        Serial.print("Error Infos SD-Card, error: ");
        Serial.println(ret);
        lcd.setCursor(0,1); 
        lcd.print("Err Infos SDCard");
  }
  else
  {
        no_picture = ret+1;
        Serial.print("no_picture starts at: ");
        Serial.println(no_picture);
        lcd.setCursor(0,1); 
        lcd.print("Num picture:");lcd.print(no_picture);
  }   
  delay(5*1000);lcd.clear();  
    
    
  // initialize the brightness sensor   
  TEMT6000.TEMT6000_init(TEMT6000_Pin); // initialize the pin connected to the sensor
  Serial.println("Init Brightness sensor OK");
  
  ivalue = TEMT6000.TEMT6000_getLight();
  Serial.print("Value between 0 (dark) and 1023 (bright): ");
  Serial.println(ivalue); 
  lcd.print("Lux:");lcd.print(ivalue);lcd.printByte(lcd_pipe);
  
  // initialize the temperature sensor   
  TMP102.TMP102_init();
  Serial.println("Init Temperature sensor OK");
  
  double temperature = TMP102.TMP102_read();
  ivalue = (int)(100.0 * temperature);
  Serial.print("Temperature: ");
  Serial.println(ivalue); 
  lcd.print("T:");lcd.print(ivalue);lcd.printByte(lcd_celcius);lcd.printByte(lcd_pipe);   
  
  // initialize the electret micro   
  //Micro.Micro_init(MICRO_Pin); // initialize the pin connected to the micro
  //Serial.println("Init Micro OK");

  // initialize the motion sensor
  pinMode(MOTION_PIN, INPUT);
  Serial.println("Init Motion sensor OK");

  // initialize the IOT Serial 1 
  IOTSerial.IOTSbegin(1); // initialize the IOT Serial 1 to communicate with IOT WIFClient ESP8266
  Serial.println ("Init IOT Serial 1 to communicate with IOT WIFClient ESP8266 OK");
  
  // initialize the IOT Serial 2, interrupt setting
  IOTSerial.IOTSbegin(2); // initialize the IOT Serial 2 to communicate with IOT WIFServer ESP8266
  Serial.println ("Init IOT Serial 2 to communicate with IOT WIFServer ESP8266 OK");
  pinMode(IOT_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(IOT_PIN), IntrIOT, RISING);         // set IOT interrupt
 
  interrupts(); // enable all interrupts
  Serial.print("Init Interrupts OK, IntIOT: "); Serial.println(IntIOT);
  
  lcd.setCursor(0,1); 
  lcd.print("End   Robot Init");
  delay(5*1000);lcd.clear();  
 
  Serial.println("End Robot Init");
  Serial.println("**************");
  Serial.println("");
 
  return SUCCESS;
  
} 
Example #6
0
double FindTension()
{

// ********* First part sweeps increasing frequency************
      connection = true; //assume a wire is connected abefore testing if it is not
      dfreq = lowfreq - 100.0;  //start at lower freq to allow supply stabilisation before starting to sample
      foundmaxdn = false;
      foundmaxup = false;
      centrefreq = 0;

       lcd.setCursor(0, 0);
       lcd.print("      RUNNING      ");

      sweep =  true;
      while (sweep == true)
      {
        if (sinevalue >= 254 )      // test if we are in the undriven half cycle
        {
    
          if (adcflag == 1 && dfreq <= lowfreq - 90.0)  // test if outside scan range and sample adc2
          {
            adcflag = 0;                // if adc2 is  30 * 4.9mV then there is no wire connected
            connection = true;          // assume wire is connected
            if (analogRead(2) > 30)
            { connection = false;        // no wire connected
              lcd.setCursor(0, 2);
              lcd.print("F     NO WIRE       ");
              lcd.setCursor(0, 3);
              lcd.print("T                   ");
            }
          }
          if (adcflag == 1 && dfreq > lowfreq)  // test if within scan range
          { // adcflag=0;
            sbi(PORTD, 6); cbi(PORTD, 6); // Test /reset PORTD,6 high to observe timing at start of analog read
            analogval = analogRead(0);   //   get value of signal from wire
    
            if (analogval > maxanalogval)
            { maxanalogval = analogval;
              centrefreq = dfreq;
              overmaxcount = 0;
            }
            if  (analogval < maxanalogval - 4)
            { overmaxcount++;
              if (overmaxcount >= maxcountlimit) {
                foundmaxup = true;
              }
            }
          }
        }
    
        if (pulsecount > cycles)    // keep same frequency for n cycles
        { pulsecount = 0;
          dfreq = dfreq + 4.0;              //Increment scan freq by a few Hz
          cbi (TIMSK2, TOIE2);             // disable Timer2 Interrupt
          tword_m = pow(2, 32) * dfreq / refclk; // calculate new tuning word
          sbi (TIMSK2, TOIE2);             // enable Timer2 Interrupt
          // test if sweep finished
          if (dfreq > highfreq  || overmaxcount > maxcountlimit )
          {
            centrefrequpKHz = 0;
            centrefrequpKHz = centrefreq / 1000.0;
            lcd.setCursor(15, 2);
            lcd.print("     ");
            lcd.setCursor(2, 2);
            if (connection == true && centrefrequpKHz != 0)
            { lcd.print(centrefrequpKHz);
            Tensionup = 0;
              Tensionup = LMDX * centrefrequpKHz * centrefrequpKHz; // in grams
              lcd.setCursor(2, 3);
              lcd.print(Tensionup);
              
            }
            maxanalogval = 0;
            overmaxcount = 0;
            lcd.setCursor(15, 2);
            lcd.print("     ");          //  now clear average values on LCD
            lcd.setCursor(15, 3);
            lcd.print("     ");
    
            sweep = false;
          }
        }
      }
      // ********* Second part sweeps decreasing frequency************
      dfreq = highfreq + 100.0;  //start at Higher freq to allow supply stabilisation before starting to sample
      maxanalogval = 0;
      overmaxcount = 0;
      sweep = true;
      while (sweep == true)
      {
        if (sinevalue >= 254 )      // test if we are in the undriven half cycle
        {
          if (adcflag == 1 && dfreq < highfreq)  // test if within scan range
          {
            sbi(PORTD, 6); cbi(PORTD, 6); // Test /reset PORTD,6 high to observe timing at start of analog read
            analogval = analogRead(0);
    
            if (analogval > maxanalogval)
            { maxanalogval = analogval;
              centrefreq = dfreq;
              overmaxcount = 0;
            }
            if  (analogval < maxanalogval - 4)
            { overmaxcount++;
              if (overmaxcount >= maxcountlimit) {foundmaxdn = true;}
            }
          }
        }
    
        if (pulsecount > cycles)    // keep same frequency for n cycles
        { pulsecount = 0;
          dfreq = dfreq - freqinc;              //Decrement scan freq by a few Hz
          cbi (TIMSK2, TOIE2);             // disable Timer2 Interrupt
          tword_m = pow(2, 32) * dfreq / refclk; // calculate new tuning word
          sbi (TIMSK2, TOIE2);             // enable Timer2 Interrupt
          // test if sweep finished
          if (dfreq < lowfreq   || overmaxcount > maxcountlimit )
          {
            if (foundmaxdn == true && foundmaxup == true)
            {
              centrefreqdnKHz = 0;
              centrefreqdnKHz = centrefreq / 1000.0;
              lcd.setCursor(8, 2);
              lcd.print(centrefreqdnKHz);
              Tensiondn = 0;
              Tensiondn = LMDX * centrefreqdnKHz * centrefreqdnKHz; // in grams
              lcd.setCursor(8, 3);
              lcd.print(Tensiondn);
            }
            else if (foundmaxdn == false || foundmaxup == false)
            {    
              connection = true;          // assume wire is connected
              if (analogRead(2) > 30)
              { connection = false;        // no wire connected
                lcd.setCursor(0, 2);
                lcd.print("F      NO WIRE      ");
                lcd.setCursor(0, 3);
                lcd.print("T                   ");
              }
              else
              { lcd.setCursor(0, 2);
                lcd.print("F     NO SIGNAL     ");
                lcd.setCursor(0, 3);
                lcd.print("T                   ");

              }
            }
            maxanalogval = 0;
            overmaxcount = 0;
            sweep = false;
          }
        }
    
      }
      avgcnt = avgcnt + 1.0;
      centrefreqavgKHz = 0;
      TensionCalcd = 0;
      if (foundmaxdn == true && foundmaxup == true)    
      {
        centrefreqavgKHz = ( centrefrequpKHz + centrefreqdnKHz) / 2.0;
        lcd.setCursor(15, 2);
        lcd.print(centrefreqavgKHz);
        TensionCalcd =  LMDX * centrefreqavgKHz * centrefreqavgKHz;
      }

      
// if (abs(Tensiondn - Tensionup) > 10)
//  {
//    lcd.setCursor(0, 2);
//    lcd.print("F     NO SIGNAL     "); 
//  }

 
 return TensionCalcd;
  }
void setSoilHumidity() {
	lcd.clear();
	lcd.print("Soil moisture");
	lcd.setCursor(0, 1);
	lcd.print("Soil moisture");
}
Example #8
0
bool navigationCheckDestination()
{
  //Only process line following if navigation destination is known
  if ( currentNavigationDestination < 0 )
  {
    lcd.clear();
    lcd.print( "Where am I going?" );
    lcdSelectDestination();

#ifdef AUTO_RETURN_HOME
    unsigned long navigationDestinationPromptTime = millis();
#endif
    while ( digitalRead(LCD_UP_PIN) == HIGH ||
            digitalRead(LCD_DOWN_PIN) == HIGH ||
            digitalRead(LCD_PLAY_PIN) == HIGH ||
            digitalRead(LCD_STOP_PIN) == HIGH
          )
    {
#ifdef AUTO_RETURN_HOME
      if ( currentNavigationLocation != currentNavigationHome && navigationDestinationPromptTime + navigationReturnHomeTimeout * 1000ul < millis() )
      {
        //Timeout has occured.. we are going to return home
        lcd.setCursor(0, 1);
        lcd.print( navigationReturnHomeTimeout );
        lcd.print( " second timeout." );
        navigationReturnHome();
        break;
      }
#endif
      if ( digitalRead(LCD_UP_PIN) == LOW )
      {
        navigationSounds->play( soundUp );
        if ( --lcdCurrentSelection < 0 ) lcdCurrentSelection = MAP_LOCATION_COUNT - 1;
        break;
      }
      if ( digitalRead(LCD_DOWN_PIN) == LOW )
      {
        navigationSounds->play( soundDown );
        if ( ++lcdCurrentSelection == MAP_LOCATION_COUNT ) lcdCurrentSelection = 0;
        break;
      }
      if ( digitalRead(LCD_PLAY_PIN) == LOW )
      {
        if ( lcdCurrentSelection != currentNavigationLocation )
        {
          navigationSounds->play( soundUhoh );
          currentNavigationDestination = lcdCurrentSelection;

          /*
          Serial.print( "Current Destination: " );
          Serial.print( currentNavigationDestination );
          Serial.print( ": " );
          Serial.println( destinationList[currentNavigationDestination] );
          */

          lcd.clear();
          lcd.print( "Traveling from:" );
          lcd.setCursor(0, 1); lcd.print( destinationList[currentNavigationLocation] );
          lcd.setCursor(0, 2); lcd.print( "to:" );
          lcd.setCursor(0, 3); lcd.print( destinationList[currentNavigationDestination] );
          delay(1000);
          break;
        }
        else
        {
          navigationSounds->play( soundOhh );
        }
      }
      if ( navigationCheckEStop() )
      {
        navigationSounds->play( soundButtonPushed );
        currentNavigationLocation = -1; //Returning to previous question
        return false;
      }
    }
    return false;
  }
  return true;
}
Example #9
0
/*--(end setup )---*/
void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  loopCount = 0;
  changeState = digitalRead(ChangeScanBtn);
  
  if (!changeState)
 {
    if (scanRange == 1) //scan range start high
    {
    scanRange = 0;
    for (int i = 0; i < 100; i++){
      scanRange = 0; //terrible hack to add delay
       Serial.print("wait");
    } 
    //LOW SCAN
    double lowfreq = 800.0;  // 1115.0 calculated frequency for 0.3N ( 30gm) tension of 79mm wire ORIGINAL 
    double highfreq =  1600.0;   //  1703.0 calculated frequency for 0.7N (70gm) tension  ORIGINAL
    lcd.setCursor(19,0);//if button pressed and set to high currently
    lcd.print("L");//then change to low
    lcd.setCursor(0,3);
    lcd.print("LowFreq = 800-1600Hz");
    //change global vars here for scan range. 
    }

    
    else if (scanRange == 0)
    {
    scanRange = 1;
    for (int i = 0; i < 100; i++){
      scanRange = 1; //terrible hack to add delay
      Serial.print("wait") ;
    }     
  //HIGH SCAN
    double lowfreq = 1115.0;  // 1115.0 calculated frequency for 0.3N ( 30gm) tension of 79mm wire
    double highfreq = 1900.0; // upped frequency to reach above 70gm 
    //change global vars here
    lcd.setCursor(19,0);
    lcd.print("H");
    lcd.setCursor(0,3);
    lcd.print("HiFreq = 1115-1900Hz");
    }
    
  }
  
  buttonState = digitalRead(RunBtn);
  if (!buttonState)
  do
  {   
    if (loopCount == 0)
    {
    lcd.setCursor(0,2);
    lcd.print("                    ");
    lcd.setCursor(0,3);
    lcd.print("                    ");
   
    }
      loopCount = loopCount+1;

      lcd.setCursor(0, 1);
      lcd.print("  UP    DOWN   AVG  ");
      lcd.setCursor(0, 2);
      lcd.print("F");
      lcd.setCursor(0, 3);
      lcd.print("T");
      
    TensionCalcd = FindTension();
    lcd.setCursor(15, 3);
    lcd.print(TensionCalcd);  


    if (loopCount == 2 && TensionCalcd != 0) {
       lcd.setCursor(0, 0);
       lcd.print("      FINISHED     ");
       break;
    }
    if (TensionCalcd == 0){
       lcd.setCursor(0, 0);
       lcd.print("      FINISHED     ");
       lcd.setCursor(0, 1);
       lcd.print("                   ");
       lcd.setCursor(0, 2);
       lcd.print("      NO SIGNAL     ");
       lcd.setCursor(0, 3);
       lcd.print("                    ");
       break;
      }
  }
 while (loopCount < 3);

}
Example #10
0
void LcdPrint(const char c[]) {
  lcd27.print(c);
  lcd3E.print(c);
}
Example #11
0
void LcdPrintX(LiquidCrystal_I2C lcd, const char c[]) { lcd.print(c); }
Example #12
0
void updatelcd() 
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("XT");
  lcd.setCursor(3,0);
  lcd.print(_XEncoderTicks);
  lcd.setCursor(10,0);
  lcd.print("XI");
  lcd.setCursor(13,0);
  lcd.print(_XEncoderIndex);
  lcd.setCursor(18,0);
  lcd.print("X");
  lcd.setCursor(19,0);
  lcd.print(">" + _XMinPinSet);
  lcd.setCursor(17,0);
  lcd.print("<" + _XMaxPinSet);
  
  lcd.setCursor(0,1);
  lcd.print("YI");
  lcd.setCursor(3,1);
  lcd.print(_YEncoderTicks);
  lcd.setCursor(10,1);
  lcd.print("YI");
  lcd.setCursor(13,1);
  lcd.print(_YEncoderIndex);
  lcd.setCursor(18,1);
  lcd.print("Y");
  lcd.setCursor(19,1);
  lcd.print("V" + _YMinPinSet);
  lcd.setCursor(17,1);
  lcd.print("^" + _YMaxPinSet);
  /*
  lcd.setCursor(15,0);
  lcd.print(_XCsens);*/
  lcd.setCursor(3,3);
  lcd.print("<" + _XJoyLeft);
  lcd.setCursor(5,3);
  lcd.print(">" + _XJoyRight);
  lcd.setCursor(4,3);
  lcd.print("^" + _YJoyUp);
  lcd.setCursor(4,3);
  lcd.print("V" + _YJoyDown);
  delay(20);
}
Example #13
0
// { SPECIAL FUNCTIONS  -------------------------------------------------------
void setup(){
    // Required if SYSTEM_MODE = SEMI_AUTOMATIC or MANUAL
    // if(Particle.connected() == false){
    //     Particle.connect();
    // }
    
    pinMode(BOARD_LED, OUTPUT); //INPUT, INPUT_PULLUP, INPUT_PULLDOWN or OUTPUT
    // pinMode(DAC, OUTPUT); //INPUT, INPUT_PULLUP, INPUT_PULLDOWN or OUTPUT
    
    // pinMode( D6, INPUT );
    // attachInterrupt( D6, on_d6, CHANGE, 13);
    
    // Note: do not set the pinMode() with analogRead(). The pinMode() is
    // automatically set to AN_INPUT the first time analogRead() is called
    // src: https://docs.particle.io/reference/firmware/photon/#analogread-adc-
    // In other words, don't do: pinMode(analog_pin, INPUT);

    // { Declare Cloud Variables/Functions/Events  ----------------------------
    // Up to 20 Variables
    Particle.variable("help", HELP);
    Particle.variable("a", PV_a);
    Particle.variable("b", PV_a);
    Particle.variable("c", PV_a);
    
    // Up to 15 Functions. 
    Particle.function("do", PF_do);
    Particle.function("set", PF_set);
    
    #ifdef USE_STDIN
    Particle.function("stdin", PF_stdin);
    #endif // USE_STDIN
    
    // Up to 4 Event Subscriptions
    Particle.subscribe("all", PS_handler, MY_DEVICES);
    
    // For documentation specify what events you will publish:
    // Particle.publish("stdout", "...")
    // Particle.publish("stderr", "...")
    
    // } Declare Cloud Variables/Functions/Events  ----------------------------

    strcpy(PV_a, "Hello World");
    
    #ifdef USE_LCD
    lcd.init();
    lcd.backlight();
    lcd.clear();
    lcd.setCursor(0,1);
    lcd.print("Hello World");
    #endif
    
    #ifdef USE_SERIAL0
    Serial.begin(9600);
    #endif // USE_SERIAL0
    #ifdef USE_SERIAL1
    Serial1.begin(9600);
    #endif // USE_SERIAL1

    #ifdef USE_TIMER_2SEC
    timer_2sec.start();
    #endif // USE_TIMER_2SEC
}
Example #14
0
void loop()
{
  sen1.requestTemperatures();
  sen2.requestTemperatures();
  sen3.requestTemperatures();
  sen4.requestTemperatures();
  sen5.requestTemperatures();
  
  echoSerial();
  delay(1000);
  echoSerial();
  delay(1000);
    
  digitalWrite ( BACKLIGHT_PIN, HIGH );
  
  if(sen1.getTempCByIndex(0) < 80){
    tmp1=sen1.getTempCByIndex(0);
  }
  if(sen2.getTempCByIndex(0) < 80){
    tmp2=sen2.getTempCByIndex(0);
  }
  if(sen3.getTempCByIndex(0) < 80){
    tmp3=sen3.getTempCByIndex(0);
  }
  if(sen4.getTempCByIndex(0) < 80){
    tmp4=sen4.getTempCByIndex(0);
  }
  if(sen5.getTempCByIndex(0) < 80){
    tmp5=sen5.getTempCByIndex(0);
  }
  
   lcd.home ();                   // go home
  lcd.print("T1=");  
  lcd.print(tmp1);
  lcd.print(" T2=");
  lcd.print(tmp2);
  lcd.setCursor ( 0, 1 );        // go to the next line
  lcd.print("T3=");
  lcd.print(tmp3);
  lcd.print("  T4=");
  lcd.print(tmp4);
  
  Serial.print(tmp1);
  Serial.print("|");
  Serial.print(tmp2);
  Serial.print("|");
  Serial.print(tmp3);
  Serial.print("|");
  Serial.print(tmp4);
  Serial.print("|");
  Serial.print(tmp5);
  Serial.println(";");
 
  for(int i=0;i<5;i++){
    echoSerial();
    delay(1000);
  }
 
 digitalWrite ( BACKLIGHT_PIN, LOW );
  
}