NewPing with the Video Experimenter

Store Forums Video Experimenter Bugs/Problems NewPing with the Video Experimenter

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #8178
    Jhahn6
    Participant

    Hello everyone,

    As I have recently gotten the video experimenter working with the sample code, I have decided to move forward and begin implementing it for a project. I believe I have inserted in the code required, however no display is being shown over the video signal. Below is the code I have created:

    #include <NewPing.h>
    #include <TVout.h>
    #include <fontALL.h>
    #define W 136
    #define H 96
    
    #define MAX_DISTANCE 200
    
    #define TRIGGER_PIN1  11
    #define ECHO_PIN1     10
    
    #define TRIGGER_PIN2  13
    #define ECHO_PIN2     12
    
    #define TRIGGER_PIN3  6
    #define ECHO_PIN3     5
    
    #define TRIGGER_PIN4  4
    #define ECHO_PIN4     3
    
    NewPing sonar1(TRIGGER_PIN1, ECHO_PIN1, MAX_DISTANCE);
    NewPing sonar2(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE);
    NewPing sonar3(TRIGGER_PIN3, ECHO_PIN3, MAX_DISTANCE);
    NewPing sonar4(TRIGGER_PIN4, ECHO_PIN4, MAX_DISTANCE);
    
    TVout tv;
    
    void setup() {
      Serial.begin(115200);
      tv.begin(NTSC, W, H);
      initOverlay();
      tv.select_font(font6x8);
      tv.fill(0);
      randomSeed(analogRead(0));
    }
    // Initialize ATMega registers for video overlay capability.
    // Must be called after tv.begin().
    void initOverlay() {
      TCCR1A = 0;
      // Enable timer1.  ICES0 is set to 0 for falling edge detection on input capture pin.
      TCCR1B = _BV(CS10);
    
      // Enable input capture interrupt
      TIMSK1 |= _BV(ICIE1);
    
      // Enable external interrupt INT0 on pin 2 with falling edge.
      EIMSK = _BV(INT0);
      EICRA = _BV(ISC11);
    }
    
    // Required to reset the scan line when the vertical sync occurs
    ISR(INT0_vect) {
      display.scanLine = 0;
    }
    
    void loop() {
    
      delay(50);
      int uS1 = sonar1.ping();
      Serial.print("Ping 1: ");
      int measure1 = uS1 / US_ROUNDTRIP_IN;
      if(measure1 > 0){
      tv.print(measure1);
      Serial.print(measure1);
      tv.print("in");
      Serial.print("in");
      }
      else{
        tv.print("    ");
      }
      int uS2 = sonar2.ping();
      Serial.print("Ping 2: ");
      int measure2 = uS2 / US_ROUNDTRIP_IN;
      if(measure2 > 0){
      tv.print("           ");
      tv.print(measure2);
      Serial.print(measure2);
      tv.print("in\n\n\n\n\n\n\n\n\n\n\n");
      Serial.print("in");
      }
      int uS3 = sonar3.ping();
      Serial.print("Ping 3: ");
      int measure3 = uS3 / US_ROUNDTRIP_IN;
      if(measure3 > 0){
      tv.print(measure3);
      Serial.print(measure3);
      tv.print("in");
      Serial.print("in");
      }
      else{
        tv.print("    ");
      }
      int uS4 = sonar4.ping();
      Serial.print("Ping 4: ");
      int measure4 = uS4 / US_ROUNDTRIP_IN;
      if(measure4 > 0){
      tv.print("           ");
      tv.print(measure4);
      Serial.print(measure4);
      tv.print("in");
      Serial.print("in");
      }
      tv.delay(500);
      tv.clear_screen();
      tv.delay_frame(5);
    }
    

    If anyone could point out the mistake(s) I have made, I would greatly appreciate it.

    Thanks!

    #8179
    Jhahn6
    Participant

    After some research I have found that Serial does not cooperate with VE. I have made a simpler code to implement one ping ultrasonic sensor:

    
    #include <TVout.h>
    #include <NewPing.h>
    #include <fontALL.h>
    #define W 136
    #define H 96
    TVout tv;
    
    #define MAX_DISTANCE 200
    
    #define TRIGGER_PIN1  11
    #define ECHO_PIN1     10
    
    NewPing sonar1(TRIGGER_PIN1, ECHO_PIN1, MAX_DISTANCE);
    
    void setup() {
      tv.begin(NTSC, W, H);
      initOverlay();
      tv.select_font(font6x8);
      tv.fill(0);
      randomSeed(analogRead(0));
    }
    
    // Initialize ATMega registers for video overlay capability.
    // Must be called after tv.begin().
    void initOverlay() {
      TCCR1A = 0;
      // Enable timer1.  ICES0 is set to 0 for falling edge detection on input capture pin.
      TCCR1B = _BV(CS10);
    
      // Enable input capture interrupt
      TIMSK1 |= _BV(ICIE1);
    
      // Enable external interrupt INT0 on pin 2 with falling edge.
      EIMSK = _BV(INT0);
      EICRA = _BV(ISC11);
    }
    
    // Required to reset the scan line when the vertical sync occurs
    ISR(INT0_vect) {
      display.scanLine = 0;
    }
    
    void loop() {
      int uS1 = sonar1.ping();
      int measure1 = uS1 / US_ROUNDTRIP_IN;
      tv.print(50,50,measure1);
      tv.print("in");
      tv.delay_frame(5);
    
    }
    

    My new question is, does using four ultrasonic sensors put too much load on the Arduino, and am I able to play with delays to increase the accuracy of the readings they provide?

    #8181
    Michael
    Keymaster

    Correct, you cannot use interrupt-based Serial communication with the TVout library because to generate the video signal, the library must run an interrupt service routine extremely often and with precise timing. That is why the TVout library comes with a polling version of serial communication. Look at the “pollserial” example in the library. That is how to do serial communication.

    When generating video, the CPU is very busy. Using an ultrasonic sensor requires precise timing of the reflected ping, but you won’t be able to measure accurately if the video interrupt is occuring during your measurement. The video code will constantly be interrupting the ping code that is trying to do a measurement. You can try disabling the video while you perform your pings.

    Bottom line: ATmega328 is a very cheap and not powerful microcontroller. You can do some pretty amazing things with it, like generate video and audio, but you won’t be able to do other things at the same time that require precise timing.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.