element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • 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
Design for a Cause - Design Challenge
  • Challenges & Projects
  • Design Challenges
  • Design for a Cause - Design Challenge
  • More
  • Cancel
Design for a Cause - Design Challenge
Blog Seeing EyeDuino - Blog #5 - Robot code
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: dwinhold
  • Date Created: 10 Aug 2018 12:06 PM Date Created
  • Views 787 views
  • Likes 7 likes
  • Comments 2 comments
  • mkr_1000
  • braille
  • robot
  • eyeduino
  • dwinhold
  • code
  • design for a cause - design challenge
Related
Recommended

Seeing EyeDuino - Blog #5 - Robot code

dwinhold
dwinhold
10 Aug 2018

For my 5th blog posting I will show the code I am using to run the robot and spelling in Braille. This part has been a bit of work but is coming along great. I have run into issues with the motors I have for driving the robot but that will be discussed in my next blog.

 

Below is the code (so far) for driving the robot. The Braille code hasn't been added in as of yet but the alphabet has been coded.

 

#include <Servo.h>

Servo myservo;  

//Drive Motors
const int MotorR1 = 6; //forward right motor 
const int MotorR2 = 7; // backward right motor 
const int MotorL1 = 4; // forward left motor 
const int MotorL2 = 5; // backward left motor 

//Ultrasonic Sensor
const int trigPin = 9; 
const int echoPin = 10; 
 
long duration; 
int distanceL;
int distanceF;
int distanceR;
int pos = 0;

void setup() {
 
  //Ultrasonic Sensor
  pinMode(trigPin, OUTPUT); 
  pinMode(echoPin, INPUT); 
  Serial.begin(9600); 
 
  //Drive Motors
  pinMode(MotorR1, OUTPUT);  
  pinMode(MotorR2, OUTPUT);  
  pinMode(MotorL1, OUTPUT);  
  pinMode(MotorL2, OUTPUT);  
  digitalWrite(MotorR1, LOW);  
  digitalWrite(MotorR2, LOW);  
  digitalWrite(MotorL1, LOW);  
  digitalWrite(MotorL2, LOW); 
  myservo.attach(8); 

  //Braille 
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);  
  pinMode(3, OUTPUT);  
  pinMode(11, OUTPUT);  
  pinMode(12, OUTPUT);  
  pinMode(13, OUTPUT);
  int timeA = 200;
  int timeB = 300;
  int timeC = 500;
    
}

void loop() {
  for (pos = 30; pos <= 90; pos += 1) { //Send servo to start position and turn to face forward
    // in steps of 1 degree
    myservo.write(pos);              
    delay(15);                     
  }

  //Ping and record distance 
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2); 
 
  digitalWrite(trigPin, HIGH); 
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW); 
  
  duration = pulseIn(echoPin, HIGH); 
 
  distanceF= duration*0.034/2;
  
  
  for (pos = 90; pos <= 150; pos += 1) { //Turn to far right 
    // in steps of 1 degree
    myservo.write(pos);             
    delay(15);                      
  }
  
  //Ping and record distance
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2); 
 
  digitalWrite(trigPin, HIGH); 
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW); 
 
 
  duration = pulseIn(echoPin, HIGH); 
 
  distanceR= duration*0.034/2;
  
    
  for (pos = 150; pos >= 90; pos -= 1) { //Return to forward position
    myservo.write(pos);              
    delay(15);                       
  }
  //Ping and record distance
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2); 
 
  digitalWrite(trigPin, HIGH); 
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW); 
 
 
  duration = pulseIn(echoPin, HIGH); 
 
  distanceF= duration*0.034/2;
  
  
  for (pos = 90; pos >= 30; pos -= 1) { //Move to far left position
    // in steps of 1 degree
    myservo.write(pos);             
    delay(15);                       
  }
  
  //Ping and record distance
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2); 
 
  digitalWrite(trigPin, HIGH); 
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW); 
 
 
  duration = pulseIn(echoPin, HIGH); 
 
  distanceL= duration*0.034/2;

//With ping distance information decide how to proceed
if (distanceL <= 600 && distanceL > 400)
    {
 
  //Add braille code here with warning
  
        //turn right  
        digitalWrite(MotorR1, LOW);  
        digitalWrite(MotorR2, HIGH);  
        digitalWrite(MotorL1, HIGH);  
        digitalWrite(MotorL2, LOW);  
        delay(500);
        digitalWrite(MotorR1, LOW);  
        digitalWrite(MotorR2, LOW);  
        digitalWrite(MotorL1, LOW);  
        digitalWrite(MotorL2, LOW);
        
    }
    
  if (distanceL <= 400)
    {
  //Add braille code here with warning
  
        //turn right  
        digitalWrite(MotorR1, LOW);  
        digitalWrite(MotorR2, HIGH);  
        digitalWrite(MotorL1, HIGH);  
        digitalWrite(MotorL2, LOW);  
        delay(750);
        digitalWrite(MotorR1, LOW);  
        digitalWrite(MotorR2, LOW);  
        digitalWrite(MotorL1, LOW);  
        digitalWrite(MotorL2, LOW);
        
    }
    
    if (distanceR <= 600 && distanceR >400)
    {
   //Add braille code here with warning
 
      //turn left  
      digitalWrite(MotorR1, HIGH);  
      digitalWrite(MotorR2, LOW);  
      digitalWrite(MotorL1, LOW);  
      digitalWrite(MotorL2, HIGH);  
      delay(500);
      digitalWrite(MotorR1, LOW);  
      digitalWrite(MotorR2, LOW);  
      digitalWrite(MotorL1, LOW);  
      digitalWrite(MotorL2, LOW);
    }
    
  if (distanceR <= 400)
    {
   //Add braille code here with warning
  
      //turn left  
      digitalWrite(MotorR1, HIGH);  
      digitalWrite(MotorR2, LOW);  
      digitalWrite(MotorL1, LOW);  
      digitalWrite(MotorL2, HIGH);  
      delay(750);
      digitalWrite(MotorR1, LOW);  
      digitalWrite(MotorR2, LOW);  
      digitalWrite(MotorL1, LOW);  
      digitalWrite(MotorL2, LOW);
    }

  if (distanceF >= 800)
    {
      //go forward  
      digitalWrite(MotorR1, HIGH);  
      digitalWrite(MotorR2, LOW);  
      digitalWrite(MotorL1, HIGH);  
      digitalWrite(MotorL2, LOW);  
      delay(2000);
    }
    
  if (distanceF < 800 && distanceF > 600)
    {
   //Add braille code here with warning
   
      //go forward  
      digitalWrite(MotorR1, HIGH);  
      digitalWrite(MotorR2, LOW);  
      digitalWrite(MotorL1, HIGH);  
      digitalWrite(MotorL2, LOW);  
      delay(1000);
    }

  if (distanceF < 599 && distanceF > 300)
    {
   //Add braille code here with warning
  
      //go forward  
      digitalWrite(MotorR1, HIGH);  
      digitalWrite(MotorR2, LOW);  
      digitalWrite(MotorL1, HIGH);  
      digitalWrite(MotorL2, LOW);  
      delay(500);
    }

  if (distanceF < 300)
    {
   //Add braille code here with warning

      //Stop  
      digitalWrite(MotorR1, LOW);  
      digitalWrite(MotorR2, LOW);  
      digitalWrite(MotorL1, LOW);  
      digitalWrite(MotorL2, LOW);  
      delay(1000);
    }

}

 

 

 

