won"t compile

:frowning: ok: using the "starter kit" by DFRobot. Using their code (project 13). I copy line by line into my sketch. I compile after each line I write. When I get to a specific line, the code won"t compile. I cannot find //motor with button control

int buttonPin = 2;
int relayPin = 3;
int relayState = HIGH;
int buttonState;
int lastButtonState;
long lastDebounceTime = 0;
long debounceDelay = 50;

void setup() {
pinMode(buttonPin, INPUT);
pinMode(relayPin,OUTPUT);
digitalWrite(relayPin, relayState);
int reading = digitalRead(buttonPin);
if (reading != lastButtonState){
lastDebounceTime = millis();
if ((millis() - lastDebounceTime) > debounceDelay){
if (reading != buttonState){
buttonState = reading;
}
}

}

// put your main code here, to run repeatedly:

} wrong. Can someone please help me so I can sleep at night. Thanks

the problem line is: if ((millis() - lastDebounceTime) > debounceDelay){

It’s not going to compile without a loop(). But there are no compilation errors with that if statement.

Try posting the code properly in </> code tags and if you’re getting an error please also post the COMPLETE error message.

Steve

Thanks for the reply. I normally use comments when I code but I have been re-entering this sketch too many times , trying to figure out what is happening. This was my first attempt to cut and paste. It will get better.
The error code is "error 1" I have no idea what that means. Is there a listing of error codes?

Hopefully, there's more than just "error 1".

Try scrolling back

I compile after each line I write.

Why ? Your program will not compile until there is at least a setup() and loop() function.

//motor with button control

int buttonPin = 2;
int relayPin = 3;
int relayState = HIGH;
int buttonState;
int lastButtonState;
long lastDebounceTime = 0;
long debounceDelay = 50;

void setup() {
pinMode(buttonPin, INPUT);
pinMode(relayPin,OUTPUT);
digitalWrite(relayPin, relayState);
int reading = digitalRead(buttonPin);
if (reading != lastButtonState){
lastDebounceTime = millis();
if ((millis() - lastDebounceTime) > debounceDelay){
if (reading != buttonState){
buttonState = reading;
}
}

}

the DFRobot project is #13: Motor Fan

I made a mistake (again) my code will not "verify" past : if((millis() - lastDebounceTime) > deBounceDelay){

Post the actual complete error message. I'm guessing it says something about main() and an undefined reference to loop().

Which is because you insist on ignoring us when we tell you that you NEED a loop() if you want it to compile.

Steve

you NEED a loop() if you want it to compile.

and you also need the setup() function to end with a }

Hi,

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

When you open a NEW code page in the IDE, you will see the minimum requirements to make your code.
please compare it with your code.

As has been repeated many time you are missing

void loop()
{
}

at the end of your code.

Tom... :slight_smile:

You're right, he's missed that out again. Trying to compile incomplete code a line at a time really is the most pointless waste of time.

Well it's not going to waste any more of my time.

Steve

Hi,
If "the DFRobot project is #13: Motor Fan" has code to go with it, compare it to yours.

You cannot write a line of code and compile after each line is added.

The compiler does not just check your lines of code but the overall STRUCTURE of your code, so you have to also include the bare necessities of the program structure.

void loop()
{
}

Is one of them.

Do not get fixated on the millis() statement error.

Tom.... :slight_smile:

//Project thirteen - the Arduino to control fan operation
int buttonPin = 2;                  // int buttonPin = 2;
int relayPin = 3;                   // int relayPin = 3;
int relayState = HIGH;              // int relayState = HIGH;
int buttonState;                    // record the current button state
int lastButtonState = LOW;          // record the last button state
long lastDebounceTime = 0;
long debounceDelay = 50;            // eliminate debounce time

void setup() {
pinMode(buttonPin, INPUT);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, relayState);  // configure the initial state of relay
}

void loop() {
int reading = digitalRead(buttonPin);             //read the value of button
// once detects change of state, record time
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
// wait for 50ms to evaluate if it is the same state as last state
// if different, change the button state
// if the state of button is high(pressed), change the state of relay
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
relayState = !relayState;
}
}
}
digitalWrite(relayPin, relayState);
//change the last state of button
lastButtonState = reading;

}

Entering all of the code first might solve your problems.
G

I want to thank you all for the help. Sorry for my lack of experience.. YES.. my sketch needed a "void loop()" . My way of thinking was that it was not needed to "verify " lines of code. I am learning, thanks.