ARDUINO PROGRAMMING, AND INTERFACING HELP

Post all your model railway electronic problems, solutions and discoverys here.
Paul-H
Posts: 133
Joined: Fri Jun 05, 2009 3:00 pm

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby Paul-H » Sat Jun 16, 2018 6:52 am

Hi

Just a thought, if I wanted to reduce the throw of any servo is it just the case of editing the main/max throw lines in the code? For the given servo.

Paul

timbologist
Posts: 362
Joined: Wed Sep 18, 2013 6:39 am
Location: Hazeldene Victoria Australia ( in the bush )

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby timbologist » Sat Jun 16, 2018 7:20 am

Hi Paul
Paul-H wrote:Just a thought, if I wanted to reduce the throw of any servo is it just the case of editing the main/max throw lines in the code? For the given servo.


Yes that is correct the lines as shown below for each servo define the maximum and minimum throw but depending on which way your servo is mounted and the direction it moves you may have to actually swap the max and min values over so the minimum value is larger than the maximum value as shown for point10 in the code window below.

Code: Select all

const int minPoint00 =   0;      // MINIMUM THROW FOR POINT00
const int maxPoint00 =  90;      // MAXIMUM ANGLE FOR POINT00

const int minPoint10 = 180;      // MINIMUM THROW FOR POINT10
const int maxPoint10 =  90;      // MAXIMUM THROW FOR POINT10


MikeTrymTrains
Posts: 14
Joined: Mon Jul 02, 2018 9:58 pm

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby MikeTrymTrains » Sat Jul 07, 2018 1:40 pm

Hi I have some Arduino code for my railway. This code is for a Zebra Crossing. It don't delay the code so you can use with other code. I hope this show up i'm new to posting on forums I will post more code if this post works.

Code: Select all

const int PZC1 = 0;  // Zebra crossing 1
const int PZC2 = 1;  // Zebra crossing 2

void setup()
{

  pinMode(PZC1, OUTPUT);   // Zebra crossing 1
  pinMode(PZC2, OUTPUT);   // Zebra crossing 2
}

void loop() {

  // ZEBRA CROSSING

  static int state8 = 801;
  static unsigned long ts8;

  switch (state8)
  {
    case 801:
      digitalWrite(PZC1, HIGH);   // Zebra crossing 1
      digitalWrite(PZC2, LOW);    // Zebra crossing 2
      ts8 = millis();
      state8 = 802;
      break;

    case 802:
      if (millis() > ts8 + 1000)
      {
        state8 = 803;
      }
      break;

    case 803:
      digitalWrite(PZC1, LOW);    // Zebra crossing 1
      digitalWrite(PZC2, HIGH);   // Zebra crossing 2
      ts8 = millis();
      state8 = 804;
      break;

    case 804:
      if (millis() > ts8 + 1000)
      {
        state8 = 801;
      }
      break;
  }
}





Image
Last edited by MikeTrymTrains on Mon Jul 09, 2018 2:58 pm, edited 1 time in total.

MikeTrymTrains
Posts: 14
Joined: Mon Jul 02, 2018 9:58 pm

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby MikeTrymTrains » Sat Jul 07, 2018 1:46 pm

This Arduino code simulate a arc welder.

Code: Select all

int ARC1 = 0;        // Arc welding torch
const int ARC2 = 1;  // Arc welding flash

int ledState5;
long nextFlash;

void setup()
{
  pinMode(ARC1, OUTPUT);   // Arc welding torch
  pinMode(ARC2, OUTPUT);   // Arc welding flash
  digitalWrite(ARC1, LOW); // Arc welding torch
  ledState5 = LOW;
  nextFlash = millis() + random(50, 70);
}

void loop() {


    // ARC WELDER EFFECT
{
    if (millis() > nextFlash) {
      if (ledState5 == LOW) ledState5 = HIGH;
      else ledState5 = LOW;
      digitalWrite(ARC1, ledState5);    // Arc welding torch
      nextFlash = millis() + random(100, 15000);
    }
  }
  static int state5 = 50;
  static unsigned long ts5;
  if (digitalRead(ARC1) == LOW)   // Arc welding torch
    state5 = 50;
  switch (state5)
  {
    case 50:
      digitalWrite(ARC2, LOW);    // Arc welding flash
      if (digitalRead(ARC1) == HIGH)    // Arc welding torch
        state5 = 51;
      break;
    case 51:
      digitalWrite(ARC2, HIGH);   // Arc welding flash
      ts5 = millis();
      state5 = 52;
      break;
    case 52:
      if (millis() > ts5 + random(1, 15000))
        state5 = 53;
      break;
    case 53:
      digitalWrite(ARC2, LOW);    // Arc welding flash
      ts5 = millis();
      state5 = 54;
      break;
    case 54:
      if (millis() > ts5 + random(1, 15000))
        state5 = 50;
      break;
  }
}



Image
Last edited by MikeTrymTrains on Mon Jul 09, 2018 3:02 pm, edited 1 time in total.

MikeTrymTrains
Posts: 14
Joined: Mon Jul 02, 2018 9:58 pm

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby MikeTrymTrains » Sat Jul 07, 2018 1:52 pm

This Arduino code simulate emergency vehicle's

Code: Select all


const int PFA1 = 0;  // White headlights 1
const int PFA2 = 1;  // White Headlights 2
const int PFA3 = 2;  // Blue Flashing Lights 1
const int PFA4 = 3;  // Blue Flashing Lights 2

void setup()
{
  pinMode(PFA1, OUTPUT);   // White headlights Lights 1
  pinMode(PFA2, OUTPUT);   // White headlights Lights 2
  pinMode(PFA3, OUTPUT);   // Blue Flashing Lights 1
  pinMode(PFA4, OUTPUT);   // Blue Flashing Lights 2
}
void loop() {

  // POLICE FIRE AMBULANCE LIGHTS

  static int state6 = 601;
  static unsigned long ts6;

  switch (state6)
  {
    case 601:
      digitalWrite(PFA2, LOW);  // White Headlights
      digitalWrite(PFA1, HIGH);   // White headlights 1
      digitalWrite(PFA3, HIGH);   // Blue Flashing Lights 1
      ts6 = millis();
      state6 = 602;
      break;
    case 602:
      if (millis() > ts6 + 100)
        state6 = 603;
      break;
    case 603:
      digitalWrite(PFA3, LOW);    // Blue Flashing Lights 1
      ts6 = millis();
      state6 = 604;
      break;
    case 604:
      if (millis() > ts6 + 100)
        state6 = 605;
      break;
    case 605:
      digitalWrite(PFA3, HIGH);   // Blue Flashing Lights 1
      ts6 = millis();
      state6 = 606;
      break;
    case 606:
      if (millis() > ts6 + 100)
        state6 = 607;
      break;
    case 607:
      digitalWrite(PFA3, LOW);    // Blue Flashing Lights 1
      ts6 = millis();
      state6 = 608;
      break;
    case 608:
      if (millis() > ts6 + 100)
        state6 = 609;
      break;
    case 609:
      digitalWrite(PFA2, HIGH);   // White Headlights
      digitalWrite(PFA1, LOW);    // White headlights 1
      digitalWrite(PFA3, LOW);    // Blue Flashing Lights 1
      digitalWrite(PFA4, HIGH);   // Blue Flashing Lights 2
      ts6 = millis();
      state6 = 610;
      break;
    case 610:
      if (millis() > ts6 + 100)
        state6 = 611;
      break;
    case 611:
      digitalWrite(PFA4, LOW);    // Blue Flashing Lights 2
      ts6 = millis();
      state6 = 612;
      break;
    case 612:
      if (millis() > ts6 + 100)
        state6 = 613;
      break;
    case 613:
      digitalWrite(PFA4, HIGH);   // Blue Flashing Lights 2
      ts6 = millis();
      state6 = 614;
    case 614:
      if (millis() > ts6 + 100)
        state6 = 615;
      break;
    case 615:
      digitalWrite(PFA4, LOW);    // Blue Flashing Lights 2
      ts6 = millis();
      state6 = 616;
      break;
    case 616:
      if (millis() > ts6 + 100)
        state6 = 601;
      break;
  }
}




Image
Last edited by MikeTrymTrains on Mon Jul 09, 2018 3:07 pm, edited 1 time in total.

MikeTrymTrains
Posts: 14
Joined: Mon Jul 02, 2018 9:58 pm

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby MikeTrymTrains » Sat Jul 07, 2018 2:13 pm

This Arduino code simulate a fire effect. I will post three codes together. One using PWM and one without PWM and a comparison between the two. The code without PWM can be used with tone's but it's not as good as the one with PWM but you can't use tone's when using the PWM version.
PWM Version

Code: Select all

int FL1 = 9; // Yellow LED
int FL2 = 10; // Red LED
int FL3 = 11; // Yellow LED

void setup()
{
  pinMode(FL1, OUTPUT);
  pinMode(FL2, OUTPUT);
  pinMode(FL3, OUTPUT);
}

void loop()
{
  static int state2 = 50;
  static unsigned long ts2;

  switch (state2)
  {
    case 50:
      analogWrite(FL1, random(120) + 135);
      analogWrite(FL2, random(120) + 135);
      analogWrite(FL3, random(120) + 135);
      ts2 = millis();
      state2 = 51;
      break;
    case 51:
      if (millis() > ts2 + 100)
        state2 = 50;
      break;
  }
}



Image


And the non PWM version

Code: Select all

const int Fire1 = 0;
const int Fire2 = 1;
const int Fire3 = 2;
int FireState1 = HIGH;
int FireState2 = HIGH;
int FireState3 = HIGH;
long previousMillis1 = 0;
long previousMillis2 = 0;
long previousMillis3 = 0;
long interval2 = 100;
long interval3 = 150;
long interval4 = 200;

void setup()
{
  pinMode(Fire1, OUTPUT); 
  pinMode(Fire2, OUTPUT);
  pinMode(Fire3, OUTPUT);
}
void loop() {
  unsigned long currentMillis1 = millis();
  if(currentMillis1 - previousMillis1 > random(10, interval2)) {

    previousMillis1 = currentMillis1;   

    if (FireState1 == HIGH)
      FireState1 = LOW;
    else
      FireState1 = HIGH;

    digitalWrite(Fire1, FireState1);
  }
  unsigned long currentMillis2 = millis();
  if(currentMillis2 - previousMillis2 > random(10, interval3)) {
    previousMillis2 = currentMillis2;   

    if (FireState2 == HIGH)
      FireState2 = LOW;
    else
      FireState2 = HIGH;

    digitalWrite(Fire2, FireState2);
  }
  unsigned long currentMillis3 = millis();
  if(currentMillis3 - previousMillis3 > random(10, interval4)) {
    previousMillis3 = currentMillis3;   

    if (FireState3 == HIGH)
      FireState3 = LOW;
    else
      FireState3 = HIGH;

    digitalWrite(Fire3, FireState3);
  }
}




Image

And a comparison between the two

Code: Select all

int FL1 = 9; // Yellow LED
int FL2 = 10; // Red LED
int FL3 = 11; // Yellow LED
const int Fire1 = 0; // Fire effect 1
const int Fire2 = 1; // Fire Effect 2
const int Fire3 = 2; // Fire Effict 3
int FireState1 = HIGH;
int FireState2 = HIGH;
int FireState3 = HIGH;
long previousMillis1 = 0;
long previousMillis2 = 0;
long previousMillis3 = 0;
long interval2 = 100;
long interval3 = 150;
long interval4 = 200;

void setup()
{
  pinMode(Fire1, OUTPUT);  // Fire effect 1
  pinMode(Fire2, OUTPUT);  // Fire Effect 2
  pinMode(Fire3, OUTPUT);  // Fire Effict 3
  pinMode(FL1, OUTPUT);
  pinMode(FL2, OUTPUT);
  pinMode(FL3, OUTPUT);
}
void loop() {

  // Fire Effect without out PWM
  unsigned long currentMillis1 = millis();
  if(currentMillis1 - previousMillis1 > random(10, interval2)) {

    previousMillis1 = currentMillis1;   

    if (FireState1 == HIGH)
      FireState1 = LOW;
    else
      FireState1 = HIGH;

    digitalWrite(Fire1, FireState1);
  }
  unsigned long currentMillis2 = millis();
  if(currentMillis2 - previousMillis2 > random(10, interval3)) {
    previousMillis2 = currentMillis2;   

    if (FireState2 == HIGH)
      FireState2 = LOW;
    else
      FireState2 = HIGH;

    digitalWrite(Fire2, FireState2);
  }
  unsigned long currentMillis3 = millis();
  if(currentMillis3 - previousMillis3 > random(10, interval4)) {
    previousMillis3 = currentMillis3;   

    if (FireState3 == HIGH)
      FireState3 = LOW;
    else
      FireState3 = HIGH;

    digitalWrite(Fire3, FireState3);
  }

  // Fire Effect with PWM
 
  static int state2 = 80;
  static unsigned long ts2;

  switch (state2)
  {
    case 80:
      analogWrite(FL1, random(120) + 135);
      analogWrite(FL2, random(120) + 135);
      analogWrite(FL3, random(120) + 135);
      ts2 = millis();
      state2 = 81;
      break;
    case 81:
      if (millis() > ts2 + 100)
        state2 = 80;
      break;
  } 
}





Image
Last edited by MikeTrymTrains on Mon Jul 09, 2018 3:17 pm, edited 3 times in total.

MikeTrymTrains
Posts: 14
Joined: Mon Jul 02, 2018 9:58 pm

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby MikeTrymTrains » Sat Jul 07, 2018 2:21 pm

This Arduino code simulate UK traffic lights. One two way and one four way.
Two way

Code: Select all

const int RT1 = 0; // RED LED TRAFFIC LIGHTS
const int AT1 = 1; // AMBER LED TRAFFIC LIGHTS
const int GT1 = 2; // GREEN LED TRAFFIC LIGHTS
const int RT2 = 3; // RED LED TRAFFIC LIGHTS
const int AT2 = 4; // AMBER LED TRAFFIC LIGHTS
const int GT2 = 5; // GREEN LED TRAFFIC LIGHTS

void setup()
{
  pinMode(GT1,OUTPUT); // GREEN LED TRAFFIC LIGHTS
  pinMode(AT1,OUTPUT); // AMBER LED TRAFFIC LIGHTS
  pinMode(RT1,OUTPUT); // RED LED TRAFFIC LIGHTS
  pinMode(GT2,OUTPUT); // GREEN LED TRAFFIC LIGHTS
  pinMode(AT2,OUTPUT); // AMBER LED TRAFFIC LIGHTS
  pinMode(RT2,OUTPUT); // RED LED TRAFFIC LIGHTS
}

void loop()
{
   // TRAFFIC LIGHTS
 
  static int state = 1;
  static unsigned long ts;
  switch(state)
  {
   case 1:
    digitalWrite(RT2,HIGH); // RED LED TRAFFIC LIGHTS ON
    digitalWrite(RT1,HIGH); // RED LED TRAFFIC LIGHTS ON
    ts = millis();
    state = 2;
    break;
  case 2:
    if(millis() > ts + 2000)
    state = 3;
    break;
  case 3:
    digitalWrite(AT2,HIGH); // AMBER LED TRAFFIC LIGHTS ON
    ts = millis();
    state = 4;
    break;
  case 4:
    if(millis() > ts + 1000)
    state = 5;
    break;
  case 5:
    digitalWrite(RT2,LOW); // RED LED TRAFFIC LIGHTS OFF
    digitalWrite(AT2,LOW); // AMBER LED TRAFFIC LIGHTS OFF
    digitalWrite(GT2,HIGH); // GREEN LED TRAFFIC LIGHTS ON
    ts = millis();
    state = 6;
    break;
  case 6:
    if(millis() > ts + 6000)
    state = 7;
    break;
  case 7:
    digitalWrite(GT2,LOW); // GREEN LED TRAFFIC LIGHTS OFF
    digitalWrite(AT2,HIGH); // AMBER LED TRAFFIC LIGHTS ON
    ts = millis();
    state = 8;
    break;
  case 8:
    if(millis() > ts + 1000)
    state = 9;
    break;
  case 9:
    digitalWrite(AT2,LOW); // AMBER LED TRAFFIC LIGHTS OFF
    digitalWrite(RT2,HIGH); // RED LED TRAFFIC LIGHTS ON
    ts = millis();
    state = 10;
    break;
  case 10:
    if(millis() > ts + 2000)
    state = 11;
    break;
  case 11:
    digitalWrite(AT1,HIGH); // AMBER LED TRAFFIC LIGHTS ON
    ts = millis();
    state = 12;
    break;
  case 12:
    if(millis() > ts + 1000)
    state = 13;
    break;
  case 13:
    digitalWrite(RT1,LOW); // RED LED TRAFFIC LIGHTS OFF
    digitalWrite(AT1,LOW); // AMBER LED TRAFFIC LIGHTS OFF
    digitalWrite(GT1,HIGH); // GREEN LED TRAFFIC LIGHTS ON
    ts = millis();
    state = 14;
    break;
  case 14:
    if(millis() > ts + 6000)
    state = 15;
    break;
  case 15:
    digitalWrite(GT1,LOW); // GREEN LED TRAFFIC LIGHTS OFF
    digitalWrite(AT1,HIGH); // AMBER LED TRAFFIC LIGHTS ON
    ts = millis();
    state = 16;
    break;
  case 16:
    if(millis() > ts + 1000)
    state = 17;
    break;
  case 17:
    digitalWrite(AT1,LOW); // AMBER LED TRAFFIC LIGHTS OFF
    digitalWrite(RT1,HIGH); // RED LED TRAFFIC LIGHTS ON
    if(millis() > ts + 1000)
    state = 1;
    break;
  }
}


