Hello there.
I have been working on this project about pull UP/Down resistors with strings and now that I am done coding this message seems to show up. Could you please help me?
void setup() {
// put your setup code here, to run once
pinMode (13,OUTPUT); // This line makes the Built IN LED the Output
pinMode (3,INPUT); // This line makes the External Pull up/Down resistor and button the Input
String myWelcomeString ("Hello there");
String myStartString ("You might be wondering, why did a robot just say hello to me? is it becuase I have sentience?");
String myEndString ("Skelly will return with more Wild facts, till then.");
String myString3 = "myWelcomeString + myStartString + myEndString"; // This line says that the serial monitor should print all of these strings at the same time
}
void loop() {
// put your main code here, to run repeatedly:
Serial.begin(9600);
Serial.print(myString3);
if(digitalRead(3) == 0); // This line says that the Arduino Board should check if the input pin is 0
{
while(digitalRead(3) == 0); //Check if the button is still pressed, if not then blink
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
}
}
All variables that you declare inside a function (setup() in this case) are not known inside other funcions, e.g. in loop().
You will have to make them global, that is, declare them outside setup() if you want them to be known in other functions.
String myWelcomeString ("Hello there");
String myStartString ("You might be wondering, why did a robot just say hello to me? is it becuase I have sentience?");
String myEndString ("Skelly will return with more Wild facts, till then.");
String myString3 = "myWelcomeString + myStartString + myEndString"; // This line says that the serial monitor should print all of these strings at the same time
void setup() {
// put your setup code here, to run once
pinMode (13,OUTPUT); // This line makes the Built IN LED the Output
pinMode (3,INPUT); // This line makes the External Pull up/Down resistor and button the Input
}
vouid loop()
{
...
...
}
Also note that String myString3 = "myWelcomeString + myStartString + myEndString"; might not be doing what you expect.
there is no need to call Serial.begin in loop. Do it once in setup.
Begin to structure your code into functions.
For example, if you want to print an message create a function for it. Based on the content of the message I thought it should be printed only once. But you can call this message again in loop if needed. If you need something two (or more) times, put it into a function or use control structures and let the microcontroller do things several time.
Furthermore read about the usage of the F-Makro
You can combine several lines already for the serial print.
Your code doesn't need Arduino Strings at all - avoid them in future especially if you need to concatenate them read more about the proper usage of Arduino Strings (e.g. what the reserve keyword is doing). But in general - try to write code without String.
I've added some linefeeds (\n) at the end of each line.
Use constant expressions for your pins. Don't write GPIOs hardcoded several times in your code. If you need to change the GPIO you have it exactly once in your code.
// https://forum.arduino.cc/t/mystring3-was-not-declared-in-this-scope/1026054/8
// 2022-08-27
constexpr byte ledPin = 13;
constexpr byte buttonPin = 3;
void intro()
{
Serial.println(F("Hello there\n"
"You might be wondering, why did a robot just say hello to me? is it because I have sentience?\n"
"Skelly will return with more Wild facts, till then."));
}
void setup() {
Serial.begin(9600); //
// put your setup code here, to run once
pinMode (ledPin, OUTPUT); // This line makes the Built IN LED the Output
pinMode (buttonPin, INPUT); // This line makes the External Pull up/Down resistor and button the Input
intro();
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(buttonPin) == LOW) // no ; at the end of the line!!!
{
//while (digitalRead(buttonPin) == LOW); // I guess this is not doing what you think it should do ...
for (int i = 0; i < 2; i++) // blink at least two times
{
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
}
}
see also
[wokwi link removed as the TO hasn't reacted any more]
furthermore you could use the internal INPUT_PULLUP instead of an external resistor.