Need help writing code for my Uno

I have never used an uno before my Uno rev 3 is powered by 12 volts through the barrel jack

i have I disconnected the speaker (a 8 ohm 2 w speaker) and put the leads input to my uno board on pin 10 input. ( Its part of an old 5 volt wireless driveway alarm with tone alarm )

I have 2 high intensity green led light bars, connected on a covvy dual relay board srd-05vdc-sl-c powered by 12 volts.( low voltage inputs). one connected to pin 11 the other to pin 12.

when an input is sent to pin 10 i would like the code to randomly decide between light 1 or light 2 keep the light on and flashing for 4 seconds. then wait for the next input trigger. then randomly decide between light 1 or light 2 keep that light on flashing for 4 seconds
example 1 speaker beeps (random output pin selection )and maybe green light on the left turns on and flashes for 4 seconds, i go through door on the left. next person speaker beeps (random selection) and green light on the right turns on and flashes for 4 seconds, that person goes through door on the right.

i am trying to use the internal count timer with a delay to select an number that is even or odd making the sequence seem random. as there are only 2 choices it does not need to be highly accurate even alternating will work if random is not achievable. i so far cant get it to work.

any thought ideas would be greatly appreciated.

My first thought is for you to get one thing working first. I suggest getting the speaker to do what you want. And then move to another feature.

Paul

I'm going to step in here as an ex-newbie and applaud what Paul said.

Whenever I start building anything, I first exercise each small part in isolation. Like "I have an LED on pin 8, I'll make sure it turns on and off; I have a piezo noisemaker on pin Whatever, I'll make sure I can get a buzz out of it." Stuff that simple (a pot, some LEDs, a buzzer) I might test with one sketch. But if I add more complicated gizmos (breakout boards or whatever), then I write a sketch for each one, exercising just that device. This gets important later when you build more complex projects.

Once you have N sketches, each one verifying that a part of your project works, you can start pasting code from those N sketches into the "big sketch" where you make all the parts play together.

This, anyhow, is the best way I have found to verify my components as I go, and be able to "roll back" instantly to simple sanity-checking of the separate bits. For example, say I have three i2c devices in my project, each one has its own sketch that does a simple "are you alive and sane" test of the device. If they start behaving strangely as an ensemble, I can immediately revert to these simple sketches to check each device separately. Often the windows for these test sketches are up on the desktop the whole time I'm working on the main project.

Hope this helps. Disk space is cheap (these days) and life hours are valuable. It's worth writing 10 sketches for one project if it speeds up debugging and testing.

davidwr:
my Uno rev 3 is powered by 12 volts through the barrel jack...

Could be mistake (learning opportunity) #1.
An Uno board runs on 5volt, and is most efficiently powered with 5volt on the USB socked or 5volt pin (PC/laptop or cellphone charger),
or with 7-12volt on the DC socket. The DC socket is the least preferred option, and 12volt being (practically) absolute max, because it generates the most heat when (power) users are connected to the Arduino.

davidwr:
I disconnected the speaker (a 8 ohm 2 w speaker) and put the leads input to my uno board on pin 10 input.

Mistake #2.
8 ohms is practically a short circuit for the pins of an Arduino (~125 ohms absolute minimum).
8 ohms could in theory draw 5volt/8ohm= 0.65A = 650mA from a pin (absolute max is 40mA).
You need an audio amplifier between pin and speaker.

davidwr:
I have never used an uno before

Wise to go through some of the basic examples that come with the IDE, before you start a complicated project.
Also read the "How to post" sticky that you can find on top of every main page.
We can't give you much help without all the details.

davidwr:
i am trying to use the internal count timer with a delay to select an number that is even or odd making the sequence seem random. as there are only 2 choices it does not need to be highly accurate even alternating will work if random is not achievable.

Seems you need the random() function.
Leo..

Tazling:
I'm going to step in here as an ex-newbie and applaud what Paul said.

Whenever I start building anything, I first exercise each small part in isolation. Like "I have an LED on pin 8, I'll make sure it turns on and off; I have a piezo noisemaker on pin Whatever, I'll make sure I can get a buzz out of it." Stuff that simple (a pot, some LEDs, a buzzer) I might test with one sketch. But if I add more complicated gizmos (breakout boards or whatever), then I write a sketch for each one, exercising just that device. This gets important later when you build more complex projects.

Once you have N sketches, each one verifying that a part of your project works, you can start pasting code from those N sketches into the "big sketch" where you make all the parts play together.

