element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Experts, Learning and Guidance
  • Technologies
  • More
Experts, Learning and Guidance
Ask an Expert Forum Ask your Expert: Nico teWinkel - Software Engineer who specialises in firmware, C and Java.
  • Blog
  • Forum
  • Documents
  • Leaderboard
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Experts, Learning and Guidance to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 21 replies
  • Subscribers 293 subscribers
  • Views 3051 views
  • Users 0 members are here
  • experts
  • ask_an_expert
  • ask_the_expert
  • software
Related
See a helpful answer?

Be sure to click 'more' and select 'suggest as answer'!

If you're the thread creator, be sure to click 'more' then 'Verify as Answer'!

Ask your Expert: Nico teWinkel - Software Engineer who specialises in firmware, C and Java.

patkelly
patkelly over 13 years ago

Nico is element14's very first member expert and member of the month for October 2011.image

Nico teWinkel's expertise: Software Engineer who  specialises in firmware, C and Java.

 

A little about Nico: "I've been a software engineer for more than  20 years. Electronics has been a hobby every since I played with the  old 10-in-1 and 200-in-1 RadioShack kits back in the 70s when I was just  a kid. Right now I'm working part time for a local hardware company  creating firmware for their devices, which brings together my software  engineering skills with my electronics hobby/obsession, and I'm having a  great time!

Software engineering in C and Java, including  iPhone and Android development. Currently developing firmware for Texas  Instruments chips using CCS. Any project involving interfacing Android  with external devices would really spark my interest."
 
See Nico's Blog
 
If you need help on anything, please post your question here!
 
If your not already, you'll need to login or register ask your question - just click reply
  • Sign in to reply
  • Cancel

Top Replies

  • johnbeetem
    johnbeetem over 12 years ago in reply to Former Member +1
    bryan molina wrote: As the software becomes more pervasive, risk to the public (due to faulty programs) become an increasingly concern. Develop a realistic doomsday scenario where the failure of a computer…
  • roineust
    roineust over 12 years ago in reply to ntewinkel +1
    Hello Nico! I have tried to compile, upload and watch trough the serial monitor the test code here, that you sent me and it was working fine. Then i discovered that i need to connect to the bluetooth module…
  • roineust
    roineust over 12 years ago in reply to ntewinkel +1
    Hey Nico! Finally i am on the horse! Altough, still at the beginning of the physical building of the robot shape itself and thus, have not yet delt with the issue of 3D priniting these parts - but at last…
