When you click on blinkSwitch, one of two commands is sent to Arduino: BLINK_ON or BLINK_OFF, depending on the status of the switch. The fundamental part, the Delphi side, resides in the following function (this allows you to send the command):
procedure TMainForm.blinkSwitchClick(Sender: TObject);
begin
case blinkSwitch.State of
tssOff:
ComPort1.WriteStr('BLINK_OFF');
tssOn:
ComPort1.WriteStr('BLINK_ON');
end;
end;
The preceding code is used for the WriteStr function of TComPort to send the command to Arduino.
Regarding the Arduino Sketch (point 9 of How to do it...), it is appropriate to say a few words:
- In the setup function, the built-in LED is defined as an output pin and the serial port is initialized
- In the main loop, all the logic takes place:
- Reading from the serial is performed (readFromSerial()).
- If the BLINK_ON command is read from the serial, then the Boolean variable blinkMode is set to true. Unless the BLINK_OFF command is sent, the blinking described in the doBlink() function is performed for each loop.
- If the BLINK_OFF command is read from the serial, then the Boolean variable blinkMode is set to false and no operation on the built-in LED is performed.