Below is the code I will be using for the Braille. I still have to decide what the warnings will say before it is added into the above code.

 

  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);  
  pinMode(3, OUTPUT);  
  pinMode(11, OUTPUT);  
  pinMode(12, OUTPUT);  
  pinMode(13, OUTPUT);
  
  int timeA = 200; //delay between on and off
  int timeB = 300; //delay between Letters
  int timeC = 500; //delay between Words
  
  
  //A & 1
  {
  digitalWrite (1, HIGH); 
  delay(timeA); //delay between on and off
  digitalWrite(1, LOW);
  }
  
  //B & 2
  {
  digitalWrite (1, HIGH); 
  digitalWrite (3, HIGH); 
  delay(timeA); //delay between on and off
  digitalWrite (1, LOW);
  digitalWrite (3, LOW); 
  }
  
  //C & 3
  {
  digitalWrite (1, HIGH); 
  digitalWrite (2, HIGH); 
  delay(timeA); //delay between on and off
  digitalWrite (1, LOW);
  digitalWrite (2, LOW); 
  }
  
  //D & 4
  {
  digitalWrite (1, HIGH); 
  digitalWrite (2, HIGH); 
  digitalWrite (11, HIGH); 
  delay(timeA); //delay between on and off
  digitalWrite (1, LOW);
  digitalWrite (2, LOW); 
  digitalWrite (11, LOW);
  }
  
  //E & 5
  {
  digitalWrite (1, HIGH); 
  digitalWrite (11, HIGH); 
  delay(timeA); //delay between on and off
  digitalWrite (1, LOW);
  digitalWrite (11, LOW);
  }
  
  //F & 6
  {
  digitalWrite (1, HIGH); 
  digitalWrite (2, HIGH); 
  digitalWrite (3, HIGH); 
  delay(timeA); //delay between on and off
  digitalWrite (1, LOW);
  digitalWrite (2, LOW); 
  digitalWrite (3, LOW);
  }
  
  //G & 7
  {
  digitalWrite (1, HIGH); 
  digitalWrite (2, HIGH); 
  digitalWrite (3, HIGH);
  digitalWrite (11, HIGH); 
  delay(timeA); //delay between on and off
  digitalWrite (1, LOW);
  digitalWrite (2, LOW); 
  digitalWrite (3, LOW);
  digitalWrite (11, LOW);
  }
  
  //H & 8
  {
  digitalWrite (1, HIGH);  
  digitalWrite (3, HIGH);
  digitalWrite (11, HIGH); 
  delay(timeA); //delay between on and off
  digitalWrite (1, LOW); 
  digitalWrite (3, LOW);
  digitalWrite (11, LOW);
  }
  
  //I & 9
  {
  digitalWrite (2, HIGH); 
  digitalWrite (3, HIGH); 
  delay(timeA); //delay between on and off
  digitalWrite (2, LOW);
  digitalWrite (3, LOW); 
  }
  
  //J & 0 (zero)
  {
  digitalWrite (2, HIGH); 
  digitalWrite (3, HIGH);
  digitalWrite (11, HIGH); 
  delay(timeA); //delay between on and off
  digitalWrite (2, LOW); 
  digitalWrite (3, LOW);
  digitalWrite (11, LOW);
  }
  
  //K
  {
  digitalWrite (1, HIGH); 
  digitalWrite (12, HIGH); 
  delay(timeA); //delay between on and off
  digitalWrite (1, LOW);
  digitalWrite (12, LOW);
  }
  
  //L
  {
  digitalWrite (1, HIGH);  
  digitalWrite (3, HIGH);
  digitalWrite (12, HIGH); 
  delay(timeA); //delay between on and off
  digitalWrite (1, LOW); 
  digitalWrite (3, LOW);
  digitalWrite (12, LOW);
  }
  
  //M
  {
  digitalWrite (1, HIGH);  
  digitalWrite (2, HIGH);
  digitalWrite (12, HIGH); 
  delay(timeA); //delay between on and off
  digitalWrite (1, LOW); 
  digitalWrite (2, LOW);
  digitalWrite (12, LOW);
  }
  
  //N
  {
  digitalWrite (1, HIGH); 
  digitalWrite (2, HIGH); 
  digitalWrite (11, HIGH);
  digitalWrite (12, HIGH); 
  delay(timeA); //delay between on and off
  digitalWrite (1, LOW);
  digitalWrite (2, LOW); 
  digitalWrite (11, LOW);
  digitalWrite (12, LOW);
  }
  
  //O
  {
  digitalWrite (1, HIGH);  
  digitalWrite (11, HIGH);
  digitalWrite (12, HIGH); 
  delay(timeA); //delay between on and off
  digitalWrite (1, LOW); 
  digitalWrite (11, LOW);
  digitalWrite (12, LOW);
  }
  
  //P
  {
  digitalWrite (1, HIGH); 
  digitalWrite (2, HIGH); 
  digitalWrite (3, HIGH);
  digitalWrite (12, HIGH); 
  delay(timeA); //delay between on and off
  digitalWrite (1, LOW);
  digitalWrite (2, LOW); 
  digitalWrite (3, LOW);
  digitalWrite (12, LOW);
  }
  
  //Q
  {
  digitalWrite (1, HIGH); 
  digitalWrite (2, HIGH); 
  digitalWrite (3, HIGH);
  digitalWrite (11, HIGH);
  digitalWrite (12, HIGH); 
  delay(timeA); //delay between on and off
  digitalWrite (1, LOW);
  digitalWrite (2, LOW);
  digitalWrite (3, LOW);  
  digitalWrite (11, LOW);
  digitalWrite (12, LOW);
  }
  
  //R
  {
  digitalWrite (1, HIGH); 
  digitalWrite (3, HIGH); 
  digitalWrite (11, HIGH);
  digitalWrite (12, HIGH); 
  delay(timeA); //delay between on and off
  digitalWrite (1, LOW);
  digitalWrite (3, LOW); 
  digitalWrite (11, LOW);
  digitalWrite (12, LOW);
  }
  
  //S
  {
  digitalWrite (2, HIGH);  
  digitalWrite (3, HIGH);
  digitalWrite (12, HIGH); 
  delay(timeA); //delay between on and off
  digitalWrite (2, LOW); 
  digitalWrite (3, LOW);
  digitalWrite (12, LOW);
  }
  
  //T
  {
  digitalWrite (2, HIGH); 
  digitalWrite (3, HIGH); 
  digitalWrite (11, HIGH);
  digitalWrite (12, HIGH); 
  delay(timeA); //delay between on and off
  digitalWrite (2, LOW);
  digitalWrite (3, LOW); 
  digitalWrite (11, LOW);
  digitalWrite (12, LOW);
  }
  
  //U
  {
  digitalWrite (1, HIGH); 
  digitalWrite (12, HIGH); 
  digitalWrite (13, HIGH);
  delay(timeA); //delay between on and off
  digitalWrite (1, LOW);
  digitalWrite (12, LOW);
  digitalWrite (13, LOW);
  }
  
  //V
  {
  digitalWrite (1, HIGH); 
  digitalWrite (3, HIGH); 
  digitalWrite (12, HIGH);
  digitalWrite (13, HIGH); 
  delay(timeA); //delay between on and off
  digitalWrite (1, LOW);
  digitalWrite (3, LOW); 
  digitalWrite (12, LOW);
  digitalWrite (13, LOW);
  }
  
  //W
  {
  digitalWrite (2, HIGH); 
  digitalWrite (3, HIGH); 
  digitalWrite (11, HIGH);
  digitalWrite (13, HIGH); 
  delay(timeA); //delay between on and off
  digitalWrite (2, LOW);
  digitalWrite (3, LOW); 
  digitalWrite (11, LOW);
  digitalWrite (13, LOW);
  }
  
  //X
  {
  digitalWrite (1, HIGH); 
  digitalWrite (2, HIGH); 
  digitalWrite (12, HIGH);
  digitalWrite (13, HIGH); 
  delay(timeA); //delay between on and off
  digitalWrite (1, LOW);
  digitalWrite (2, LOW); 
  digitalWrite (12, LOW);
  digitalWrite (13, LOW);
  }
  
  //Y
  {
  digitalWrite (1, HIGH); 
  digitalWrite (2, HIGH); 
  digitalWrite (11, HIGH);
  digitalWrite (12, HIGH);
  digitalWrite (13, HIGH); 
  delay(timeA); //delay between on and off
  digitalWrite (1, LOW);
  digitalWrite (2, LOW);
  digitalWrite (11, LOW);  
  digitalWrite (12, LOW);
  digitalWrite (13, LOW);
  }
  
  //Z
  {
  digitalWrite (1, HIGH); 
  digitalWrite (11, HIGH); 
  digitalWrite (12, HIGH);
  digitalWrite (13, HIGH); 
  delay(timeA); //delay between on and off
  digitalWrite (1, LOW);
  digitalWrite (11, LOW); 
  digitalWrite (12, LOW);
  digitalWrite (13, LOW);
  }
  
  
  //.
  {
  digitalWrite (3, HIGH); 
  digitalWrite (11, HIGH);
  digitalWrite (13, HIGH); 
  delay(timeA); //delay between on and off
  digitalWrite (3, LOW); 
  digitalWrite (11, LOW);
  digitalWrite (13, LOW);
  }
  
  //#
  {
  digitalWrite (2, HIGH); 
  digitalWrite (11, HIGH); 
  digitalWrite (12, HIGH);
  digitalWrite (13, HIGH); 
  delay(timeA); //delay between on and off
  digitalWrite (2, LOW);
  digitalWrite (11, LOW); 
  digitalWrite (12, LOW);
  digitalWrite (13, LOW);
  }

 