Parents
  • roineust
    roineust over 12 years ago

    Hello Nico!

     

    There seems to be a problem in my code, that arise from both the servo and the sound libraries trying to use together pin 11 timer.

     

    I have tried to replace the Servo.h library with ServoTimer2.h library, but it did not help.

     

    Can you please take a look and tell me if you know of any solution to this problem?

     

    What happens with the robot itself, is that when the tank is close enough to a wall and the distance function kicks in the sound sample, the servos stop working and just make these funny uncontroled movement each time the sound sample is played.

     

    Here is the code:

     

     

    #include <ServoTimer2.h>

    #include <PCM.h> // This is the sound library

     

     

    const unsigned char sample[] PROGMEM = {

      126, 127, 128, 128, 128, 128, 128, 127 //  .......Here comes a very long array of numbers, that describe the sound sample and i took them off, for the sake of readablilty.

     

    }; //This is the sound sample

     

     

      #define trigPin 13

    #define echoPin 12

     

     

    char val; // variable to receive data from the serial port

     

     

    ServoTimer2 wheel1;

    ServoTimer2 wheel2;

     

     

    ServoTimer2 camera_servo1;

    ServoTimer2 camera_servo2;

     

     

    char mem=' '; //remember the last menu choice on the BlueControl app

     

     

    int camera_servo1_mem_servo1=1500;  // remember the camera servo1 last angle so they will not exceed their limits

    int camera_servo2_mem_servo2=1500;  // remember the camera servo1 last angle so they will not exceed their limits

     

     

    int led =  10;    // LED connected to digital pin 13

    int led_count = 225; // LED minimum dim value brfore it starts lighting

     

     

    int light_saver_timer=0; // Count 3.5 minutes with no action to shut light and save battery

     

     

    void setup() {

     

      Serial.begin(9600);       // start serial communication at 9600bps

     

     

      pinMode(trigPin, OUTPUT); //initiate ultrasound sensor trig pin

      pinMode(echoPin, INPUT);  //initiate ultrasound sensor echi pin

     

      pinMode(led, OUTPUT); //initiate led pin

     

     

      pinMode(11,OUTPUT);  //initiate speaker pin

      digitalWrite(11,LOW); //initiate speaker pin

     

     

    }

     

     

    void loop() {

     

     

      long duration, distance; // Next lines deal with finding and calculating ultrasound distance sensor

     

      digitalWrite(trigPin, LOW);  // Added this line

      delayMicroseconds(2); // Added this line

      digitalWrite(trigPin, HIGH);

     

     

      delayMicroseconds(10); // Added this line

      digitalWrite(trigPin, LOW);

     

      duration = pulseIn(echoPin, HIGH);

      distance = (duration/2) / 29.1;

     

     

     

      if (millis()-light_saver_timer > 210000){ // Count 3.5 minutes with no action to shut light and save battery

        led_count=225;

        light_saver_timer=millis();

        analogWrite(led,led_count);

      }

     

     

      if( Serial.available() ){       // if data is available to read

     

         val = Serial.read();         // read it and store it in 'val'

      }

     

      //Next lines save the last value that was chosen in the BlueControl app menu

     

      if( val == 'a') 

             {

                mem='a';

     

                val=' ';

             }

       else if( val == 'c')

              {

                mem='c';

     

                val=' ';

              }

      else if( val == 'd')

              {

                mem='d';

     

                val=' ';

              }

              else if( val == 'e')

              {

                mem='e';

     

                val=' ';

              }

     

     

    else if( val == 'U' && mem == 'a' )

     

          {

     

          light_saver(); 

     

          drive_forward_long(); 

     

          val=' ';

     

         }

       else if(val == 'U' && mem == 'c' )

     

         {

     

          light_saver();

     

          drive_forward_short(); 

     

          val=' ';

     

         }

     

       else if( val == 'D' && mem == 'a' )              

      {

     

        light_saver();

     

        drive_backward_long();

     

        val=' ';

      }

     

       else if( val == 'D' && mem == 'c' )              

      {

     

        light_saver();

     

        drive_backward_short();

     

        val=' ';

      }

     

       else if( val == 'R' )              

      {

     

        light_saver();

     

        turn_right(); 

     

         val=' ';

      }

       else if( val == 'L' )             

      {

     

        light_saver();

     

        turn_left();

     

         val=' ';

      }

     

      else if( val == 'C' )             

      {

     

        light_saver();

     

        motor_stop();

     

         val=' ';

      }

    else if( val == 'U' && mem == 'd' )

     

          {

     

          light_saver(); 

     

          camera_servo_up(); 

     

          val=' ';

     

         }

    else if( val == 'D' && mem == 'd' )

     

          {

     

          light_saver(); 

     

          camera_servo_down(); 

     

          val=' ';

     

         }

     

       else if(val == 'U' && mem == 'e' )

     

         {

     

           light_saver();

     

            if ( led_count < 255)

       {

     

        led_count=led_count+2;

        analogWrite(led, led_count);

     

       }

     

          val=' ';

     

         }  

     

     

          else if(val == 'D' && mem == 'e' )

     

         {

     

            light_saver();

     

            if ( led_count > 225)

       {

     

        led_count=led_count-2;

        analogWrite(led, led_count);

     

       }

         val=' ';

     

         }

     

     

        else if (distance <= 41){  //Next lines play the sound sample in different frequencies, according to disdtance measured by ultra-sound sensor

     

           startPlayback(sample, sizeof(sample));

     

      if (distance <=41 && distance >=27) delay(8500);

         else if (distance <=27 && distance >=20) delay(5250);

          else if (distance <=20 && distance >=17) delay(2440);

           else if (distance <=17 && distance >=12) delay(1500);

            else if (distance <=12 && distance >=8) delay(1080);

             else if (distance <=8 && distance >=5) delay(740);

              else if (distance <=5 && distance >=3) delay(530);

               else if (distance <=4 && distance >=2) delay(340);

                else if (distance <=2 && distance >=0) delay(180);

     

      }

     

    }

     

     

    void motor_stop(){

     

       wheel1.attach(6);

       wheel2.attach(5);

     

     

       wheel1.attach(6);

       wheel2.attach(5);

     

       wheel1.write(1458);  // set servo to mid-point

       wheel2.write(1418);

     

    }

     

     

    void drive_forward_long(){

     

     

      wheel1.attach(6);

    wheel2.attach(5);

     

    wheel1.write(2000); 

    wheel2.write(1000);

    delay(4000);

    wheel1.write(1458);  // set servo to mid-point

    wheel2.write(1418);

     

    }

     

     

    void drive_forward_short(){

     

     

    wheel1.attach(6);

    wheel2.attach(5);

     

    wheel1.write(2000); 

    wheel2.write(1000);

     

     

    delay(800);

     

     

    wheel1.write(1458);  // set servo to mid-point

    wheel2.write(1418);

     

    }

     

     

    void drive_backward_long(){

     

     

    wheel1.attach(6);

    wheel2.attach(5);

     

    wheel1.write(1000); 

    wheel2.write(2000);

     

    delay(4000);

     

    wheel1.write(1458);  // set servo to mid-point

    wheel2.write(1418);

     

     

     

     

    }

     

     

    void drive_backward_short(){

     

     

    wheel1.attach(6);

    wheel2.attach(5);

     

    wheel1.write(1000); 

    wheel2.write(2000);

     

     

    delay(800);

     

     

    wheel1.write(1458);  // set servo to mid-point

    wheel2.write(1418);

     

     

    }

     

     

     

     

    void turn_left(){

     

     

    wheel1.attach(6);

    wheel2.attach(5);

     

    wheel1.write(1000); 

    wheel2.write(1000);

     

    delay(150);

     

    wheel1.write(1458);  // set servo to mid-point

    wheel2.write(1418);

     

    }

     

     

    void turn_right(){

     

     

    wheel1.attach(6);

    wheel2.attach(5);

     

    wheel1.write(2000); 

    wheel2.write(2000);

     

    delay(150);

     

    wheel1.write(1458);  // set servo to mid-point

    wheel2.write(1418);

     

     

    }

     

     

    void camera_servo_up(){

     

     

    camera_servo1.attach(8);

    camera_servo2.attach(9);

     

    if (camera_servo1_mem_servo1>800 && camera_servo2_mem_servo2<2200)

    {

       camera_servo1_mem_servo1=camera_servo1_mem_servo1+30;

       camera_servo2_mem_servo2=camera_servo2_mem_servo2-30;

     

       camera_servo1.write(camera_servo1_mem_servo1);

       camera_servo2.write(camera_servo2_mem_servo2);

    }

     

     

    }

     

     

    void camera_servo_down(){

     

     

       camera_servo1.attach(8);

       camera_servo2.attach(9);

     

    if (camera_servo1_mem_servo1>800 && camera_servo2_mem_servo2<2200)

    {

     

       camera_servo1_mem_servo1=camera_servo1_mem_servo1-30;

       camera_servo2_mem_servo2=camera_servo2_mem_servo2+30;

     

       camera_servo1.write(camera_servo1_mem_servo1);

       camera_servo2.write(camera_servo2_mem_servo2);

    }

     

     

    }

     

     

    void light_saver() {

     

       light_saver_timer=millis();

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • ntewinkel
    ntewinkel over 12 years ago in reply to roineust

    Hi Roi,

     

    I think the issue is with your delays:

      if (distance <=41 && distance >=27) delay(8500);

     

    delay makes the program stop and just sit there, and while it's doing that none of your other code runs (except interrupts, which may be what keeps the sound going).

     

    I'm not sure what the purpose of the delays are, but maybe you can do that in some other way that still allows the robot to respond to other things?

     

    Cheers,

    -Nico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • roineust
    roineust over 12 years ago in reply to ntewinkel

    Hello Nico!

     

    The purpose of this section in the code, is that when the robot gets closer to the wall, the frequency of playback of the audio sample, will become higher. So the robot will say "Left" "Left" "Left"...etc or "Right" "Right" "Right"...etc at a higher or a lower frequency, according to the distance and direction of an obstacles on its sideways.

     

    What would be the best way to implement this functionality in the code, without casuing this delay problem?

     

    BTW, i had a problem when i was trying to implement the sections of the code, that deal with the ultra-sound sensor, when trying to implement it for 2 sensros and i was able to make only 1 sensor react to objects at various distances.

     

    Do you have any clue what am i doing wrong about it?

     

    Here is the original code, that i found here:

     

    http://www.instructables.com/id/Simple-Arduino-and-HC-SR04-Example/step3/Upload-the-sketch/

     

    And which on it, i tried to double many elements, so 2 sensors would be functional, but to no avail:

     

    Here is the code, after trying to adapt it to work with 2 sensors:

     

    /*

    HC-SR04 Ping distance sensor]

    VCC to arduino 5v GND to arduino GND

    Echo to Arduino pin 13 Trig to Arduino pin 12

    Red POS to Arduino pin 11

    Green POS to Arduino pin 10

    560 ohm resistor to both LED NEG and GRD power rail

    More info at: http://goo.gl/kJ8Gl

    Original code improvements to the Ping sketch sourced from Trollmaker.com

    Some code and wiring inspired by http://en.wikiversity.org/wiki/User:Dstaub/robotcar

    */

     

     

    #define trigPin 13

    #define echoPin 12

    #define trigPin1 7

    #define echoPin1 4

    //#define led 11

    //#define led2 10

     

     

    void setup() {

      Serial.begin (9600);

      pinMode(trigPin, OUTPUT);

      pinMode(echoPin, INPUT);

      pinMode(trigPin1, OUTPUT);

      pinMode(echoPin1, INPUT);

      //pinMode(led, OUTPUT);

      //pinMode(led2, OUTPUT);

    }

     

     

    void loop() {

      long duration, distance, duration1, distance1;

      digitalWrite(trigPin, LOW);  // Added this line

      delayMicroseconds(4); // Added this line

      digitalWrite(trigPin1, LOW);  // Added this line

      delayMicroseconds(4); // Added this line

      digitalWrite(trigPin, HIGH);

      //  delayMicroseconds(1000); - Removed this line

      delayMicroseconds(20); // Added this line

      digitalWrite(trigPin1, HIGH);

      //  delayMicroseconds(1000); - Removed this line

      delayMicroseconds(20); // Added this line

      digitalWrite(trigPin, LOW);

      digitalWrite(trigPin1, LOW);

      duration = pulseIn(echoPin, HIGH);

      duration1 = pulseIn(echoPin1, HIGH);

      distance = (duration/2) / 29.1;

      distance1 = (duration1/2) / 29.1;

      //if (distance < 4) {  // This is where the LED On/Off happens

       // digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off

    // digitalWrite(led2,LOW);

    //}

      //else {

       // digitalWrite(led,LOW);

       // digitalWrite(led2,HIGH);

    // }

      if (distance >= 200 || distance <= 0){

        Serial.println("Out of range LEFT");

      }

      else {

        Serial.println("LEFT");

        Serial.print(distance);

        Serial.println(" cm");

      }

     

       delay(1000);

     

      if (distance1 >= 200 || distance1 <= 0){

        Serial.println("Out of range RIGHT");

      }

      else {

        Serial.println("RIGHT");

        Serial.print(distance1);

        Serial.println(" cm");

      }

     

     

      delay(1000);

    }

     

     

     

    Thanks a lot,

    Roi.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • roineust
    roineust over 12 years ago in reply to ntewinkel

    Hello Nico!

     

    The purpose of this section in the code, is that when the robot gets closer to the wall, the frequency of playback of the audio sample, will become higher. So the robot will say "Left" "Left" "Left"...etc or "Right" "Right" "Right"...etc at a higher or a lower frequency, according to the distance and direction of an obstacles on its sideways.

     

    What would be the best way to implement this functionality in the code, without casuing this delay problem?

     

    BTW, i had a problem when i was trying to implement the sections of the code, that deal with the ultra-sound sensor, when trying to implement it for 2 sensros and i was able to make only 1 sensor react to objects at various distances.

     

    Do you have any clue what am i doing wrong about it?

     

    Here is the original code, that i found here:

     

    http://www.instructables.com/id/Simple-Arduino-and-HC-SR04-Example/step3/Upload-the-sketch/

     

    And which on it, i tried to double many elements, so 2 sensors would be functional, but to no avail:

     

    Here is the code, after trying to adapt it to work with 2 sensors:

     

    /*

    HC-SR04 Ping distance sensor]

    VCC to arduino 5v GND to arduino GND

    Echo to Arduino pin 13 Trig to Arduino pin 12

    Red POS to Arduino pin 11

    Green POS to Arduino pin 10

    560 ohm resistor to both LED NEG and GRD power rail

    More info at: http://goo.gl/kJ8Gl

    Original code improvements to the Ping sketch sourced from Trollmaker.com

    Some code and wiring inspired by http://en.wikiversity.org/wiki/User:Dstaub/robotcar

    */

     

     

    #define trigPin 13

    #define echoPin 12

    #define trigPin1 7

    #define echoPin1 4

    //#define led 11

    //#define led2 10

     

     

    void setup() {

      Serial.begin (9600);

      pinMode(trigPin, OUTPUT);

      pinMode(echoPin, INPUT);

      pinMode(trigPin1, OUTPUT);

      pinMode(echoPin1, INPUT);

      //pinMode(led, OUTPUT);

      //pinMode(led2, OUTPUT);

    }

     

     

    void loop() {

      long duration, distance, duration1, distance1;

      digitalWrite(trigPin, LOW);  // Added this line

      delayMicroseconds(4); // Added this line

      digitalWrite(trigPin1, LOW);  // Added this line

      delayMicroseconds(4); // Added this line

      digitalWrite(trigPin, HIGH);

      //  delayMicroseconds(1000); - Removed this line

      delayMicroseconds(20); // Added this line

      digitalWrite(trigPin1, HIGH);

      //  delayMicroseconds(1000); - Removed this line

      delayMicroseconds(20); // Added this line

      digitalWrite(trigPin, LOW);

      digitalWrite(trigPin1, LOW);

      duration = pulseIn(echoPin, HIGH);

      duration1 = pulseIn(echoPin1, HIGH);

      distance = (duration/2) / 29.1;

      distance1 = (duration1/2) / 29.1;

      //if (distance < 4) {  // This is where the LED On/Off happens

       // digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off

    // digitalWrite(led2,LOW);

    //}

      //else {

       // digitalWrite(led,LOW);

       // digitalWrite(led2,HIGH);

    // }

      if (distance >= 200 || distance <= 0){

        Serial.println("Out of range LEFT");

      }

      else {

        Serial.println("LEFT");

        Serial.print(distance);

        Serial.println(" cm");

      }

     

       delay(1000);

     

      if (distance1 >= 200 || distance1 <= 0){

        Serial.println("Out of range RIGHT");

      }

      else {

        Serial.println("RIGHT");

        Serial.print(distance1);

        Serial.println(" cm");

      }

     

     

      delay(1000);

    }

     

     

     

    Thanks a lot,

    Roi.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Children
  • ntewinkel
    ntewinkel over 12 years ago in reply to roineust

    Hi Roi,

     

    I don't have time to dig into your code, sorry (lots of deadlines this week). Maybe try posting this as a new discussion (marked as a question) in the Arduino forum, where more people can look into it for you. (www.element14.com/.../arduino, click "Start a Discussion" on the bar beside the orange "Join this group" button).

     

    For an alternative to using delay, you could try something like millis() to keep track of time, as is done in this tutorial:

    http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

     

    Or maybe the TimeAlarms library will help:

    http://www.pjrc.com/teensy/td_libs_TimeAlarms.html

     

    I'm wondering if you could set it to run the sound at desired intervals just using alarmRepeat.

     

    Cheers,

    -Nico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube