Need help changing coding on arduino no experience

Discussion in 'Microcontrollers, Programming and IoT' started by Theshay, Apr 16, 2018.

  1. Theshay

    Theshay

    Joined:
    Apr 16, 2018
    Messages:
    3
    Likes Received:
    0
    I am trying to program an arduino r3 to operate crossing gates and lights on a train table. I have found several different codes but dont have coding experience to change them to my needs. I am using 2 reed switches 2 leds and two servos. also have 2 10 second sound cards that i wish to activate. My goal is to have 1 reed switch activate the servos and the lights along with activating the two sound cards the second reed switch will deactivate everything. this is the code i would like to change i need to take out the third led
     
    Last edited: Apr 16, 2018
    Theshay, Apr 16, 2018
    #1
  2. Theshay

    Theshay

    Joined:
    Apr 16, 2018
    Messages:
    3
    Likes Received:
    0
    this is the code

    include <Servo.h>


    class Flasher
    {
    // Class Member Variables
    // These are initialized at startup
    int ledPin; // the number of the LED pin
    long OnTime; // milliseconds of on-time
    long OffTime; // milliseconds of off-time

    // These maintain the current state
    int ledState; // ledState used to set the LED
    unsigned long previousMillis; // will store last time LED was updated

    // Constructor - creates a Flasher
    // and initializes the member variables and state
    public:
    Flasher(int pin, long on, long off)
    {
    ledPin = pin;
    pinMode(ledPin, OUTPUT);

    OnTime = on;
    OffTime = off;

    ledState = LOW;
    previousMillis = 0;
    }

    void Update()
    {
    // check to see if it's time to change the state of the LED
    unsigned long currentMillis = millis();

    if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
    {
    ledState = LOW; // Turn it off
    previousMillis = currentMillis; // Remember the time
    digitalWrite(ledPin, ledState); // Update the actual LED
    }
    else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
    {
    ledState = HIGH; // turn it on
    previousMillis = currentMillis; // Remember the time
    digitalWrite(ledPin, ledState); // Update the actual LED
    }
    }
    };

    class Sweeper
    {
    Servo servo; // the servo
    int pos; // current servo position
    int increment; // increment to move for each interval
    int updateInterval; // interval between updates
    unsigned long lastUpdate; // last update of position

    public:
    Sweeper(int interval)
    {
    updateInterval = interval;
    increment = 1;
    }

    void Attach(int pin)
    {
    servo.attach(pin);
    }

    void Detach()
    {
    servo.detach();
    }

    void Update()
    {
    if((millis() - lastUpdate) > updateInterval) // time to update
    {
    lastUpdate = millis();
    pos += increment;
    servo.write(pos);
    Serial.println(pos);
    if ((pos >= 180) || (pos <= 0)) // end of sweep
    {
    // reverse direction
    increment = -increment;
    }
    }
    }
    };


    Flasher led1(11, 123, 400);
    Flasher led2(12, 350, 350);
    Flasher led3(13, 200, 222);

    Sweeper sweeper1(15);
    Sweeper sweeper2(25);

    void setup()
    {
    Serial.begin(9600);
    sweeper1.Attach(9);
    sweeper2.Attach(10);
    }


    void loop()
    {
    sweeper1.Update();

    if(digitalRead(2) == HIGH)
    {
    sweeper2.Update();
    led1.Update();
    }

    led2.Update();
    led3.Update();
    } [/CODE]
     
    Last edited: Apr 16, 2018
    Theshay, Apr 16, 2018
    #2
  3. Theshay

    Theshay

    Joined:
    Apr 16, 2018
    Messages:
    3
    Likes Received:
    0
    [​IMG]
     
    Theshay, Apr 16, 2018
    #3
  4. Theshay

    Doug3004

    Joined:
    Sep 5, 2014
    Messages:
    111
    Likes Received:
    23
    This code could be improved a lot, but I have a few questions before proceeding.

    1) I assume that you want the two LEDs to blink on alternately, like regular (US) train crossing signs do?

    2) You say that you want one reed switch to turn the signal on, and the other to turn it off. How will these switches be placed and activated? Normally there is a switch placed a couple inches from a road crossing, on both sides of the road. As long as either switch is activated, the crossing signal stays "on". So it is not really that there is an 'on' switch and an 'off' switch, but that the signal turns on if either switch is activated.

    3) How much electronics and Arduino (programming) experience do you have?
     
    Doug3004, Apr 18, 2018
    #4
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.