Image



And four way

Code: Select all

const int RT1 = 0; // RED LED TRAFFIC LIGHTS
const int AT1 = 1; // AMBER LED TRAFFIC LIGHTS
const int GT1 = 2; // GREEN LED TRAFFIC LIGHTS
const int RT2 = 3; // RED LED TRAFFIC LIGHTS
const int AT2 = 4; // AMBER LED TRAFFIC LIGHTS
const int GT2 = 5; // GREEN LED TRAFFIC LIGHTS
const int RT3 = 6; // RED LED TRAFFIC LIGHTS
const int AT3 = 7; // AMBER LED TRAFFIC LIGHTS
const int GT3 = 8; // GREEN LED TRAFFIC LIGHTS
const int RT4 = 9; // RED LED TRAFFIC LIGHTS
const int AT4 = 10; // AMBER LED TRAFFIC LIGHTS
const int GT4 = 11; // GREEN LED TRAFFIC LIGHTS


void setup()
{
  pinMode(GT1,OUTPUT); // GREEN LED TRAFFIC LIGHTS
  pinMode(AT1,OUTPUT); // AMBER LED TRAFFIC LIGHTS
  pinMode(RT1,OUTPUT); // RED LED TRAFFIC LIGHTS
  pinMode(GT2,OUTPUT); // GREEN LED TRAFFIC LIGHTS
  pinMode(AT2,OUTPUT); // AMBER LED TRAFFIC LIGHTS
  pinMode(RT2,OUTPUT); // RED LED TRAFFIC LIGHTS
  pinMode(GT3,OUTPUT); // GREEN LED TRAFFIC LIGHTS
  pinMode(AT3,OUTPUT); // AMBER LED TRAFFIC LIGHTS
  pinMode(RT3,OUTPUT); // RED LED TRAFFIC LIGHTS
  pinMode(GT4,OUTPUT); // GREEN LED TRAFFIC LIGHTS
  pinMode(AT4,OUTPUT); // AMBER LED TRAFFIC LIGHTS
  pinMode(RT4,OUTPUT); // RED LED TRAFFIC LIGHTS
}

void loop()
{
   // TRAFFIC LIGHTS
  static int state = 1;
  static unsigned long ts;
  switch(state)
  {
   case 1:
    digitalWrite(RT2,HIGH); // RED LED TRAFFIC LIGHTS ON
    digitalWrite(RT1,HIGH); // RED LED TRAFFIC LIGHTS ON
    digitalWrite(RT3,HIGH); // RED LED TRAFFIC LIGHTS ON
    digitalWrite(RT4,HIGH); // RED LED TRAFFIC LIGHTS ON
    ts = millis();
    state = 2;
    break;
  case 2:
    if(millis() > ts + 2000)
    state = 3;
    break;
  case 3:
    digitalWrite(AT1,HIGH); // AMBER LED TRAFFIC LIGHTS ON
    ts = millis();
    state = 4;
    break;
  case 4:
    if(millis() > ts + 1000)
    state = 5;
    break;
  case 5:
    digitalWrite(RT1,LOW); // RED LED TRAFFIC LIGHTS OFF
    digitalWrite(AT1,LOW); // AMBER LED TRAFFIC LIGHTS OFF
    digitalWrite(GT1,HIGH); // GREEN LED TRAFFIC LIGHTS ON
    ts = millis();
    state = 6;
    break;
  case 6:
    if(millis() > ts + 6000)
    state = 7;
    break;
  case 7:
    digitalWrite(GT1,LOW); // GREEN LED TRAFFIC LIGHTS OFF
    digitalWrite(AT1,HIGH); // AMBER LED TRAFFIC LIGHTS ON
    ts = millis();
    state = 8;
    break;
  case 8:
    if(millis() > ts + 1000)
    state = 9;
    break;
  case 9:
    digitalWrite(AT1,LOW); // AMBER LED TRAFFIC LIGHTS OFF
    digitalWrite(RT1,HIGH); // RED LED TRAFFIC LIGHTS ON
    ts = millis();
    state = 10;
    break;
  case 10:
    if(millis() > ts + 2000)
    state = 11;
    break;
  case 11:
    digitalWrite(AT2,HIGH); // AMBER LED TRAFFIC LIGHTS ON
    ts = millis();
    state = 12;
    break;
  case 12:
    if(millis() > ts + 1000)
    state = 13;
    break;
  case 13:
    digitalWrite(RT2,LOW); // RED LED TRAFFIC LIGHTS OFF
    digitalWrite(AT2,LOW); // AMBER LED TRAFFIC LIGHTS OFF
    digitalWrite(GT2,HIGH); // GREEN LED TRAFFIC LIGHTS ON
    ts = millis();
    state = 14;
    break;
  case 14:
    if(millis() > ts + 6000)
    state = 15;
    break;
  case 15:
    digitalWrite(GT2,LOW); // GREEN LED TRAFFIC LIGHTS OFF
    digitalWrite(AT2,HIGH); // AMBER LED TRAFFIC LIGHTS ON
    ts = millis();
    state = 16;
    break;
  case 16:
    if(millis() > ts + 1000)
    state = 17;
    break;
  case 17:
    digitalWrite(AT2,LOW); // AMBER LED TRAFFIC LIGHTS OFF
    digitalWrite(RT2,HIGH); // RED LED TRAFFIC LIGHTS ON
    if(millis() > ts + 1000)
    state = 18;
    break;
     case 18:
    digitalWrite(RT3,HIGH); // RED LED TRAFFIC LIGHTS ON
    ts = millis();
    state = 19;
    break;
  case 19:
    if(millis() > ts + 2000)
    state = 20;
    break;
  case 20:
    digitalWrite(AT3,HIGH); // AMBER LED TRAFFIC LIGHTS ON
    ts = millis();
    state = 21;
    break;
  case 21:
    if(millis() > ts + 1000)
    state = 22;
    break;
  case 22:
    digitalWrite(RT3,LOW); // RED LED TRAFFIC LIGHTS OFF
    digitalWrite(AT3,LOW); // AMBER LED TRAFFIC LIGHTS OFF
    digitalWrite(GT3,HIGH); // GREEN LED TRAFFIC LIGHTS ON
    ts = millis();
    state = 23;
    break;
  case 23:
    if(millis() > ts + 6000)
    state = 24;
    break;
  case 24:
    digitalWrite(GT3,LOW); // GREEN LED TRAFFIC LIGHTS OFF
    digitalWrite(AT3,HIGH); // AMBER LED TRAFFIC LIGHTS ON
    ts = millis();
    state = 25;
    break;
  case 25:
    if(millis() > ts + 1000)
    state = 26;
    break;
  case 26:
    digitalWrite(AT3,LOW); // AMBER LED TRAFFIC LIGHTS OFF
    digitalWrite(RT3,HIGH); // RED LED TRAFFIC LIGHTS ON
    ts = millis();
    state = 27;
    break;
  case 27:
    if(millis() > ts + 2000)
    state = 28;
    break;
  case 28:
    digitalWrite(AT4,HIGH); // AMBER LED TRAFFIC LIGHTS ON
    ts = millis();
    state = 29;
    break;
  case 29:
    if(millis() > ts + 1000)
    state = 30;
    break;
  case 30:
    digitalWrite(RT4,LOW); // RED LED TRAFFIC LIGHTS OFF
    digitalWrite(AT4,LOW); // AMBER LED TRAFFIC LIGHTS OFF
    digitalWrite(GT4,HIGH); // GREEN LED TRAFFIC LIGHTS ON
    ts = millis();
    state = 31;
    break;
  case 31:
    if(millis() > ts + 6000)
    state = 32;
    break;
  case 32:
    digitalWrite(GT4,LOW); // GREEN LED TRAFFIC LIGHTS OFF
    digitalWrite(AT4,HIGH); // AMBER LED TRAFFIC LIGHTS ON
    ts = millis();
    state = 33;
    break;
  case 33:
    if(millis() > ts + 1000)
    state = 34;
    break;
  case 34:
    digitalWrite(AT4,LOW); // AMBER LED TRAFFIC LIGHTS OFF
    digitalWrite(RT4,HIGH); // RED LED TRAFFIC LIGHTS ON
    if(millis() > ts + 1000)
    state = 1;
    break; 
  }
}


Image
Last edited by MikeTrymTrains on Mon Jul 09, 2018 3:23 pm, edited 2 times in total.

MikeTrymTrains
Posts: 14
Joined: Mon Jul 02, 2018 9:58 pm

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby MikeTrymTrains » Sat Jul 07, 2018 2:37 pm

I'm posting my master code to show what can be done with an Arduino and it might help others with there projects

Code: Select all

