#include <Servo.h>

Please help, I need to include this code, #include <Servo.h> otherwise my program will no compile. line 63 has this code, for (ctr1 = 0; ctr1 <= flashTimes; ctr1 += 1)
I have tried, include<servo.h) but then the program will not accept line 53.
Thanks, Wally.

Please post your full sketch.

If possible, you should always post code directly in the forum thread as text using code tags:

  • Do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code. This will make it easier for you to spot bugs and make it easier for us to read.
  • In the Arduino IDE or Arduino Web Editor, click on the window that contains your sketch code.
  • Press "Ctrl + A". This will select all the text.
  • Press "Ctrl + C". This will copy the selected text to the clipboard.
  • In a forum reply here, click the "Reply" button.
  • click on the reply field.
  • Click the </> button on the forum toolbar. This will add the forum's code tags markup to your reply.
  • Press "Ctrl + V". This will paste the sketch between the code tags.
  • Move the cursor outside of the code tags before you add any additional text to your reply.
  • Repeat the above process if your sketch has multiple tabs.

This will make it easy for anyone to look at it, which will increase the likelihood of you getting help.

If the sketch is longer than the 9000 characters maximum allowed by the forum, then it's OK to add it as an attachment. After clicking the "Reply" button, you will see an "Attachments and other settings" link.

When your code requires a library that's not included with the Arduino IDE please post a link (using the chain links icon on the forum toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manger (Sketch > Include Library > Manage Libraries in the Arduino IDE or Libraries > Library Manager in the Arduino Web Editor) then say so and state the full name of the library.

Please do this:

  • When you encounter an error, you'll see a button on the right side of the orange bar "Copy error messages" in the Arduino IDE (or the icon that looks like two pieces of paper at the top right corner of the black console window in the Arduino Web Editor). Click that button..
  • In a forum reply here, click on the reply field.
  • Click the </> button on the forum toolbar. This will add the forum's code tags markup to your reply.
  • Press "Ctrl + V". This will paste the error between the code tags.
  • Move the cursor outside of the code tags before you add any additional text to your reply.

If the text exceeds the forum's 9000 character limit, save it to a .txt file and post it as an attachment. If you click the "Reply" button here, you will see an "Attachments and other settings" link.

#include <Servo.h>
...
... 
 for (ctr1 = 0; ctr1 <= flashTimes; ctr1 += 1)

Thank for your answer, the circuit is now working.

< is HTML code for < (less than)
> is HTML code for > (greater than)

You probably copied from a crappy website or made a copy mistake (e.g. view source and copy)

Can someone please help me to change the code so that I can change the speed of the servo's.
Thanks, Wally.

// Train crossing ©The Geek Pub, LLC - Mike Murray 2019
// Freely distributable with attribution and link to TheGeekPub.com

#include<Servo.h>

//Let's define easy constants for the components so we can remember them easier
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin1L = 13; // the number of the 1st Indicator LED Left pin
const int ledPin1R = 12; // the number of the 1st Indicator LED Right pin
const int ledPin2L = 11; // the number of the 2nd Indicator LED Left pin
const int ledPin2R = 10; // the number of the 2nd Indicator LED Right pin
const int servo1Pin = 7; // the number of the 1st Crossing Guard Servo pin
const int servo2Pin = 6; // the number of the 2nd Crossong Guard Servo pin
const int buzzerPin = 5; // the number of the buzzer pin (optional)

// variables will change:
int buttonState = LOW; // variable for reading the pushbutton status

// create servo objects
Servo myServo1;
Servo myServo2;

void setup() {
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);

  // initialize the LED pins as an output:
  pinMode(ledPin1L, OUTPUT);
  pinMode(ledPin1R, OUTPUT);
  pinMode(ledPin2L, OUTPUT);
  pinMode(ledPin2R, OUTPUT);

  // initialize the servo pins
  myServo1.attach(servo1Pin);
  myServo2.attach(servo2Pin);

  // set inital crossing guard positions
  CrossingGuardsUp();

  // initialize the buzzer pin as output:
  pinMode(buzzerPin, OUTPUT);

  // write to the serial console that we're up!
  Serial.begin(9600);
  Serial.println("Train Crossing Active!");
}

void CrossingGuardsDown() {
  // Bring the Crossing Guard arms to the down state
  myServo1.write(180);
  myServo2.write(0);
}

void CrossingGuardsUp() {
  // Bring the Crossing Guard arms to the up state
  myServo1.write(90);
  myServo2.write(80);
}

void FlashLEDs(int flashTimes, int flashDelay, bool beep) {
  // Flash the crossing guard warning indicators for xx period of time
  int ctr1 = 0;
  for (ctr1 = 0; ctr1 <= flashTimes; ctr1 += 1) {
    // Outer lights on, inner lights off
    digitalWrite(ledPin1L, HIGH);
    digitalWrite(ledPin2R, HIGH);
    digitalWrite(ledPin2L, LOW);
    digitalWrite(ledPin1R, LOW);

    // if beep is true buzz the buzzer only on this cycle
    if (beep == true) {
      digitalWrite(buzzerPin, HIGH);
    }
    delay(flashDelay);
    // if beep is true stop the buzzer
    if (beep == true) {
      digitalWrite(buzzerPin, LOW);
    }
    // Inner lights on, outer lights off
    digitalWrite(ledPin2L, HIGH);
    digitalWrite(ledPin1R, HIGH);
    digitalWrite(ledPin1L, LOW);
    digitalWrite(ledPin2R, LOW);
    delay(flashDelay);
  }
  // all lights off at end of sequence
  digitalWrite(ledPin1L, LOW);
  digitalWrite(ledPin2R, LOW);
  digitalWrite(ledPin2L, LOW);
  digitalWrite(ledPin1R, LOW);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // start the crossing guard actions

    // Write console notifications
    Serial.println("Train Crossing Button Pressed!");

    // flash the LEDs x number of times for YY milliseconds
    FlashLEDs(7, 200, true);

    // lower the crossing guards
    CrossingGuardsDown();

    // flash the LEDs x number of times for YY milliseconds
    FlashLEDs(20, 200, true);

    // open the crossing guards when complete
    CrossingGuardsUp();

    // flash the LEDs x number of times for YY milliseconds
    FlashLEDs(5, 200, false);

  } else {
    // literally do nothing, though you could do some stuff here if you wanted....
    // which is why I put the else here... otherwise just omit the else completely...
  }
}

I hope you mean make the servos slower. They're already going at full speed. Servos don't basically have any speed control, they always go full speed. The general way to slow them is to move in small steps with a delay between each step (see the Sweep example sketch in the IDE). That would be a big code change from where you are now.

Alternatively you can try the VarSpeedServo library instead of the standard Servo.h. VarSpeedServo has a speed parameter in the write() command e.g. instead of write(180) you can do write(180, speed) where speed = 1 -255, VERY slow to full speed.

Steve