Python’s Manim Code to Animate Distance using Ultrasonic Sensor and Arduino Uno Microcontroller

from manim import *
from manim import Broadcast as sfn
class AnnularSectorExample(Scene):
    def construct(self):
        #Changes background color to clearly visualize changes in fill_opacity.
        self.camera.background_color = PURE_BLUE
        mc= ("float duration;\n"
         "void setup()\n"\
        "{\n"\
        "pinMode(2, OUTPUT);\n"\
         "pinMode(3, INPUT);\n"\
         "Serial.begin(9600);\n"\
         "}\n"\
        "void loop()\n"\
         "{\n"\
          "digitalWrite(2, HIGH);\n"\
            "delayMicroseconds(10);\n"
           "digitalWrite(2, LOW);\n"  
          "duration=pulseIn(3,HIGH);\n"\
          "distance = duration*0.034/2;\n"\
            "Serial.println(distance);\n"\
           "}\n"  )
        md=	("This statement declares variable duration which stores time travelled by ultrasonic wave.\n"\
            "setup() function is used to configure Pins for INPUT, OUTPUT, signal rate etc.\n"\
        	"Block of setup() function starts here.\n"\
        	"It indicates pin no. 2 is used for OUTPUT.\n"
             "It indicates pin no. 3 is used for INPUT.\n"
             "This statement sets signal rate 9600 Kbps.\n"
            "Block of setup() function ends here.\n"
            "Statements inside loop() function run forever.\n"\
            "Block of loop() function starts here.\n"\
            "digitalWrite(2, HIGH) statement will make  trigger pin no. 2 supply voltage HIGH.\n"\
             "This  statement is for delay of 10 microseconds of ultrsonic waves.\n"
             "digitalWrite(2, LOW) statement will make  trigger pin no. 2 supply voltage LOW\n"
             "PulseIn function checks whether echo is HIGH, and returns propagation delay from sensor  to object.\n"\
             "distance = duration*0.034/2 statements  calculates distance, where 0.034 dm/micros  speed of ultrasonic wave.\n"\
             "Serial.println(distance) prints distance on serial monitor.\n"\
             "End of loop() function\n")
        mrc= Code(code=mc, tab_width=4,style=Code.styles_list[10],language="C",font_size=12,line_spacing=1.25,font="georgia")
        mdesc= Code(code=md, tab_width=4,style=Code.styles_list[10],language="C",font_size=12,line_spacing=1.25,font="georgia")
        self.add(mrc[0])
        self.add(mrc[1])
        self.add(mdesc[0])
        self.add(mdesc[1])
        mdesc.move_to(2.00*RIGHT+0.25*UP)
        mrc.move_to(5.50*LEFT+0.25*UP)
        ################
        self.wait(5)
        self.add(mrc[2][0])
        self.play(Circumscribe(mrc[2][0],buff=0.01,time_width=5),run_time=5)
        self.wait(5)
        self.add(mdesc[2][0])
        ar1=[Arrow(mrc[2][0],mdesc[1][0],color=RED,)]
        self.play(GrowArrow(ar1[0]))
        self.play(Circumscribe(mdesc[2][0],buff=0.01,time_width=5))
        self.remove(*ar1)
        self.wait(5)
        #######################################
        self.add(mrc[2][1])
        self.play(Circumscribe(mrc[2][1],buff=0.01,time_width=5))
        self.wait(5)
        self.add(mdesc[2][1])
        ar2=[Arrow(mrc[2][1],mdesc[1][1],color=RED,)]
        self.play(GrowArrow(ar2[0]))
        self.play(Circumscribe(mdesc[2][1],buff=0.01,time_width=5),run_time=5)
        self.remove(*ar2)
        self.wait(5)
        #######################################
        self.add(mrc[2][2])
        self.play(Circumscribe(mrc[2][2],buff=0.01,time_width=5),run_time=5)
        self.wait(5)
        self.add(mdesc[2][2])
        ar3=[Arrow(mrc[2][2],mdesc[1][2],color=RED)]
        self.play(GrowArrow(ar3[0]))
        self.play(Circumscribe(mdesc[2][2],buff=0.01,time_width=5),run_time=5)
        self.remove(*ar3)
        self.wait(5)
        #######################################
        
        #######################################
        self.add(mrc[2][3])
        self.play(Create(mrc[2][3]),Circumscribe(mrc[2][3],buff=0.01,time_width=5),run_time=5)
        self.wait(5)
        self.add(mdesc[2][3])
        ar4=[Arrow(mrc[2][3],mdesc[1][3],color=RED)]
        self.play(GrowArrow(ar4[0]))
        self.play(Circumscribe(mdesc[2][3],buff=0.01,time_width=5),run_time=5)
        self.wait(5)
        self.remove(*ar4)
        #######################################
        self.add(mrc[2][4])
        self.play(Circumscribe(mrc[2][4],buff=0.01,time_width=5),run_time=5)
        self.wait(5)
        self.add(mdesc[2][4])
        ar5=[Arrow(mrc[2][4],mdesc[1][4],color=RED)]
        self.play(GrowArrow(ar5[0]))
        self.play(Circumscribe(mdesc[2][4],buff=0.01,time_width=5),run_time=5)
        self.wait(5)
        self.remove(*ar5)
        self.wait(2)
        #######################################
        self.add(mrc[2][5])
        self.play(Circumscribe(mrc[2][5],buff=0.01,time_width=15),run_time=5)
        self.wait(5)
        self.add(mdesc[2][5])
        ar6=[Arrow(mrc[2][5],mdesc[1][5],color=RED)]
        self.play(GrowArrow(ar6[0]))
        self.play(Circumscribe(mdesc[2][5],buff=0.01,time_width=15),run_time=5)
        self.wait(5)
        self.remove(*ar6)
        self.wait(5)
        #####################################
        self.add(mrc[2][6])
        self.play(Circumscribe(mrc[2][6],buff=0.01,time_width=15),run_time=5)
        self.wait(5)
        self.add(mdesc[2][6])
        ar7=[Arrow(mrc[2][6],mdesc[1][6],color=RED)]
        self.play(GrowArrow(ar7[0]))
        self.play(Circumscribe(mdesc[2][6],buff=0.01,time_width=15),run_time=5)
        self.wait(5)
        self.remove(*ar7)
        self.wait(5)
        #####################################
        self.add(mrc[2][7])
        self.play(Circumscribe(mrc[2][7],buff=0.01,time_width=15),run_time=5)
        self.wait(5)
        self.add(mdesc[2][7])
        ar8=[Arrow(mrc[2][7],mdesc[1][7],color=RED)]
        self.play(GrowArrow(ar8[0]))
        self.play(Circumscribe(mdesc[2][7],buff=0.01,time_width=15),run_time=5)
        self.wait(5)
        self.remove(*ar8)
        self.wait(5)
        #####################################
        self.add(mrc[2][8])
        self.play(Circumscribe(mrc[2][8],buff=0.01,time_width=15),run_time=5)
        self.wait(5)
        self.add(mdesc[2][8])
        ar9=[Arrow(mrc[2][8],mdesc[1][8],color=RED)]
        self.play(GrowArrow(ar9[0]))
        self.play(Circumscribe(mdesc[2][8],buff=0.01,time_width=15),run_time=5)
        self.wait(5)
        self.remove(*ar9)
        self.wait(5)
        #####################################
        self.add(mrc[2][9])
        self.play(Circumscribe(mrc[2][9],buff=0.01,time_width=15),run_time=5)
        self.wait(5)
        self.add(mdesc[2][9])
        ar10=[Arrow(mrc[2][9],mdesc[1][9],color=RED)]
        self.play(GrowArrow(ar10[0]))
        self.play(Circumscribe(mdesc[2][9],buff=0.01,time_width=15),run_time=5)
        self.wait(5)
        self.remove(*ar10)
        self.wait(5)
        #####################################
        self.add(mrc[2][10])
        self.play(Circumscribe(mrc[2][10],buff=0.01,time_width=15),run_time=5)
        self.wait(5)
        self.add(mdesc[2][10])
        ar11=[Arrow(mrc[2][10],mdesc[1][10],color=RED)]
        self.play(GrowArrow(ar11[0]))
        self.play(Circumscribe(mdesc[2][10],buff=0.01,time_width=15),run_time=5)
        self.wait(5)
        self.remove(*ar11)
        self.wait(5)
        #####################################
        self.add(mrc[2][11])
        self.play(Circumscribe(mrc[2][11],buff=0.01,time_width=15),run_time=5)
        self.wait(5)
        self.add(mdesc[2][11])
        ar12=[Arrow(mrc[2][11],mdesc[1][11],color=RED)]
        self.play(GrowArrow(ar12[0]))
        self.play(Circumscribe(mdesc[2][11],buff=0.01,time_width=15),run_time=5)
        self.wait(5)
        self.remove(*ar12)
        self.wait(5)
        #####################################
        self.add(mrc[2][12])
        self.play(Circumscribe(mrc[2][12],buff=0.01,time_width=15),run_time=5)
        self.wait(5)
        self.add(mdesc[2][12])
        ar13=[Arrow(mrc[2][12],mdesc[1][12],color=RED)]
        self.play(GrowArrow(ar13[0]))
        self.play(Circumscribe(mdesc[2][12],buff=0.01,time_width=15),run_time=5)
        self.wait(5)
        self.remove(*ar13)
        self.wait(5)
        #####################################
        self.add(mrc[2][13])
        self.play(Circumscribe(mrc[2][13],buff=0.01,time_width=15),run_time=5)
        self.wait(5)
        self.add(mdesc[2][13])
        ar14=[Arrow(mrc[2][13],mdesc[1][13],color=RED)]
        self.play(GrowArrow(ar14[0]))
        self.play(Circumscribe(mdesc[2][13],buff=0.01,time_width=15),run_time=5)
        self.wait(5)
        self.remove(*ar14)
        self.wait(5)
        #####################################
        #####################################
        self.add(mrc[2][14])
        self.play(Circumscribe(mrc[2][14],buff=0.01,time_width=15),run_time=5)
        self.wait(5)
        self.add(mdesc[2][14])
        ar15=[Arrow(mrc[2][14],mdesc[1][14],color=RED)]
        self.play(GrowArrow(ar15[0]))
        self.play(Circumscribe(mdesc[2][14],buff=0.01,time_width=15),run_time=5)
        self.wait(5)
        self.remove(*ar15)
        self.wait(5)
         #####################################
        self.add(mrc[2][15])
        self.play(Circumscribe(mrc[2][15],buff=0.01,time_width=15),run_time=5)
        self.wait(5)
        self.add(mdesc[2][15])
        ar16=[Arrow(mrc[2][15],mdesc[1][15],color=RED)]
        self.play(GrowArrow(ar16[0]))
        self.play(Circumscribe(mdesc[2][15],buff=0.01,time_width=15),run_time=5)
        self.wait(5)
        self.remove(*ar16)
        self.wait(5)
        #####################################
        self.play(FadeOut(mdesc))
        im= ImageMobject("ultra.png")
        im.width=3.00
        im.height=3.00
        im.move_to(0.0*LEFT)
        self.add(im)
        #w1= AnnularSector(inner_radius=0.20, outer_radius=0.30,start_angle= 270*DEGREES, angle=180* DEGREES, color=YELLOW)
        #w2 = AnnularSector(inner_radius=0.30, outer_radius=0.40,start_angle= 270*DEGREES, angle=180* DEGREES, color=YELLOW)
        w3= AnnularSector(inner_radius=0.40, outer_radius=0.50,start_angle= 270*DEGREES, angle=180* DEGREES, color=YELLOW)
        w4 = AnnularSector(inner_radius=0.60, outer_radius=0.70,start_angle= 270*DEGREES, angle=180* DEGREES, color=YELLOW)
        w5= AnnularSector(inner_radius=0.70, outer_radius=0.80,start_angle= 270*DEGREES, angle=180* DEGREES, color=YELLOW)
        w6= AnnularSector(inner_radius=0.90, outer_radius=1.00,start_angle= 270*DEGREES, angle=180* DEGREES, color=YELLOW)
        w7= AnnularSector(inner_radius=1.20, outer_radius=1.40,start_angle= 270*DEGREES, angle=180* DEGREES, color=YELLOW)
        w8= AnnularSector(inner_radius=1.40, outer_radius=1.60,start_angle= 270*DEGREES, angle=180* DEGREES, color=YELLOW)
        w9= AnnularSector(inner_radius=1.60, outer_radius=1.80,start_angle= 270*DEGREES, angle=180* DEGREES, color=YELLOW)
        w10= AnnularSector(inner_radius=1.80, outer_radius=2.00,start_angle= 270*DEGREES, angle=180* DEGREES, color=YELLOW)
        ############-------------################
        #w1o = AnnularSector(inner_radius=0.20, outer_radius=0.30,start_angle= 270*DEGREES, angle=-180* DEGREES, color=RED)
        #w2o = AnnularSector(inner_radius=0.30, outer_radius=0.40,start_angle= 270*DEGREES, angle=-180* DEGREES, color=RED)
        w3o = AnnularSector(inner_radius=0.40, outer_radius=0.50,start_angle= 270*DEGREES, angle=-180* DEGREES, color=RED)
        w4o = AnnularSector(inner_radius=0.60, outer_radius=0.70,start_angle= 270*DEGREES, angle=-180* DEGREES, color=RED)
        w5o = AnnularSector(inner_radius=0.70, outer_radius=0.80,start_angle= 270*DEGREES, angle=-180* DEGREES, color=RED)
        w6o = AnnularSector(inner_radius=0.90, outer_radius=1.00,start_angle= 270*DEGREES, angle=-180* DEGREES, color=RED)
        w7o = AnnularSector(inner_radius=1.20, outer_radius=1.40,start_angle= 270*DEGREES, angle=-180* DEGREES, color=RED)
        ############-------------################
        for i in range(10):
               #b1=Broadcast(mobject=w1,focal_point=2*RIGHT+1.0*UP,n_mobs=50,remover=False)
               #b2=Broadcast(mobject=w2,focal_point=2.50*RIGHT+0.90*UP,n_mobs=50,remover=False)
               b3=Broadcast(mobject=w3,focal_point=3.00*RIGHT+0.80*UP,n_mobs=20,remover=False)
               b4=Broadcast(mobject=w4,focal_point=3.50*RIGHT+0.70*UP,n_mobs=20,remover=False)
               b5=Broadcast(mobject=w5,focal_point=4.00*RIGHT+0.60*UP,n_mobs=20,remover=False)
               b6=Broadcast(mobject=w6,focal_point=4.50*RIGHT+0.50*UP,n_mobs=20,remover=False)
               b7=Broadcast(mobject=w7,focal_point=5.00*RIGHT+0.40*UP,n_mobs=20,remover=False)
               b8=Broadcast(mobject=w8,focal_point=5.50*RIGHT+0.30*UP,n_mobs=20,remover=False)
               b9=Broadcast(mobject=w9,focal_point=6.00*RIGHT+0.20*UP,n_mobs=20,remover=False)
               b10=Broadcast(mobject=w10,focal_point=6.50*RIGHT+0.10*UP,n_mobs=20,remover=False)
               self.play(b3,b4,b5,b6,b7,b8,b9,b10,Circumscribe(mrc[2][9],buff=0.01,time_width=1,color=GREEN),run_time=1)
               self.wait(1)
        for i in range(15):
               #b1=Broadcast(mobject=w1,focal_point=2*RIGHT+1.0*UP,n_mobs=50,remover=False)
               #b2=Broadcast(mobject=w2,focal_point=2.50*RIGHT+0.90*UP,n_mobs=50,remover=False)
               b3=Broadcast(mobject=w3,focal_point=3.00*RIGHT+0.80*UP,n_mobs=20,remover=False)
               b4=Broadcast(mobject=w4,focal_point=3.50*RIGHT+0.70*UP,n_mobs=20,remover=False)
               b5=Broadcast(mobject=w5,focal_point=4.00*RIGHT+0.60*UP,n_mobs=20,remover=False)
               b6=Broadcast(mobject=w6,focal_point=4.50*RIGHT+0.50*UP,n_mobs=20,remover=False)
               b7=Broadcast(mobject=w7,focal_point=5.00*RIGHT+0.40*UP,n_mobs=20,remover=False)
               #b1o=Broadcast(mobject=w1o,focal_point=2*RIGHT-1.0*UP,n_mobs=50,remover=False)
               #b2o=Broadcast(mobject=w2o,focal_point=2.50*RIGHT-0.90*UP,n_mobs=50,remover=False)
               b3o=Broadcast(mobject=w3o,focal_point=3.00*RIGHT-0.80*UP,n_mobs=20,remover=False)
               b4o=Broadcast(mobject=w4o,focal_point=3.50*RIGHT-0.70*UP,n_mobs=20,remover=False)
               b5o=Broadcast(mobject=w5o,focal_point=4.00*RIGHT-0.60*UP,n_mobs=20,remover=False)
               b6o=Broadcast(mobject=w6o,focal_point=4.50*RIGHT-0.50*UP,n_mobs=20,remover=False)
               b7o=Broadcast(mobject=w7o,focal_point=5.00*RIGHT-0.40*UP,n_mobs=20,remover=False)
               r=Rectangle(width=0.10, height=4.0,color=RED) 
               r.move_to(5.50*RIGHT)
               r.set_fill(RED)
               self.add(r)
               self.play(Circumscribe(r,time_width=15),b3,b4,b5,b6,b7,b3o,b4o,b5o,b6o,b7o,Circumscribe(mrc[2][9],buff=0.01,time_width=1,color=GREEN),Circumscribe(mrc[2][12],buff=0.01,time_width=1,color=GREEN),run_time=1)
               self.wait(1)
               
                          
               
        
        
        
               
               
               
               
        

©Postnetwork-All rights reserved.