const int TFB = 13;   // Train forward and backward
const int TG1 = 12;   // Train light green
const int TR1 = 11;   // Train light Red
const int FC1 = 10;   // Faller railway crossing stop
const int AC1 = 8;    // Amber crossing leds
const int RC1 = 9;    // Red crossing leds right
const int RC2 = 7;    // Red crossing leds left
const int SER = 6;    // Servo Crossing Gates
const int SW1 = 5;    // Switch for railway crossing
const int TON = 4;    // Railway crossing transister to speaker toy
const int TON1 = 3;   // Pedestrian crossing tone
const int PCG = 2;    // Green led pedestrian crossing
const int PCR = 1;    // Red led pedestrian switch activated crossing
const int PCR1 = 0;   // Red led pedestrian crossing
const int FC2 = 14;   // Faller traffic lights
const int FC3 = 15;   // Faller traffic lights
const int SW2 = 16;   // Switch for pedestrian crossing
const int GT1 = 17;   // Green traffic light 1
const int AT1 = 18;   // Amber traffic lights 1
const int RT1 = 19;   // Red traffic lights 1
const int GT2 = 20;   // Green traffic lights 2
const int AT2 = 21;   // Amber traffic lights 2
const int RT2 = 22;   // Red traffic Lights 2
const int Fire1 = 23; // Fire effect 1
const int Fire2 = 24; // Fire Effect 2
const int Fire3 = 25; // Fire Effict 3
int ARC1 = 26;        // Arc welding torch
const int ARC2 = 27;  // Arc welding flash
const int PFA1 = 28;  // White headlights 1
const int PFA2 = 29;  // White Headlights 2
const int PFA3 = 30;  // Blue Flashing Lights 1
const int PFA4 = 31;  // Blue Flashing Lights 2
const int ELF1 = 32;  // Yellow flashing lights 1
const int ELF2 = 33;  // Yellow flashing lights 2
const int PZC1 = 34;  // Zebra crossing 1
const int PZC2 = 35;  // Zebra crossing 2
int pulse = 500;
int pulse1 = 1150;
int state5 = LOW;
int reading;
int previous = LOW;
long time = 0;
long debounce = 10;
int ledState5;
long nextFlash;
int SW2State = 0;   // Switch for pedestrian crossing
int ledState1 = HIGH;
int buttonState1;
int lastButtonState1 = LOW;
long lastDebounceTime1 = 0;
long debounceDelay1 = 50;
int ledState = LOW;
unsigned long previousMillis = 0;   // will store last time LED was updated
const long interval = 45000;
int FireState1 = HIGH;
int FireState2 = HIGH;
int FireState3 = HIGH;
long previousMillis1 = 0;
long previousMillis2 = 0;
long previousMillis3 = 0;
long interval2 = 100;
long interval3 = 150;
long interval4 = 200;

void setup()
{
  pinMode(PCR1, OUTPUT);   // Red led pedestrian crossing
  pinMode(FC1, OUTPUT);    // Faller railway crossing
  pinMode(SER, OUTPUT);    // Servo Crossing Gates
  pinMode(SW1, INPUT);     // Switch for railway crossing
  pinMode(TON, OUTPUT);    // Railway crossing transister to speaker toy
  pinMode(AC1, OUTPUT);    // Amber crossing leds
  pinMode(RC1, OUTPUT);    // Red crossing leds right
  pinMode(RC2, OUTPUT);    // Red crossing leds left
  pinMode(FC2, OUTPUT);    // Faller traffic lights
  pinMode(Fire1, OUTPUT);  // Fire effect 1
  pinMode(Fire2, OUTPUT);  // Fire Effect 2
  pinMode(Fire3, OUTPUT);  // Fire Effict 3
  pinMode(PCG, OUTPUT);    // Green led pedestrian crossing
  pinMode(PCR, OUTPUT);    // Red led pedestrian switch activated crossing
  pinMode(FC3, OUTPUT);    // Faller traffic lights
  pinMode(TON1, OUTPUT);   // Pedestrian crossing tone
  pinMode(SW2, INPUT);     // Switch for pedestrian crossing
  pinMode(ARC1, OUTPUT);   // Arc welding torch
  pinMode(ARC2, OUTPUT);   // Arc welding flash
  pinMode(PFA1, OUTPUT);   // White headlights 1
  pinMode(PFA2, OUTPUT);   // White Headlights
  pinMode(PFA3, OUTPUT);   // Blue Flashing Lights 1
  pinMode(PFA4, OUTPUT);   // Blue Flashing Lights 2
  pinMode(ELF1, OUTPUT);   // Yellow flashing lights 1
  pinMode(ELF2, OUTPUT);   // Yellow flashing lights 2
  pinMode(TFB, OUTPUT);    // Train forward and backward
  pinMode(RT1, OUTPUT);    // Red traffic lights 1
  pinMode(AT1, OUTPUT);    // Amber traffic lights 1
  pinMode(GT1, OUTPUT);    // Green traffic light 1
  pinMode(RT2, OUTPUT);    // Red traffic Lights 2
  pinMode(AT2, OUTPUT);    // Amber traffic lights 2
  pinMode(GT2, OUTPUT);    // Green traffic lights 2
  pinMode(TG1, OUTPUT);    // Train light green
  pinMode(TR1, OUTPUT);    // Train light red
  pinMode(PZC1, OUTPUT);   // Zebra crossing 1
  pinMode(PZC2, OUTPUT);   // Zebra crossing 2
  digitalWrite(FC1, HIGH); // Faller railway crossing stop
  digitalWrite(TR1, HIGH); // Train light Red
  digitalWrite(PCR1, HIGH);// Red led pedestrian crossing
  digitalWrite(ARC1, LOW); // Arc welding torch
  ledState5 = LOW;
  nextFlash = millis() + random(50, 70);
}