Any questions, comments or ideas is always welcome

 

Dale Winhold

  • Sign in to reply

Top Comments

  • dixonselvan
    dixonselvan over 6 years ago +2
    Thanks dwinhold for sharing the code. But the code looks quite lengthy which could be reduced if you try to find out the repetitive pattern and pass the values to a function. Something like this, if (distanceL…
  • aspork42
    aspork42 over 6 years ago in reply to dixonselvan +2
    I was thinking you could write a similar routine for each of the letters in the alphabet. They could be stored as an array and passed to a function which does all the digital Writes. Something like below…
  • aspork42
    aspork42 over 6 years ago in reply to dixonselvan

    I was thinking you could write a similar routine for each of the letters in the alphabet. They could be stored as an array and passed to a function which does all the digital Writes.

     

    Something like below (This compiles without error but I haven't run it on any hardware):

     

    void setup() {
      pinMode(1, OUTPUT);  
      pinMode(2, OUTPUT);    
      pinMode(3, OUTPUT);    
      pinMode(11, OUTPUT);    
      pinMode(12, OUTPUT);    
      pinMode(13, OUTPUT);  
    }
    
    
    //Create a constant variable for each Braile character
    const bool _Char_A[6] = {1,0,0,0,0,0};
    const bool _Char_B[6] = {1,1,0,0,0,0};
    const bool _Char_C[6] = {1,0,0,1,0,0};
    
    
    //remember which output pins we will be using in our For loop
    const int _OutputPins[6] = { 1,2,3,11,12,13}; 
    
    
    void loop() {
      WriteLetter(_Char_A);
      delay(1000);
      WriteLetter(_Char_B);
      delay(1000);
      while(1);
    }
    
    
    void WriteLetter(bool letter[])
    {
      // turn pins ON
      for(int n =0; n < 6; n++)
      {
        digitalWrite(_OutputPins[n], letter[n]);
      }
      //wait 
      delay(500);
    
    
      //turn pins OFF
      for(int n =0; n < 6; n++)
      {
        digitalWrite(_OutputPins[n], LOW);
      }  
    }

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • dixonselvan
    dixonselvan over 6 years ago

    Thanks dwinhold  for sharing the code. But the code looks quite lengthy which could be reduced if you try to find out the repetitive pattern and pass the values to a function.

     

    Something like this,

    1. if (distanceL <= 600 && distanceL > 400) 
    2.     { 
    3. //Add braille code here with warning
    4. //turn right 
    5.         digitalWrite(MotorR1, LOW);   
    6.         digitalWrite(MotorR2, HIGH);   
    7.         digitalWrite(MotorL1, HIGH);   
    8.         digitalWrite(MotorL2, LOW);   
    9.         delay(500); 
    10.         digitalWrite(MotorR1, LOW);   
    11.         digitalWrite(MotorR2, LOW);   
    12.         digitalWrite(MotorL1, LOW);   
    13.         digitalWrite(MotorL2, LOW);

     

    Could be changed to,

     

    If (condition)

    {

      motorWrite (LOW, HIGH, HIGH, LOW);

      delay(500);

      motorWrite (LOW, LOW, LOW, LOW);

    }

     

    void motorWrite (val1, val2, val3, val4)

    {

       digitalWrite(MotorR1, val1);

       digitalWrite(MotorR2, val2);

       digitalWrite(MotorL1, val3);

       digitalWrite(MotorL2, val4);

    }

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • 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