13 Replies Latest reply on Aug 18, 2017 2:18 PM by karl.lange

    Ultasonic and line sensor

    karl.lange

      Hi all,

         

      I am new to the PSoC platform and am having trouble implementing an ultrasonic and line sensor for a robot.

         

      The arduino code for each is listed below, any help on how i could implement this code with PSoC would be greatly appreciated.

         

      Ultrasonic

         
      /* Tested with HY-SRF05, HC-SR04 The circuit:  * VVC connection of the sensor attached to +5V  * GND connection of the sensor attached to ground  * TRIG connection of the sensor attached to digital pin 12         * ECHO connection of the sensor attached to digital pin 13 */ const int TRIG_PIN = 12;const int ECHO_PIN = 13; void setup() {  // initialize serial communication:  Serial.begin(9600);    pinMode(TRIG_PIN,OUTPUT);  pinMode(ECHO_PIN,INPUT);} void loop(){   long duration, distanceCm, distanceIn;    // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:  digitalWrite(TRIG_PIN, LOW);  delayMicroseconds(2);  digitalWrite(TRIG_PIN, HIGH);  delayMicroseconds(10);  digitalWrite(TRIG_PIN, LOW);  duration = pulseIn(ECHO_PIN,HIGH);    // convert the time into a distance  distanceCm = duration / 29.1 / 2 ;  distanceIn = duration / 74 / 2;    if (distanceCm <= 0){    Serial.println("Out of range");  }  else {    Serial.print(distanceIn);    Serial.print("in, ");    Serial.print(distanceCm);    Serial.print("cm");    Serial.println();  }  delay(1000);}
         

       

         

      Line Sensor

         

          

         

                    

         
           //Code for the QRE1113 Digital board   
         
           //Outputs via the serial terminal - Lower numbers mean more reflected   
         
           //3000 or more means nothing was reflected.   
         
          
         
         
              
         
              
         
              
         
              
         

       

         
              
               
                  
               
                  
               
             int QRE1113_Pin = 2; //connected to digital 2     
             

       

         
              
         
           void setup(){   
         
             Serial.begin(9600);   
         
           }   
         
              
         
           void loop(){   
         
             int QRE_Value = readQD();   
         
             Serial.println(QRE_Value);    
         
           }   
         
              
         
           int readQD(){
         
         
              
         
              
         
              
         
              
         

       

         
              
               
                  
               
                  
               
               //Returns value from the QRE1113      
             

       

         
              
         
              
         
              
         
              
         
              
         

       

         
              
               
                  
               
                  
               
               //Lower numbers mean more refleacive     
             

       

         
              
         
              
         
              
         
              
         
              
         

       

         
              
               
                  
               
                  
               
               //More than 3000 means nothing was reflected.     
             

       

         
             pinMode( QRE1113_Pin, OUTPUT );
         
         
              
         
              
         
              
         
             digitalWrite( QRE1113_Pin, HIGH );     
         
              
         
              
         
              
         
              
         
              
         
              
         
              
         
              
         
             delayMicroseconds(10);   
         
             pinMode( QRE1113_Pin, INPUT );   
         
              
         
             long time = micros();   
         
          
         
         
              
         
              
         
              
         
              
         

       

         
              
               
                  
               
                  
               
               //time how long the input is HIGH, but quit after 3ms as nothing happens after that     
             

       

         
             while (digitalRead(QRE1113_Pin) == HIGH && micros() - time < 3000);    
         
             int diff = micros() - time;   
         
              
         
             return diff;   
         
           }