void loop() {

  // TRAIN FORWARDS AND BACKWARDS

  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {

    previousMillis = currentMillis;

    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    digitalWrite(TFB , ledState); // Train forward and backward
  }

  // SWITCH FOR PEDESTRIAN CROSSING

  if (digitalRead(PCG) == HIGH)   // Green led pedestrian crossing
    digitalWrite(PCR, LOW);   // Red led pedestrian switch activated crossing
  else
    SW2State = digitalRead(SW2);    // Switch for pedestrian crossing
  if (SW2State == HIGH) {
    digitalWrite(PCR, HIGH);    // Red led pedestrian switch activated crossing
  }

  // TRAFFIC LIGHTS

  static int state = 0;
  static unsigned long ts;
  switch (state)
  {
    case 0:
      digitalWrite(RT2, HIGH);    // Red traffic Lights 2 on
      digitalWrite(RT1, HIGH);    // Red traffic lights 1 on
      if (digitalRead(PCR) == HIGH) {   // Red led pedestrian switch activated crossing
        digitalWrite(PCG, HIGH);    // Green led pedestrian crossing
      }
      digitalWrite(PCR, LOW);   // Red led pedestrian switch activated crossing
      state = 1;
      break;
    case 1:
      if (digitalRead(PCG) == HIGH)   // Green led pedestrian crossing
        digitalWrite (PCR1, LOW);   // Red led pedestrian crossing
      if (digitalRead(PCG) == HIGH)   // Green led pedestrian crossing
        state = 2;
      if (digitalRead(PCG) == LOW)    // Green led pedestrian crossing
        state =  6;
      break;
      state = 2;
      break;
    case 2:
      if (millis() > ts + 4000)
        state = 3;
      break;
    case 3:
      digitalWrite(PCG, LOW);   // Green led pedestrian crossing
      state = 4;
      break;
    case 4:
      digitalWrite (PCR1, HIGH);    // Red led pedestrian crossing
      if (millis() > ts + 2000)
        state = 5;
      break;
    case 5:
      digitalWrite(RT2, HIGH);    // Red traffic Lights 2 on
      ts = millis();
      state = 1;
      break;
    case 6:
      if (millis() > ts + 2000)
        state = 7;
      break;
    case 7:
      digitalWrite(AT2, HIGH);    // Amber traffic lights 2 on
      ts = millis();
      state = 8;
      break;
    case 8:
      if (millis() > ts + 1000)
        state = 9;
      break;
    case 9:
      digitalWrite(RT2, LOW);   // Red traffic Lights 2 off
      digitalWrite(AT2, LOW);   // Amber traffic lights 2 off
      digitalWrite(FC2, HIGH);    // Faller traffic lights go
      digitalWrite(GT2, HIGH);    // Green traffic lights 2 on
      ts = millis();
      state = 10;
      break;
    case 10:
      if (millis() > ts + 5000)
        state = 11;
      break;
    case 11:
      digitalWrite(FC2, LOW);   // Faller traffic lights stop
      digitalWrite(GT2, LOW);   // Green traffic lights 2 off
      digitalWrite(AT2, HIGH);    // Amber traffic lights 2 on
      ts = millis();
      state = 12;
      break;
    case 12:
      if (millis() > ts + 1000)
        state = 13;
      break;
      break;
    case 13:
      digitalWrite(AT2, LOW);   // Amber traffic lights 2 on
      digitalWrite(RT2, HIGH);    // Red traffic Lights 2 on
      ts = millis();
      state = 14;
      break;
    case 14:
      if (millis() > ts + 1000)
        state = 15;
      break;
    case 15:
      digitalWrite(AT1, HIGH);    // Amber traffic lights 1 on
      ts = millis();
      state = 16;
      break;
    case 16:
      if (millis() > ts + 1000)
        state = 17;
      break;
    case 17:
      digitalWrite(FC3, HIGH);    // Faller traffic lights go
      digitalWrite(AT1, LOW);   // Amber traffic lights 1 off
      digitalWrite(RT1, LOW);   // Red traffic lights 1 off
      digitalWrite(GT1, HIGH);    // Green traffic light 1 on
      ts = millis();
      state = 18;
      break;
    case 18:
      if (millis() > ts + 5000)
        state = 19;
      break;
    case 19:
      digitalWrite(FC3, LOW);   // Faller traffic lights stop
      digitalWrite(GT1, LOW);   // Green traffic light 1 off
      digitalWrite(AT1, HIGH);    // Amber traffic lights 1 on
      ts = millis();
      state = 20;
      break;
    case 20:
      if (millis() > ts + 1000)
        state = 21;
      break;
    case 21:
      digitalWrite(AT1, LOW);   // Amber traffic lights 1 off
      digitalWrite(RT1, HIGH);    // Red traffic lights 1 on
      if (millis() > ts + 1000)
        state = 0;
      break;
  }

  // PEDESTRIAN CROSSING TONE

  static int state2 = 201;
  static unsigned long ts2;
  if (digitalRead(PCG) == LOW)    // Green led pedestrian crossing
    state2 =  201;
  switch (state2)
  {
    case 201:
      if (digitalRead(PCG) == HIGH)   // Green led pedestrian crossing
        state2 = 202;
      break;
    case 202:
      tone(TON1, 3000);   // Pedestrian crossing tone
      ts2 = millis();
      state2 = 203;
      break;
    case 203:
      if (millis() > ts2 + 100)
        state2 = 204;
      break;
    case 204:
      noTone(TON1);   // Pedestrian crossing tone
      ts2 = millis();
      state2 = 205;
      break;
    case 205:
      if (millis() > ts2 + 100)
        state2 = 202;
      break;
  }
  // RAILWAY CROSSING SWITCH

  reading = digitalRead(SW1);   // Switch for railway crossing
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state5 == HIGH)
      state5 = LOW;
    else
      state5 = HIGH;

    time = millis();
  }

  digitalWrite(TG1, state5);    // Train light green

  previous = reading;

  if (digitalRead(TG1) == HIGH)   // Train light green
    digitalWrite(TR1, LOW);   // Train light Red
  else
    digitalWrite(TR1, HIGH);    // Train light Red


  // RAILWAY CROSSING

  static int state3 = 301;
  static unsigned long ts3;

  switch (state3)
  {
    case 301:
      if (digitalRead(TG1) == HIGH)   // Train light green
        state3 = 302;
      break;
    case 302:
      digitalWrite(AC1, HIGH);    // Amber crossing leds
      digitalWrite(TON, HIGH);    // Railway crossing transister to speaker toy
      ts3 = millis();
      state3 = 303;
      break;
    case 303:
      if (millis() > ts3 + 250)
        state3 = 304;
      break;
    case 304:
      digitalWrite(FC1, LOW);   // Faller railway crossing stop
      ts3 = millis();
      state3 = 305;
      break;
    case 305:
      if (millis() > ts3 + 250)
        state3 = 306;
      break;
    case 306:
      digitalWrite(AC1, LOW);   // Amber crossing leds
      ts3 = millis();
      state3 = 307;
      break;
    case 307:
      if (millis() > ts3 + 250)
        state3 = 308;
      break;
    case 308:
      digitalWrite(RC1, HIGH);    // Red crossing leds right
      digitalWrite(RC2, HIGH);    // Red crossing leds left
      ts3 = millis();
      state3 = 309;
      break;
    case 309:
      if (millis() > ts3 + 250)
        state3 = 310;
      break;
    case 310:
      digitalWrite(RC1, LOW);   // Red crossing leds right
      digitalWrite(RC2, LOW);   // Red crossing leds left
      ts3 = millis();
      state3 = 311;
      break;
    case 311:
      if (millis() > ts3 + 250)
        state3 = 312;
      break;
    case 312:
      digitalWrite(RC1, HIGH);    // Red crossing leds right
      digitalWrite(RC2, HIGH);    // Red crossing leds left
      ts3 = millis();
      state3 = 313;
      break;
    case 313:
      if (millis() > ts3 + 250)
        state3 = 314;
      break;
    case 314:
      digitalWrite(RC1, LOW);   // Red crossing leds right
      digitalWrite(RC2, LOW);   // Red crossing leds left
      ts3 = millis();
      state3 = 315;
      break;
    case 315:
      if (millis() > ts3 + 250)
        state3 = 316;
      break;
    case 316:
      digitalWrite(RC1, HIGH);    // Red crossing leds right
      digitalWrite(RC2, LOW);   // Red crossing leds left
      ts3 = millis();
      state3 = 317;
      break;
    case 317:
      if (millis() > ts3 + 250)
        state3 = 318;
      break;
    case 318:
      digitalWrite(RC1, LOW);   // Red crossing leds right
      digitalWrite(RC2, HIGH);    // Red crossing leds left
      ts3 = millis();
      state3 = 319;
      break;
    case 319:
      if (millis() > ts3 + 250)
        state3 = 320;
      break;
    case 320:
      if (digitalRead(TG1) == LOW)    // Train light green
        state3 = 321;
      else
        state3 = 316;
      break;
    case 321:
      digitalWrite(TON, LOW);   // Railway crossing transister to speaker toy
      digitalWrite(RC2, LOW);   // Red crossing leds left
      digitalWrite(TG1, LOW);   // Train light green
      ts3 = millis();
      state3 = 322;
      break;
    case 322:
      if (millis() > ts3 + 2500)
        state3 = 323;
      break;
    case 323:
      digitalWrite(FC1, HIGH);    // Faller railway crossing stop
      state3 = 301;
      break;
  }

 // Fire Effect 
   unsigned long currentMillis1 = millis();
  if(currentMillis1 - previousMillis1 > random(10, interval2)) {

    previousMillis1 = currentMillis1;   

    if (FireState1 == HIGH)
      FireState1 = LOW;
    else
      FireState1 = HIGH;

    digitalWrite(Fire1, FireState1);
  }
  unsigned long currentMillis2 = millis();
  if(currentMillis2 - previousMillis2 > random(10, interval3)) {
    previousMillis2 = currentMillis2;   

    if (FireState2 == HIGH)
      FireState2 = LOW;
    else
      FireState2 = HIGH;

    digitalWrite(Fire2, FireState2);
  }
  unsigned long currentMillis3 = millis();
  if(currentMillis3 - previousMillis3 > random(10, interval4)) {
    previousMillis3 = currentMillis3;   

    if (FireState3 == HIGH)
      FireState3 = LOW;
    else
      FireState3 = HIGH;

    digitalWrite(Fire3, FireState3);
  }
  {

    // ARC WELDER EFFECT

    if (millis() > nextFlash) {
      if (ledState5 == LOW) ledState5 = HIGH;
      else ledState5 = LOW;
      digitalWrite(ARC1, ledState5);    // Arc welding torch
      nextFlash = millis() + random(100, 15000);
    }
  }
  static int state5 = 50;
  static unsigned long ts5;
  if (digitalRead(ARC1) == LOW)   // Arc welding torch
    state5 = 50;
  switch (state5)
  {
    case 50:
      digitalWrite(ARC2, LOW);    // Arc welding flash
      if (digitalRead(ARC1) == HIGH)    // Arc welding torch
        state5 = 51;
      break;
    case 51:
      digitalWrite(ARC2, HIGH);   // Arc welding flash
      ts5 = millis();
      state5 = 52;
      break;
    case 52:
      if (millis() > ts5 + random(1, 15000))
        state5 = 53;
      break;
    case 53:
      digitalWrite(ARC2, LOW);    // Arc welding flash
      ts5 = millis();
      state5 = 54;
      break;
    case 54:
      if (millis() > ts5 + random(1, 15000))
        state5 = 50;
      break;
  }

  // POLICE FIRE AMBULANCE LIGHTS

  static int state6 = 601;
  static unsigned long ts6;

  switch (state6)
  {
    case 601:
      digitalWrite(PFA2, LOW);  // White Headlights
      digitalWrite(PFA1, HIGH);   // White headlights 1
      digitalWrite(PFA3, HIGH);   // Blue Flashing Lights 1
      ts6 = millis();
      state6 = 602;
      break;
    case 602:
      if (millis() > ts6 + 100)
        state6 = 603;
      break;
    case 603:
      digitalWrite(PFA3, LOW);    // Blue Flashing Lights 1
      ts6 = millis();
      state6 = 604;
      break;
    case 604:
      if (millis() > ts6 + 100)
        state6 = 605;
      break;
    case 605:
      digitalWrite(PFA3, HIGH);   // Blue Flashing Lights 1
      ts6 = millis();
      state6 = 606;
      break;
    case 606:
      if (millis() > ts6 + 100)
        state6 = 607;
      break;
    case 607:
      digitalWrite(PFA3, LOW);    // Blue Flashing Lights 1
      ts6 = millis();
      state6 = 608;
      break;
    case 608:
      if (millis() > ts6 + 100)
        state6 = 609;
      break;
    case 609:
      digitalWrite(PFA2, HIGH);   // White Headlights
      digitalWrite(PFA1, LOW);    // White headlights 1
      digitalWrite(PFA3, LOW);    // Blue Flashing Lights 1
      digitalWrite(PFA4, HIGH);   // Blue Flashing Lights 2
      ts6 = millis();
      state6 = 610;
      break;
    case 610:
      if (millis() > ts6 + 100)
        state6 = 611;
      break;
    case 611:
      digitalWrite(PFA4, LOW);    // Blue Flashing Lights 2
      ts6 = millis();
      state6 = 612;
      break;
    case 612:
      if (millis() > ts6 + 100)
        state6 = 613;
      break;
    case 613:
      digitalWrite(PFA4, HIGH);   // Blue Flashing Lights 2
      ts6 = millis();
      state6 = 614;
    case 614:
      if (millis() > ts6 + 100)
        state6 = 615;
      break;
    case 615:
      digitalWrite(PFA4, LOW);    // Blue Flashing Lights 2
      ts6 = millis();
      state6 = 616;
      break;
    case 616:
      if (millis() > ts6 + 100)
        state6 = 601;
      break;
  }

  // YELLOW FLASHING LIGHTS

  static int state7 = 701;
  static unsigned long ts7;

  switch (state7)
  {
    case 701:
      digitalWrite(ELF2, LOW);    // Yellow flashing lights 2
      digitalWrite(ELF1, HIGH);   // Yellow flashing lights 1
      ts7 = millis();
      state7 = 702;
      break;
    case 702:
      if (millis() > ts7 + 300)
        state7 = 703;
      break;
    case 703:
      digitalWrite(ELF1, LOW);    // Yellow flashing lights 1
      digitalWrite(ELF2, HIGH);   // Yellow flashing lights 2
      ts7 = millis();
      state7 = 704;
      break;
    case 704:
      if (millis() > ts7 + 300)
        state7 = 701;
      break;
  }

  // ZEBRA CROSSING

  static int state8 = 801;
  static unsigned long ts8;

  switch (state8)
  {
    case 801:
      digitalWrite(PZC1, HIGH);   // Zebra crossing 1
      digitalWrite(PZC2, LOW);    // Zebra crossing 2
      ts8 = millis();
      state8 = 802;
      break;

    case 802:
      if (millis() > ts8 + 1000)
      {
        state8 = 803;
      }
      break;

    case 803:
      digitalWrite(PZC1, LOW);    // Zebra crossing 1
      digitalWrite(PZC2, HIGH);   // Zebra crossing 2
      ts8 = millis();
      state8 = 804;
      break;

    case 804:
      if (millis() > ts8 + 1000)
      {
        state8 = 801;
      }
      break;
  }

  // RAILWAY CROSSING GATE SERVO

  static int state9 = 901;
  static unsigned long ts9;

  switch (state9)
  {
    case 901:

      if (digitalRead (TR1) == HIGH)    // Train light Red
        state9 = 902;

      break;
    case 902:
      digitalWrite(SER, HIGH);    // Servo Crossing Gates
      delayMicroseconds(pulse);
      digitalWrite(SER, LOW);   // Servo Crossing Gates
      ts9 = millis();
      state9 = 903;
      break;

    case 903:
      if (millis() > ts9 + 60)
        state9 = 904;
      break;

    case 904:
      digitalWrite(SER, HIGH);    // Servo Crossing Gates
      delayMicroseconds(pulse);
      digitalWrite(SER, LOW);   // Servo Crossing Gates
      ts9 = millis();
      state9 = 905;
      break;

    case 905:
      if (millis() > ts9 + 60)
        state9 = 901;
      break;
  }
  static int state10 = 906;
  static unsigned long ts10;

  switch (state10)
  {
    case 906:

      if (digitalRead (TG1) == HIGH)    // Train light green
        state10 = 907;
      else
        state10 = 906;
      break;

    case 907:

      ts10 = millis();
      state10 = 908;
      break;

    case 908:
      if (millis() > ts10 + 1500)
        state10 = 909;
      break;

    case 909:
      digitalWrite(SER, HIGH);    // Servo Crossing Gates
      delayMicroseconds(pulse1);
      digitalWrite(SER, LOW);   // Servo Crossing Gates
      ts10 = millis();
      state10 = 910;
      break;

    case 910:
      if (millis() > ts10 + 60)
        state10 = 911;
      break;

    case 911:
      digitalWrite(SER, HIGH);    // Servo Crossing Gates
      delayMicroseconds(pulse1);
      digitalWrite(SER, LOW);   // Servo Crossing Gates
      ts10 = millis();
      state10 = 912;
      break;

    case 912:
      if (millis() > ts10 + 60)

        if (digitalRead (TG1) == LOW)   // Train light green
          state10 = 906;
        else
          state10 = 909;

      break;
  }
}