This, anyhow, is the best way I have found to verify my components as I go, and be able to "roll back" instantly to simple sanity-checking of the separate bits. For example, say I have three i2c devices in my project, each one has its own sketch that does a simple "are you alive and sane" test of the device. If they start behaving strangely as an ensemble, I can immediately revert to these simple sketches to check each device separately. Often the windows for these test sketches are up on the desktop the whole time I'm working on the main project.

Hope this helps. Disk space is cheap (these days) and life hours are valuable. It's worth writing 10 sketches for one project if it speeds up debugging and testing.

I find tabs in the IDE to be quite useful for this. Using tabs keeps the new function in development walled off from the main program. Most of the time.

Hope i am inserting my new code properly here. Thanks for all the input so far my 12 battery supplies 5 volts to the arduino uno barrel plug and the 5volt pin is the power source for my driveway alarm sensor.
the code seems long but does flash the led directional lights only when sensorval =1 as i have no idea yet how to use the speaker to signal the sensorval.

int sensor;              //speaker input pin dont know how to read it yet???
volatile byte sensorval;    // in case i need to store a value to use for signalState HIGH or LOW
int alternate;                 //stored value for random number used to temporarily signal relay if statement
volatile byte signalState;// Save the signal state from sensor input (currently unknow)
int randNumber;            //random number for my 2 choices which signal which led to blink
int relay = 12;              // Left Led Blink
int relay2 = 11;            // Right Led Blink
int val = 500;              // delay time


// Timer Variables
long lastDebounceTime = 0; // may need debounce on sensor signal unknown value
long debounceDelay = 4000;

void setup() {
  randomSeed(analogRead(A0)); // random number
  Serial.begin(9600);
  //pinMode(PsInterrupt, INPUT);// Ps motion sensor set as an input
  //digitalWrite(PsInterrupt, LOW);
  pinMode(relay, OUTPUT);     // Output to Left Led
  pinMode(relay2, OUTPUT);    // Output to Right Led
  digitalWrite(relay, HIGH);  // turn LED off
  digitalWrite(relay2, HIGH); // turn LED off
}
void loop() {
  int sensorval = 0; // manual test of if statements as sensor input not declared
  if (sensorval == 1)
  { randNumber = random(1, 3);//generate number 1 or 2
    int alternate = (randNumber);
    Serial.print("alternatestart:= ");
    Serial.println(alternate);
    if ((sensorval == 1 ) && (alternate == 1))
    { Serial.println("left: ");
      Serial.print("Sensorval:= ");
      Serial.println(sensorval);
      delay(1300);
      Serial.print("alternateleft:= ");
      Serial.println(alternate);
      delay(val);
      digitalWrite(relay, LOW);
      delay(val);
      digitalWrite(relay, HIGH);
      delay(val);
      digitalWrite(relay, LOW);
      delay(val);
      digitalWrite(relay, HIGH);
      delay(val);
      digitalWrite(relay, LOW);
      delay(val);
      digitalWrite(relay, HIGH);
      delay(val);
      digitalWrite(relay, LOW);
      delay(val);
      digitalWrite(relay, HIGH);
      delay(val);
      digitalWrite(relay, LOW);
      delay(val);
      digitalWrite(relay, HIGH);
      delay(val);
      int sensorval = 0; // turns sensor value off at the test end
    }  else if ((sensorval == 1) && (alternate == 2))
    { //Serial.println("right: ");
      Serial.print("Sensorval:= ");
       Serial.println(sensorval);
       Serial.print("alternateright:= ");
      Serial.println(alternate);
      Serial.println();
       delay(val);
      digitalWrite(relay2, LOW);
      delay(val);
      digitalWrite(relay2, HIGH);
      delay(val);
      digitalWrite(relay2, LOW);
      delay(val);
      digitalWrite(relay2, HIGH);
      delay(val);
      digitalWrite(relay2, LOW);
      delay(val);
      digitalWrite(relay2, HIGH);
      delay(val);
      digitalWrite(relay2, LOW);
      delay(val);
      digitalWrite(relay2, HIGH);
      delay(val);
      digitalWrite(relay2, LOW);
      delay(val);
      digitalWrite(relay2, HIGH);
      delay(val);
 int sensorval = 0; // turns sensor value off at the test end
    }
  }
}

the speaker (a 8 ohm 2 w speaker) and put the leads input to my uno board on pin 10 ...
...when an input is sent to pin 10 ...

I’m not sure what’s going on here.
Do you want the speaker to act as a microphone (input) on pin 10 ?

What’s the goal of the whole project ?

Hi,
Welcome to the forum.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile: