Drawbridge Project, LEDS aren't turning on.

Hello, I’m in my first basic engineering course in college and I’m just learning how to use arduino. I’m using an arduino uno processor, and I’m currently working on a small drawbridge for a project.

int buzzer = 8;
int prox = A5;
int pot = A4;
int presr = A3;
int lghtsen = A2;
int bigblue = 11;
int led1 = 6;
int led2 = 5;
int led3 = 3;
int potval = 0;
int lghtval = 0;
int prxval = 0;
int presrval = 0;
#include <Servo.h>
Servo serv1;
Servo serv2;
int pos = 0;


void setup(){
  pinMode(buzzer, OUTPUT);
  pinMode(prox, INPUT);
  pinMode(pot, INPUT);
  pinMode(presr, INPUT);
  serv1.attach(9);
  serv2.attach(10);
  pinMode(lghtsen, INPUT);
  pinMode(bigblue, OUTPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
}

void loop(){
  potval=analogRead(pot);
  lghtval=analogRead(lghtsen);
  prxval=analogRead(prox);
  presrval=analogRead(presr);
  
  digitalWrite(led1, HIGH);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(bigblue, LOW);
  digitalWrite(buzzer, LOW);
  
  if (lghtval>500 && prxval>60 && presrval<300){
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, LOW);
    delay(5000);
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, HIGH);
    tone(buzzer, 300);
    digitalWrite(bigblue, HIGH);
    if (potval<420){
      for(pos = 0; pos<60; pos+= 1){
        serv1.write(pos);
        serv2.write(pos);
        delay(20);
      }
      delay(4000);
      for(pos = 60; pos>=1; pos-=1){
        serv1.write(pos);
        serv2.write(pos);
        delay(20);
      }
    }
    if (potval>420 && potval<850){
      for(pos = 0; pos<90; pos+= 1){
        serv1.write(pos);
        serv2.write(pos);
        delay(20);
      }
      delay(4000);
      for(pos = 90; pos>=1; pos-=1){
        serv1.write(pos);
        serv2.write(pos);
        delay(20);
      }
    } 
    if (potval>850){
      for(pos = 0; pos<180; pos+= 1){
        serv1.write(pos);
        serv2.write(pos);
        delay(20);
      }
      delay(4000);
      for(pos = 180; pos>=1; pos-=1){
        serv1.write(pos);
        serv2.write(pos);
        delay(20);
      }
    }
  if (lghtval<500 && prxval>60 && presrval<300){
    delay(7000);
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, LOW);
    delay(5000);
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, HIGH);
    tone(buzzer, 300);
    digitalWrite(bigblue, HIGH);
    if (potval<420){
      for(pos = 0; pos<60; pos+= 1){
        serv1.write(pos);
        serv2.write(pos);
        delay(20);
      }
      delay(4000);
      for(pos = 60; pos>=1; pos-=1){
        serv1.write(pos);
        serv2.write(pos);
        delay(20);
      }
    }
    if (potval>420 && potval<850){
      for(pos = 0; pos<90; pos+= 1){
        serv1.write(pos);
        serv2.write(pos);
        delay(20);
      }
      delay(4000);
      for(pos = 90; pos>=1; pos-=1){
        serv1.write(pos);
        serv2.write(pos);
        delay(20);
      }
    } 
    if (potval>850){
      for(pos = 0; pos<180; pos+= 1){
        serv1.write(pos);
        serv2.write(pos);
        delay(20);
      }
      delay(4000);
      for(pos = 180; pos>=1; pos-=1){
        serv1.write(pos);
        serv2.write(pos);
        delay(20);
      }
    }
  }
  }}

Basically for the code, when an object (a “boat”, since it’s a drawbridge) gets close enough to the distance sensor and there is no “car” on the bridge (making the pressure value under a certain amount) the bridge should raise. I have it so that when it’s “daytime” (based off light sensor) the bridge will wait before opening to let more traffic through, and then at “night” when there is less traffic, the bridge will automatically open. The potentiometer controls how high the bridges rises.

The problem I have is that when the bridge rises the buzzer and blue light ( in code as bigblue) should turn on. Also the 3 leds (led1, led2, led3) are supposed to act as traffic lights. My problem is that none of these outputs are working. Only my two servos are actually changing when the bridge raises.

Is it a problem in my code? Or is it a power issue? I figure if it was a power issue then the servos wouldn’t work either. Of course I’m very new to both arduino and dealing with electronics in general. Any help would truly be appreciated. I hope the answer is simple to more experienced users.

  pinMode(prox, INPUT);
  pinMode(pot, INPUT);

You can't set the mode of a read-only analog pin.

Why not write a simple sketch that simply turns the LED pins on? Then, if the LEDs light up, they are wired correctly. If not, they aren't.

Hi, to make you code easier to read, in the IDE select Tool then AutoFormat, or press control-T. This will place you { and } and if and else statements in a tab regemented order so you can see your for and if groups.

Good work if its your first attempt, but as PaulS has hinted, develop you sketch as you go, get the night/day working then the drawbigde, then the LED's, this way you solve problems as you go. So any you encounter has to have originated since your last good code. We have had Final Year Uni students on here and they haven't even got this far before asking for help.

Tom.. :) Looks like you read how to use the forum before posting. You will get a karma from me.

as I see this, you have a down stream sensor to sense the presence of a ship once that is tripped, you operate the traffic lights, then raise the drawbridge then wait until the boat has passed the upstream sensors to verify it has passed then lower the bridge then change the traffic lights.

each scan you turn off the lights.

I would set them all off in void setup or when you initialize them.

then use

if(boat detected){ light lights raise bridge lower bridge change lights } else { turn lights off }

that way they are only turned on or off by the other actions.

also, since you have two copies of your routine and the only difference is the light level, you can change your primary IF

from:

if (lghtval<500 && prxval>60 && presrval<300){
delay(7000);

to :

if (prxval>60 && presrval<300){
if (lghtval<500 ){
delay(7000);
}

that way, you have one section of code and only if lghtval is over 500 will it run that delay. if it is under, it will not run the delay.

Thanks for the advice, I'll definitely try to double check that my LEDs are programmed correctly, to see if it does happen to just be a problem of me setting up the hardware incorrectly.