when it is finish it will control my town here is a video of early stages of the project https://www.youtube.com/watch?v=eVsJr6-w5-0
Image
I'm sorry about the rats nest of wires.
Last edited by MikeTrymTrains on Mon Jul 09, 2018 3:38 pm, edited 2 times in total.

MikeTrymTrains
Posts: 14
Joined: Mon Jul 02, 2018 9:58 pm

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby MikeTrymTrains » Sat Jul 07, 2018 4:21 pm

This Arduino code simulate a railway crossing

Code: Select all

const int TG1 = 12;   // Train light green
const int TR1 = 11;   // Train light Red
const int AC1 = 0;    // Amber crossing leds
const int RC1 = 1;    // Red crossing leds right
const int RC2 = 2;    // Red crossing leds left
const int SER = 6;    // Servo Crossing Gates
const int SW1 = 5;    // Switch for railway crossing
const int TONE = 9;    // Railway crossing transister to speaker toy

int pulse = 500;
int pulse1 = 1150;
int state5 = LOW;
int reading;
int previous = LOW;
long time = 0;
long debounce = 10;
int ledState5;
long debounceDelay1 = 50;


void setup()
{
  pinMode(SER, OUTPUT);    // Servo Crossing Gates
  pinMode(SW1, INPUT);     // Switch for railway crossing
  pinMode(TONE, OUTPUT);    // Railway crossing transister to speaker toy
  pinMode(AC1, OUTPUT);    // Amber crossing leds
  pinMode(RC1, OUTPUT);    // Red crossing leds right
  pinMode(RC2, OUTPUT);    // Red crossing leds left
  pinMode(TG1, OUTPUT);
  pinMode(TR1, OUTPUT);
  ledState5 = LOW;
}

void loop() {

 
  // RAILWAY CROSSING SWITCH

  reading = digitalRead(SW1);   // Switch for railway crossing
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state5 == HIGH)
      state5 = LOW;
    else
      state5 = HIGH;

    time = millis();
  }

  digitalWrite(TG1, state5);    // Train light green

  previous = reading;

  if (digitalRead(TG1) == HIGH)   // Train light green
    digitalWrite(TR1, LOW);   // Train light Red
  else
    digitalWrite(TR1, HIGH);    // Train light Red


  // RAILWAY CROSSING

  static int state3 = 301;
  static unsigned long ts3;

  switch (state3)
  {
    case 301:
      if (digitalRead(TG1) == HIGH)   // Train light green
        state3 = 302;
      break;
    case 302:
      digitalWrite(AC1, HIGH);    // Amber crossing leds
      ts3 = millis();
      state3 = 303;
      break;
    case 303:
      if (millis() > ts3 + 250)
        state3 = 304;
      break;
    case 304:
      ts3 = millis();
      state3 = 305;
      break;
    case 305:
      if (millis() > ts3 + 250)
        state3 = 306;
      break;
    case 306:
      digitalWrite(AC1, LOW);   // Amber crossing leds
      ts3 = millis();
      state3 = 307;
      break;
    case 307:
      if (millis() > ts3 + 250)
        state3 = 308;
      break;
    case 308:
      digitalWrite(RC1, HIGH);    // Red crossing leds right
      digitalWrite(RC2, HIGH);    // Red crossing leds left
      ts3 = millis();
      state3 = 309;
      break;
    case 309:
      if (millis() > ts3 + 250)
        state3 = 310;
      break;
    case 310:
      digitalWrite(RC1, LOW);   // Red crossing leds right
      digitalWrite(RC2, LOW);   // Red crossing leds left
      ts3 = millis();
      state3 = 311;
      break;
    case 311:
      if (millis() > ts3 + 250)
        state3 = 312;
      break;
    case 312:
      digitalWrite(RC1, HIGH);    // Red crossing leds right
      digitalWrite(RC2, HIGH);    // Red crossing leds left
      ts3 = millis();
      state3 = 313;
      break;
    case 313:
      if (millis() > ts3 + 250)
        state3 = 314;
      break;
    case 314:
      digitalWrite(RC1, LOW);   // Red crossing leds right
      digitalWrite(RC2, LOW);   // Red crossing leds left
      ts3 = millis();
      state3 = 315;
      break;
    case 315:
      if (millis() > ts3 + 250)
        state3 = 316;
      break;
    case 316:
      digitalWrite(RC1, HIGH);    // Red crossing leds right
      digitalWrite(RC2, LOW);   // Red crossing leds left
      ts3 = millis();
      state3 = 317;
      break;
    case 317:
      if (millis() > ts3 + 250)
        state3 = 318;
      break;
    case 318:
      digitalWrite(RC1, LOW);   // Red crossing leds right
      digitalWrite(RC2, HIGH);    // Red crossing leds left
      ts3 = millis();
      state3 = 319;
      break;
    case 319:
      if (millis() > ts3 + 250)
        state3 = 320;
      break;
    case 320:
      if (digitalRead(TG1) == LOW)    // Train light green
        state3 = 321;
      else
        state3 = 316;
      break;
    case 321:
      digitalWrite(RC2, LOW);   // Red crossing leds left
      digitalWrite(TG1, LOW);   // Train light green
      ts3 = millis();
      state3 = 301;
      break;
  }
// Tone
  static int state2 = 201;
  static unsigned long ts2;
  if (digitalRead(TG1) == LOW)    // Green led pedestrian crossing
    state2 =  201;
  switch (state2)
  {
    case 201:
      noTone(TONE);
      if (digitalRead(TG1) == HIGH)   // Green led pedestrian crossing
        state2 = 202;
      break;
    case 202:
      tone(TONE, 2000);
      ts2 = millis();
      state2 = 203;
      break;
    case 203:
      if (millis() > ts2 + 250)
        state2 = 204;
      break;
    case 204:
      tone(TONE, 1500);
      ts2 = millis();
      state2 = 205;
      break;
    case 205:
      if (millis() > ts2 + 250)
        state2 = 202;
      break;
  }
  // RAILWAY CROSSING GATE SERVO

  static int state9 = 901;
  static unsigned long ts9;

  switch (state9)
  {
    case 901:

      if (digitalRead (TR1) == HIGH)    // Train light Red
        state9 = 902;

      break;
    case 902:
      digitalWrite(SER, HIGH);    // Servo Crossing Gates
      delayMicroseconds(pulse);
      digitalWrite(SER, LOW);   // Servo Crossing Gates
      ts9 = millis();
      state9 = 903;
      break;

    case 903:
      if (millis() > ts9 + 60)
        state9 = 904;
      break;

    case 904:
      digitalWrite(SER, HIGH);    // Servo Crossing Gates
      delayMicroseconds(pulse);
      digitalWrite(SER, LOW);   // Servo Crossing Gates
      ts9 = millis();
      state9 = 905;
      break;

    case 905:
      if (millis() > ts9 + 60)
        state9 = 901;
      break;
  }
  static int state10 = 906;
  static unsigned long ts10;

  switch (state10)
  {
    case 906:

      if (digitalRead (TG1) == HIGH)    // Train light green
        state10 = 907;
      else
        state10 = 906;
      break;

    case 907:

      ts10 = millis();
      state10 = 908;
      break;

    case 908:
      if (millis() > ts10 + 1500)
        state10 = 909;
      break;

    case 909:
      digitalWrite(SER, HIGH);    // Servo Crossing Gates
      delayMicroseconds(pulse1);
      digitalWrite(SER, LOW);   // Servo Crossing Gates
      ts10 = millis();
      state10 = 910;
      break;

    case 910:
      if (millis() > ts10 + 60)
        state10 = 911;
      break;

    case 911:
      digitalWrite(SER, HIGH);    // Servo Crossing Gates
      delayMicroseconds(pulse1);
      digitalWrite(SER, LOW);   // Servo Crossing Gates
      ts10 = millis();
      state10 = 912;
      break;

    case 912:
      if (millis() > ts10 + 60)

        if (digitalRead (TG1) == LOW)   // Train light green
          state10 = 906;
        else
          state10 = 909;

      break;
  }
}




Image
Last edited by MikeTrymTrains on Mon Jul 09, 2018 3:32 pm, edited 1 time in total.

MikeTrymTrains
Posts: 14
Joined: Mon Jul 02, 2018 9:58 pm

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby MikeTrymTrains » Sat Jul 07, 2018 4:29 pm

This Arduino code move a train forward and backwards with a relay.

Code: Select all

const int TFB = 0;   // Train forward and backward
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 45000;

void setup()
{

  pinMode(TFB, OUTPUT);    // Train forward and backward
}

void loop() {

  // TRAIN FORWARDS AND BACKWARDS

  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {

    previousMillis = currentMillis;

    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    digitalWrite(TFB , ledState); // Train forward and backward
  }
}




Image
Last edited by MikeTrymTrains on Mon Jul 09, 2018 3:35 pm, edited 1 time in total.

User avatar
TimberSurf
Posts: 2103
Joined: Wed Jan 08, 2014 5:47 pm
Location: N.Wales
Contact:

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby TimberSurf » Sat Jul 07, 2018 8:19 pm

Mike, first welcome to the forum! :D
Thanks for posting your code, it is very interesting to see how others write. I have checked out the small programs and they work fine.
Your main program will not compile. Not sure if it was just for info or if it is supposed to work?


I am no expert at Arduino, but I am proficient in others and most importantly understand systems and logic as a whole.
We all tend to learn an aspect of programming and get it to work, tis human, but the fact of the matter is that in programming there is often six ways of doing the same thing, of which none of them is 'wrong'! (makes looking at others codeing a nightmare!). But sometimes, one style (using one command for all) is just not viable (or at worst, much harder).
Looking at your video, did you fix the problem?
I feel the issue with the road/rail junction needs some logic, that "case" may not be the best answer. I think it could be done, but it will get very complicated.
In essence, you are using "case" sequentially, this is called a "Drum Sequencer", it basically can stop, but can only roll round and ultimately return to its start point of its circumference. For the lighting circuits, this works well. I wrote the same style but just using a memory number rather than 'State' or 'case'. for your junction, you need to jump out of the normal sequence and run through an alternate program for that condition, this is known as jumping. How you do it....there are six different ways! You could use boolean and if statements, case, state, program jumps, loops, all sorts of ways!
But all will involve mastering some boolean logic. Don't despair, this just means you need to learn how to combine conditions and get a True/false result.

I keep this list of arduino commands to hand to help me:-

and &&
and_eq &=
bitand &
bitor |
not !
not_eq !=
or ||
or_eq |=
xor ^
xor_eq ^=

You can then combine conditions {is train coming and is truck left coming or truck_right_coming}

to:-

If then else logic
like below:-

If train_coming && (truck_left_coming ||truck_right_coming)
then
Stop_both_trucks
else
allow_both_trucks

Obviously simplified but it should give you some idea's
Image
Lumsdonia <--- Hit link to go to my website for full story and wiring advice!

User avatar
TimberSurf
Posts: 2103
Joined: Wed Jan 08, 2014 5:47 pm
Location: N.Wales
Contact:

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby TimberSurf » Sat Jul 07, 2018 8:22 pm

And for a quick comparison, here is my version of a 4 way traffic light

Code: Select all

//Traffic Lights


//Declaring the LED and button pins and initial state
int EWgreen = 2;
int EWyellow = 3;
int EWred = 4;
int NSgreen = 5;
int NSyellow = 6;
int NSred = 7;
int state = 0;

int Onesec = 1000;

void setup()
{
   //Declaring the functioning of LED and Button
   pinMode(EWgreen, OUTPUT); 
   pinMode(EWyellow, OUTPUT);
   pinMode(EWred, OUTPUT);
   pinMode(NSgreen, OUTPUT); 
   pinMode(NSyellow, OUTPUT);
   pinMode(NSred, OUTPUT);
}

