[{"data":1,"prerenderedAt":140},["ShallowReactive",2],{"example-page-linefollower":3},{"id":4,"info":5,"parts":6,"wired":19,"refs":20,"nets":21,"moreNets":134,"code":135},"linefollower","The two-wheel robot with two TCRT5000 line sensors under its front edge, about 5 mm above the floor, one left and one right of the 19 mm tape. Over the white mat much infrared comes back and a sensor's DO is low; over the black tape little comes back and DO goes high. When the tape slips under the left sensor, the ESP32 slows the left wheel and the robot turns after the line, the same on the right. The sensors follow their data sheet (signal over the distance) and see the floor where the physics puts them. In the World, move or bend the tape while it runs, change SPEED in the sketch until it loses the line in the curves, or raise the sensors with their standoff.",[7,10,12,15,17],[8,9],"gearmotor",2,[11,9],"tcrt5000",[13,14],"battery",1,[16,14],"esp32_devkitc",[18,14],"l298n_module",false,{"A1":16,"BT1":13,"U3":18,"U4":11,"U5":11,"ML":8,"MR":8},[22,37,44,52,59,65,71,77,83,89,95,102,108,115,121,128],{"name":23,"pins":24},"0",[25,28,31,33,35],[26,27],"A1","GND",[29,30],"BT1","−",[32,27],"U3",[34,27],"U4",[36,27],"U5",{"name":38,"pins":39},"V5",[40,42],[26,41],"5V",[32,43],"+5V",{"name":45,"pins":46},"V33",[47,49,51],[26,48],"3V3",[34,50],"VCC",[36,50],{"name":53,"pins":54},"VBAT",[55,57],[29,56],"+",[32,58],"+12V",{"name":60,"pins":61},"ENA",[62,64],[26,63],"GPIO25",[32,60],{"name":66,"pins":67},"ENB",[68,70],[26,69],"GPIO14",[32,66],{"name":72,"pins":73},"IN1",[74,76],[26,75],"GPIO26",[32,72],{"name":78,"pins":79},"IN2",[80,82],[26,81],"GPIO27",[32,78],{"name":84,"pins":85},"IN3",[86,88],[26,87],"GPIO32",[32,84],{"name":90,"pins":91},"IN4",[92,94],[26,93],"GPIO33",[32,90],{"name":96,"pins":97},"LINE_L",[98,100],[26,99],"GPIO18",[34,101],"DO",{"name":103,"pins":104},"LINE_R",[105,107],[26,106],"GPIO19",[36,101],{"name":109,"pins":110},"ML_A",[111,113],[112,56],"ML",[32,114],"OUT1",{"name":116,"pins":117},"ML_B",[118,119],[112,30],[32,120],"OUT2",{"name":122,"pins":123},"MR_A",[124,126],[125,30],"MR",[32,127],"OUT3",{"name":129,"pins":130},"MR_B",[131,132],[125,56],[32,133],"OUT4",0,{"file":136,"lang":137,"text":138,"lines":139,"more":19},"sketch.ino","cpp","\u002F\u002F Line follower: two TCRT5000 sensors under the front of the robot look at the floor, one left and\n\u002F\u002F one right of the black tape. Over the white mat a sensor's DO is LOW; over the tape it goes HIGH.\n\u002F\u002F When the tape slips under the left sensor, the line bends left: the left wheel slows down (or even\n\u002F\u002F turns back a little) and the robot turns after it. The same on the right.\n\u002F\u002F\n\u002F\u002F Try it: change SPEED (faster: it starts to lose the line in the curves) or INNER (how hard it\n\u002F\u002F corrects), then press Simulate again. Move the tape in the World while it runs.\n\n\u002F\u002F pins\nconstexpr int ENA = 25, IN1 = 26, IN2 = 27;     \u002F\u002F left motor\nconstexpr int ENB = 14, IN3 = 32, IN4 = 33;     \u002F\u002F right motor\nconstexpr int SENSE_L = 18, SENSE_R = 19;       \u002F\u002F line sensors' DO: HIGH over the black tape\n\nconstexpr float SPEED = 0.35;                   \u002F\u002F straight on: share of full PWM\nconstexpr float INNER = -0.15;                  \u002F\u002F the inner wheel while it corrects (negative: backwards)\nconstexpr int RAMP_MS = 800;                    \u002F\u002F soft start\nconstexpr int PWM_BITS = 10, PWM_MAX = (1 \u003C\u003C PWM_BITS) - 1;\n\n\u002F\u002F a signed command -1..1 for one motor: direction pins and PWM duty\nvoid motor(int en, int a, int b, float u) {\n  u = constrain(u, -1.0f, 1.0f);\n  digitalWrite(a, u > 0 ? HIGH : LOW);\n  digitalWrite(b, u > 0 ? LOW : HIGH);\n  ledcWrite(en, (int)(fabsf(u) * PWM_MAX));\n}\nvoid drive(float left, float right) { motor(ENA, IN1, IN2, left); motor(ENB, IN3, IN4, right); }\n\nvoid setup() {\n  Serial.begin(115200);\n  pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);\n  pinMode(SENSE_L, INPUT); pinMode(SENSE_R, INPUT);\n  ledcAttach(ENA, 1000, PWM_BITS);\n  ledcAttach(ENB, 1000, PWM_BITS);\n  Serial.println(\"line follower ready\");\n  \u002F\u002F start softly, straight on (the tape runs between the two sensors)\n  for (int k = 1; k \u003C= RAMP_MS \u002F 10; k++) { float u = SPEED * k \u002F (RAMP_MS \u002F 10); drive(u, u); delay(10); }\n}\n\nint last = -1;\n\nvoid loop() {\n  bool l = digitalRead(SENSE_L) == HIGH, r = digitalRead(SENSE_R) == HIGH;\n  int state = l && !r ? 1 : r && !l ? 2 : 0;\n  if (state == 1) drive(INNER, SPEED);          \u002F\u002F line under the left sensor: turn left\n  else if (state == 2) drive(SPEED, INNER);     \u002F\u002F under the right one: turn right\n  else drive(SPEED, SPEED);                     \u002F\u002F between them (or both on it, a crossing): straight on\n  if (state != last) {\n    Serial.println(state == 1 ? \"line left: turn left\" : state == 2 ? \"line right: turn right\" : \"straight on\");\n    last = state;\n  }\n  delay(2);\n}",52,1796304400776]