[{"data":1,"prerenderedAt":225},["ShallowReactive",2],{"example-page-mega-multiserial":3},{"id":4,"info":5,"parts":6,"wired":17,"refs":18,"nets":19,"moreNets":219,"code":220},"mega-multiserial","The Mega 2560 has four hardware serial ports and 15 PWM pins. Twelve LEDs on D2–D13 dim up and down one after the other; potentiometer RV1 on A0 sets the speed. Serial1 sends \"ping n\" on TX1 (D18); a wire takes it to RX2 (D17), Serial2 answers and the USB serial port reports what arrived. Serial3 sends the potentiometer value. The serial monitor shows all four ports (lines of Serial1–3 start with their name) and can send to each of them. Put the logic analyzer on LINK to see the 9600 baud frames.",[7,10,12,15],[8,9],"led",12,[11,9],"resistor",[13,14],"arduino_mega",1,[16,14],"pot",false,{"A1":13,"D1":8,"D2":8,"D3":8,"D4":8,"D5":8,"D6":8,"D7":8,"D8":8,"D9":8,"D10":8,"D11":8,"D12":8,"RV1":16,"R1":11,"R2":11,"R3":11,"R4":11,"R5":11,"R6":11,"R7":11,"R8":11,"R9":11,"R10":11,"R11":11,"R12":11},[20,54,60,67,73,80,85,92,97,104,109,116,121,128,133,140,145,152,157,164,169,176,181,188,193,200,205,212],{"name":21,"pins":22},"0",[23,26,29,31,33,35,37,39,41,43,45,47,49,51],[24,25],"A1","GND",[27,28],"D1","K",[30,28],"D2",[32,28],"D3",[34,28],"D4",[36,28],"D5",[38,28],"D6",[40,28],"D7",[42,28],"D8",[44,28],"D9",[46,28],"D10",[48,28],"D11",[50,28],"D12",[52,53],"RV1","2",{"name":55,"pins":56},"5V",[57,58],[24,55],[52,59],"1",{"name":61,"pins":62},"L1",[63,65],[24,64],"~D2",[66,59],"R1",{"name":68,"pins":69},"L1_A",[70,72],[27,71],"A",[66,53],{"name":74,"pins":75},"L2",[76,78],[24,77],"~D3",[79,59],"R2",{"name":81,"pins":82},"L2_A",[83,84],[30,71],[79,53],{"name":86,"pins":87},"L3",[88,90],[24,89],"~D4",[91,59],"R3",{"name":93,"pins":94},"L3_A",[95,96],[32,71],[91,53],{"name":98,"pins":99},"L4",[100,102],[24,101],"~D5",[103,59],"R4",{"name":105,"pins":106},"L4_A",[107,108],[34,71],[103,53],{"name":110,"pins":111},"L5",[112,114],[24,113],"~D6",[115,59],"R5",{"name":117,"pins":118},"L5_A",[119,120],[36,71],[115,53],{"name":122,"pins":123},"L6",[124,126],[24,125],"~D7",[127,59],"R6",{"name":129,"pins":130},"L6_A",[131,132],[38,71],[127,53],{"name":134,"pins":135},"L7",[136,138],[24,137],"~D8",[139,59],"R7",{"name":141,"pins":142},"L7_A",[143,144],[40,71],[139,53],{"name":146,"pins":147},"L8",[148,150],[24,149],"~D9",[151,59],"R8",{"name":153,"pins":154},"L8_A",[155,156],[42,71],[151,53],{"name":158,"pins":159},"L9",[160,162],[24,161],"~D10",[163,59],"R9",{"name":165,"pins":166},"L9_A",[167,168],[44,71],[163,53],{"name":170,"pins":171},"L10",[172,174],[24,173],"~D11",[175,59],"R10",{"name":177,"pins":178},"L10_A",[179,180],[46,71],[175,53],{"name":182,"pins":183},"L11",[184,186],[24,185],"~D12",[187,59],"R11",{"name":189,"pins":190},"L11_A",[191,192],[48,71],[187,53],{"name":194,"pins":195},"L12",[196,198],[24,197],"~D13",[199,59],"R12",{"name":201,"pins":202},"L12_A",[203,204],[50,71],[199,53],{"name":206,"pins":207},"LINK",[208,210],[24,209],"D18\u002FTX1",[24,211],"D17\u002FRX2",{"name":213,"pins":214},"POT",[215,217],[24,216],"A0",[52,218],"W",0,{"file":221,"lang":222,"text":223,"lines":224,"more":17},"sketch.ino","cpp","\u002F\u002F Arduino Mega 2560: four serial ports and twelve PWM channels\n\u002F\u002F  - D2..D13: twelve LEDs, each one dims up and down a little later than the one before (a wave)\n\u002F\u002F  - the potentiometer on A0 sets the speed of the wave\n\u002F\u002F  - Serial1 (TX1 = D18) sends \"ping n\" every half second; a wire takes it to Serial2 (RX2 = D17)\n\u002F\u002F  - Serial2 answers on its own TX (D16), Serial3 (TX3 = D14) reports the potentiometer\n\u002F\u002F  - Serial (USB) shows what Serial2 received\n\u002F\u002F The serial monitor shows every port; lines of Serial1..3 start with their name.\nconst byte LEDS[12] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };\nunsigned long lastPing = 0, lastPot = 0;\nint n = 0;\nString line;\n\nvoid setup() {\n  Serial.begin(115200);\n  Serial1.begin(9600);\n  Serial2.begin(9600);\n  Serial3.begin(115200);\n  Serial.println(\"Mega 2560: 4 serial ports, 12 PWM channels\");\n}\n\nvoid loop() {\n  unsigned long now = millis();\n  int speed = map(analogRead(A0), 0, 1023, 1, 8);\n  for (int i = 0; i \u003C 12; i++) {\n    int p = (now \u002F 4 * speed + i * 43) % 512;           \u002F\u002F triangle 0..255..0, shifted per LED\n    analogWrite(LEDS[i], p \u003C 256 ? p : 511 - p);\n  }\n  if (now - lastPing >= 500) {\n    lastPing = now;\n    Serial1.print(\"ping \");\n    Serial1.println(n++);\n  }\n  while (Serial2.available()) {\n    char c = Serial2.read();\n    if (c == '\\n') {\n      Serial2.print(\"got: \");\n      Serial2.println(line);\n      Serial.print(\"Serial2 received: \");\n      Serial.println(line);\n      line = \"\";\n    } else if (c != '\\r') line += c;\n  }\n  if (now - lastPot >= 1000) {\n    lastPot = now;\n    Serial3.print(\"A0 = \");\n    Serial3.println(analogRead(A0));\n  }\n}",48,1796304400943]