void loop()
{
   if (state == 1)    //EW Green,  NS Red 
   {
      digitalWrite(EWgreen,HIGH);
      digitalWrite(EWyellow,LOW);
      digitalWrite(EWred,LOW);
      digitalWrite(NSred,HIGH);
      digitalWrite(NSyellow,LOW);
      digitalWrite(NSgreen,LOW);
   }
   else if (state == 11)    //EW Yellow,  NS Red
   {
      digitalWrite(EWgreen,LOW);
      digitalWrite(EWyellow,HIGH);
      digitalWrite(EWred,LOW);
      digitalWrite(NSred,HIGH);
      digitalWrite(NSyellow,LOW);
      digitalWrite(NSgreen,LOW);
   }
   else if (state == 14)    //EW Red,  NS Red
   {
      digitalWrite(EWgreen,LOW);
      digitalWrite(EWyellow,LOW);
      digitalWrite(EWred,HIGH);
      digitalWrite(NSred,HIGH);
      digitalWrite(NSyellow,LOW);
      digitalWrite(NSgreen,LOW);
   }
   else if (state == 16)    //EW Red,  NS Red/Amber
   {
      digitalWrite(EWgreen,LOW);
      digitalWrite(EWyellow,LOW);
      digitalWrite(EWred,HIGH);
      digitalWrite(NSred,HIGH);
      digitalWrite(NSyellow,HIGH);
      digitalWrite(NSgreen,LOW);
   }
   else if (state == 18)    //EW Red,  NS Green
   {
      digitalWrite(EWgreen,LOW);
      digitalWrite(EWyellow,LOW);
      digitalWrite(EWred,HIGH);
      digitalWrite(NSred,LOW);
      digitalWrite(NSyellow,LOW);
      digitalWrite(NSgreen,HIGH);
   }
   else if (state == 22)    //EW Red,  NS Yellow
   {
      digitalWrite(EWgreen,LOW);
      digitalWrite(EWyellow,LOW);
      digitalWrite(EWred,HIGH);
      digitalWrite(NSred,LOW);
      digitalWrite(NSyellow,HIGH);
      digitalWrite(NSgreen,LOW);
   }
   else if (state == 25)    //EW Red,  NS Red
   {
      digitalWrite(EWgreen,LOW);
      digitalWrite(EWyellow,LOW);
      digitalWrite(EWred,HIGH);
      digitalWrite(NSred,HIGH);
      digitalWrite(NSyellow,LOW);
      digitalWrite(NSgreen,LOW);
   }
   else if (state == 27)    //EW Red/Amber,  NS Red
   {
      digitalWrite(EWgreen,LOW);
      digitalWrite(EWyellow,HIGH);
      digitalWrite(EWred,HIGH);
      digitalWrite(NSred,HIGH);
      digitalWrite(NSyellow,LOW);
      digitalWrite(NSgreen,LOW);
   }
   
   if (state == 28)  {
      state = 0;
   }
   {
      delay(Onesec); //the delay can be changed to 2 seconds,3 seconds,etc.
   }
   {
      state = state + 1;
   }
}



Note the difference in length of code!
The reason mainly being that I "loop out" to increment the time, rather than doing it in each state and that the states are effectively the time!
Image
Lumsdonia <--- Hit link to go to my website for full story and wiring advice!

User avatar
TimberSurf
Posts: 2103
Joined: Wed Jan 08, 2014 5:47 pm
Location: N.Wales
Contact:

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby TimberSurf » Sat Jul 07, 2018 8:33 pm

If I could be bothered, the shortest code would be to write the sequence in binary, loop a mask, with a loop for time constants.
You could probably get it down to about 30 lines! (yours is 200+, mine is 100+)
Image
Lumsdonia <--- Hit link to go to my website for full story and wiring advice!

MikeTrymTrains
Posts: 14
Joined: Mon Jul 02, 2018 9:58 pm

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby MikeTrymTrains » Sun Jul 08, 2018 12:25 am

Hi TimberSurf I tried the code from this forum and it work on my mega so i don't know why it won't compile. The problem I had in the video was when I run the magnet over the reed switch it would lift the bread board and a wire came loose. I'm not a programmer I search around the web to find code and modify it to work together with other code. The main code I use is the finite state machine here is a link to the website where I found it. https://hackingmajenkoblog.wordpress.com/2016/02/01/the-finite-state-machine/ I like the look of your code I try it but it has to many delay's in it. It's ok if you just want to run the traffic lights. If you add other codes to it you're other code get blocks by it. I don't know enough about millis to be able to add it to your code to remove the delay. Like others I went to the Arduino forums for help but all you get is learn blink without delay it's not helpful so i thought I will put up my code to help others. Most of my codes run together without being block by delay just change the pin numbers. I'm not very good a explaining things so I hope you understand me. Many thank Mike.

User avatar
TimberSurf
Posts: 2103
Joined: Wed Jan 08, 2014 5:47 pm
Location: N.Wales
Contact:

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby TimberSurf » Sun Jul 08, 2018 10:24 am

Mike, you are absolutely right, delay is really no good, if it can affect other programs, I just cobbled that program together as a stand alone for the 4 way lights. (I will have dozens of individual Arduino's on my layout eventually). I have replaced the delay function so that you can combine it with other loops.

Code: Select all

//Two direction Traffic Lights

unsigned long Tcurrent = 0;    // stores the value of millis() in each iteration of loop() this is equivalent to noting the time from a clock
unsigned long Tlapseold = 0; // the time when the last state was set
unsigned long Tlapse = 1000; // the time between each state change

//Declaring the LED and button pins (and initial state)
int EWgreen = 2;
int EWyellow = 3;
int EWred = 4;
int NSgreen = 5;
int NSyellow = 6;
int NSred = 7;
int state = 0;

void setup()
{
   //Declaring the functioning of LED and Button
   pinMode(EWgreen, OUTPUT); 
   pinMode(EWyellow, OUTPUT);
   pinMode(EWred, OUTPUT);
   pinMode(NSgreen, OUTPUT); 
   pinMode(NSyellow, OUTPUT);
   pinMode(NSred, OUTPUT);
}

void loop(){
Tcurrent = millis();   // capture the latest value of millis()
  if (Tcurrent - Tlapseold >= Tlapse) {  // check if the lapse time has elasped
    Tlapseold = Tcurrent;  // reset the last time the state as changed
    state = state + 1;  //increment the state by 1
  }

   if (state == 1)    //EW Green,  NS Red 
   {
      digitalWrite(EWgreen,HIGH);
      digitalWrite(EWyellow,LOW);
      digitalWrite(EWred,LOW);
      digitalWrite(NSred,HIGH);
      digitalWrite(NSyellow,LOW);
      digitalWrite(NSgreen,LOW);
   }
   else if (state == 11)    //EW Yellow,  NS Red
   {
      digitalWrite(EWgreen,LOW);
      digitalWrite(EWyellow,HIGH);
      digitalWrite(EWred,LOW);
      digitalWrite(NSred,HIGH);
      digitalWrite(NSyellow,LOW);
      digitalWrite(NSgreen,LOW);
   }
   else if (state == 14)    //EW Red,  NS Red
   {
      digitalWrite(EWgreen,LOW);
      digitalWrite(EWyellow,LOW);
      digitalWrite(EWred,HIGH);
      digitalWrite(NSred,HIGH);
      digitalWrite(NSyellow,LOW);
      digitalWrite(NSgreen,LOW);
   }
   else if (state == 16)    //EW Red,  NS Red/Amber
   {
      digitalWrite(EWgreen,LOW);
      digitalWrite(EWyellow,LOW);
      digitalWrite(EWred,HIGH);
      digitalWrite(NSred,HIGH);
      digitalWrite(NSyellow,HIGH);
      digitalWrite(NSgreen,LOW);
   }
   else if (state == 18)    //EW Red,  NS Green
   {
      digitalWrite(EWgreen,LOW);
      digitalWrite(EWyellow,LOW);
      digitalWrite(EWred,HIGH);
      digitalWrite(NSred,LOW);
      digitalWrite(NSyellow,LOW);
      digitalWrite(NSgreen,HIGH);
   }
   else if (state == 22)    //EW Red,  NS Yellow
   {
      digitalWrite(EWgreen,LOW);
      digitalWrite(EWyellow,LOW);
      digitalWrite(EWred,HIGH);
      digitalWrite(NSred,LOW);
      digitalWrite(NSyellow,HIGH);
      digitalWrite(NSgreen,LOW);
   }
   else if (state == 25)    //EW Red,  NS Red
   {
      digitalWrite(EWgreen,LOW);
      digitalWrite(EWyellow,LOW);
      digitalWrite(EWred,HIGH);
      digitalWrite(NSred,HIGH);
      digitalWrite(NSyellow,LOW);
      digitalWrite(NSgreen,LOW);
   }
   else if (state == 27)    //EW Red/Amber,  NS Red
   {
      digitalWrite(EWgreen,LOW);
      digitalWrite(EWyellow,HIGH);
      digitalWrite(EWred,HIGH);
      digitalWrite(NSred,HIGH);
      digitalWrite(NSyellow,LOW);
      digitalWrite(NSgreen,LOW);
   }
   
   if (state == 28)  {
      state = 0;
   }
}


I looked into doing it in binary, but it would just make it impossible to understand without having a deep understanding of boolean, better to leave it more human so beginners can understand and fiddle with it, it only takes up 3% of flash, so not a problem.
Image
Lumsdonia <--- Hit link to go to my website for full story and wiring advice!


Return to “Electronics”

Who is online

Users browsing this forum: No registered users and 1 guest