Question for a Project

Hi, I am having trouble writing a code for a project I am doing. The basic language of the code needs to be able to send commands through the serial monitor in order to turn on/off digital pins. In addition, I was told that I need to use "Serial.read." Is any able to help me build this code? Attached below is my first code, but this was not correct.

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

What was not correct?

You have posted an image of code instead of posting code inline using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The "Code: [Select]" feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower right corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

No No No No !

There is forum etiquette you need to follow.


Show us a good schematic of your circuit.
Show us a good image of your wiring.
Give links to components.
Posting images:
https://forum.arduino.cc/index.php?topic=519037.0

Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu.
[code]Paste your sketch here[/code]

If you want to use serial, you can't use pin 0. It's used by serial.

Ok sorry I will put up the code with code tags and will draw out a schematic.

aarg:
If you want to use serial, you can't use pin 0. It's used by serial.

Alright I uploaded a circuit picture and also fixed the code link I think? So as far as the code how do I make it send commands through the serial monitor to turn on and off pins. My code right now just works as a way to have the LED turn on for a second and then off for a second.

Answers to most of our serial questions can be found in one or other example of Robin2's serial tutorial.

The code below is a simple enhancement to Example1 therein, and uses Y to turn the builtin led on, and N to turn it off:

// BASED ON Example 1 - Receiving single characters
// from https://forum.arduino.cc/index.php?topic=396450
// this enhancement uses Y/N to on/off builtin led

char receivedChar;
boolean newData = false;

void setup() 
{
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.println("Arduino is ready: enter Y for on or N for off");
}//setup

void loop() 
{
  recvOneChar();
  showNewData();
}//loop

void recvOneChar() 
{
  if (Serial.available() > 0) 
  {
    receivedChar = Serial.read();
    newData = true;
  }
}//recvOneChar

void showNewData() 
{
  if (newData == true) 
  {
    Serial.print("This just in ... ");
    Serial.println(receivedChar);
    if (receivedChar == 'Y' || receivedChar == 'y') digitalWrite(LED_BUILTIN, HIGH);
    else if (receivedChar == 'N' || receivedChar == 'n') digitalWrite(LED_BUILTIN, LOW);
    else Serial.println("ignoring....");
    newData = false;
  }
}//showNewData

This is exactly what you are looking for: turn on/off LED via Serial Monitor

IoT_hobbyist:
This is exactly what you are looking for

Nope....

String

... may be trivial in that example, but a really bad thing to suggest in general.

"... may be trivial in that example, but a really bad thing to suggest in general."

So post your c-string code. :wink:

zoomkat:
So post your c-string code. :wink:

I had already pointed to Robin2's tutorial, where what OP wants to do can be done in a char ala example 1.