Can anyone help me

Hey guys I was wondering if anyone could help me out with my program since I'm new at Arduino programming.

I have 4 targets on a hockey net. There is an LED beside each target and a vibration sensor on the back of each target.

So I have 4 outputs and 4 inputs.

The outputs are LEDs and the inputs are vibration sensors.

Outputs
Pin 12 is the top left LED
Pin 11 is the top right LED
Pin 10 is the bottom left LED
Pin 9 is the bottom right LED

Inputs
Pin 8 is the top left vibration sensor
Pin 7 is the top right vibration sensor
Pin 6 is the bottom left vibration sensor
Pin 5 is the bottom right vibration sensor

Here's what I'm looking to do.

Have 1 output go high for 4 seconds and while that output is high have the Arduino read the associated sensor (ex. when pin 11 is high, read pin 7).
If the Arduino does not receive a signal from pin 7 within the 4 seconds then pin 11 goes low. After 11 goes low there is a 4 second break until the next output goes high (ex. pin 9 high, read pin 5). Now if the Arduino receives a signal on 5 while 9 is high I want 9 to go low and then another 4 second break until the next output goes high (ex. 10 high, read 6).

Conclusion: Have random output on pin 12-9 go high for 4 seconds and have Arduino read associated sensor for 4 seconds. If no signal is received then output goes low, and there is a 4 second break then another output goes high. If the signal is received from sensor while associated LED is on then turn LED off and begin 4 second break and program continues to cycle the LEDs.

Any help is greatly appreciated.

A link to your vibration sensors might be useful. Most such sensors output a variable analog voltage proportional to the amount of vibration and are more suitable to read via the analog input pins rather then with digital input pins.

Lefty

Post your code.

Here's the vibration sensor.

Here's what I have so far, go easy on me I know it's going to be full of errors it's my first time.

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int LED TL = 12;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(LED TL, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(LED TL, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(4000);               // wait for a second
  digitalWrite(LED TL, LOW);    // turn the LED off by making the voltage LOW
  delay(4000);               // wait for a second
}

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int LED TR = 11;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(LED TR, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(LED TR, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(4000);               // wait for a second
  digitalWrite(LED TR, LOW);    // turn the LED off by making the voltage LOW
  delay(4000);               // wait for a second
}

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int LED BL = 10;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(LED BL, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(LED BL, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(4000);               // wait for a second
  digitalWrite(LED BL, LOW);    // turn the LED off by making the voltage LOW
  delay(4000);               // wait for a second
}

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int LED TL = 9;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(LED BR, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(LED BR, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(4000);               // wait for a second
  digitalWrite(LED BR, LOW);    // turn the LED off by making the voltage LOW
  delay(4000);               // wait for a second
}

Moderator edit: Code tags. Again. AWOL

You can't have multiple setup() and loop() functions. Here's a start:

int ledPins[] = { 12, 11, 10, 9 };
int vibrationSensorPins[] = { 8, 7, 6, 5 };

void setup()
{
  // TODO: Set the appropriate pins to INPUT/OUTPUT.
}

void loop()
{
  // TODO: Get a random number
  // Turn on an LED
  // Record the time
  // Block until 4 seconds elapsed, or sensor goes off
  // Turn off led
  // wait 4 seconds
}

Hi Weaver,
You say you are new to this (we all were at one time).
I assume you have the hardware, and can load programs into the arduion board. What board do you have ?
Suggestions:

  1. work with the system a while, testing, experimenting, finding out how it works, before starting a full sized project.
    load some of the examples and see how they function.
    make the leds turn on and off. Sense the motion detectors, and light an led when motion is detected.

If you can play with it for a couple weeks, you will be ready to start on a project.

Good Luck, Jack

Thanks for the example Arrch. I'll start with that then put my revised code up.

I have the uno board and the cord to connect it to my computer so I can put the program on the arduino.

I'm hoping to have this done in 2 weeks at the latest.

Ok guys I've made some progress, here's what I have.

TL = top left
TR = top right
BL = bottom left
BR= bottom right
VS = vibration sensor

int TL LED = 12;
int TR LED = 11;
int BL LED = 10;
int BR LED = 9;
int TL VS = 8;
int TR VS = 7;
int BL VS = 6;
int BR VS = 5;

void setup()
{
  pinMode(TL LED, OUTPUT);
  pinMode(TR LED, OUTPUT);
  pinMode(BL LED, OUTPUT);
  pinMode(BR LED, OUTPUT);
  pinMode(TL VS, INPUT);
  pinMode(TR VS, INPUT);
  pinMode(BL VS, INPUT);
  pinMode(BR VS, INPUT);
}

void loop()
{
 [color=red]?[/color] // TODO: Get a random number
 digitalWrite([color=orange]?[/color], HIGH); // Turn on an LED
 delay(4000) or [color=blue]?[/color]; // LED on for 4 seconds
 digitalWrite([color=orange]?[/color], LOW); // Turn off led
 delay(4000); // wait 4 seconds
}

I have a few questions, thus the "?" in my code, I colour coded my issues to help make it easier to understand.

I don't know how to randomly make a pin between 12-9 go high.

Not sure what to put here, but this should be solved once I figure out the red problem.

I don't know how to do an OR statement, (LED turns off after 4 seconds or if associated sensor sends signal. 12 & 8, 11 & 7, 10 & 6, 9 & 5).

Thanks for all the help guys.

There is a function call random(), you can find it in the reference page, on the arduino main site. (It is a pain in the butt to try and make links on an iPhone)

Nevermind, I have the arduino companion app. Ok, so you want to generate a random number for you LEDs, easy.

int RandNum = random(9, 12); // random(min, max);
digitalWrite(RandNum, HIGH);
/* look into blink without delay example*/
digitalWrite(RandNum, LOW);
/* ----------------------------------------------*/

Ok I'm still unsure how to have the random function randomly select a pin between 9-12. Could I use the array function?

Weaver14:
Ok I'm still unsure how to have the random function randomly select a pin between 9-12. Could I use the array function?

Did you not notice the random function accepts a min and max argument? It's also worth nothing that min is inclusive, while max is exclusive.

I don't know how to do an OR statement, (LED turns off after 4 seconds or if associated sensor sends signal. 12 & 8, 11 & 7, 10 & 6, 9 & 5).

You can't use delay() if you want to keep track of the sensor during that time. You need to use a while loop that has two conditions that keep it going based on the sensor reading AND the the time elapsed since the while loop has started. The BlinkWithoutDelay example demonstrates how to "record the current time".

Thanks arrch, I was confused when I looked at the example because one of the values was 299 which I thought was high.

But I just got it, but Hazard beat me to it! Thanks

Going to look into the blink without delay example then I'll get back to you guys, the helps much appreciated everyone!

Here's where I'm at now.

int TL LED = 12;
int TR LED = 11;
int BL LED = 10;
int BR LED = 9;
int TL VS = 8;
int TR VS = 7;
int BL VS = 6;
int BR VS = 5;
int RandNum = random(9, 12);
[color=red]const int TL LED = 12;
const int TR LED = 11;
const int BL LED = 10;
const int BR LED = 9;
const int TL VS = 8;
const int TR VS = 7;
const int BL VS = 6;
const int BR VS = 5;
int TL LEDState = LOW;
int TR LEDState = LOW;
int BL LEDState = LOW;
int BR LEDState = LOW;[/color]
long previousMillis = 0;
long interval = 4000;

void setup()
{
  pinMode(TL LED, OUTPUT);
  pinMode(TR LED, OUTPUT);
  pinMode(BL LED, OUTPUT);
  pinMode(BR LED, OUTPUT);
  pinMode(TL VS, INPUT);
  pinMode(TR VS, INPUT);
  pinMode(BL VS, INPUT);
  pinMode(BR VS, INPUT);
}

void loop()
{
 digitalWrite(RandNum, HIGH); // Turn on an LED between pin 9-12
 unsigned long currentMillis = millis ();
 if(currentMillis - previousMillis > interval) {
 previousMillis = currentMillis;
 if ([color=blue]ledState [/color]== LOW)
     [color=blue]ledState [/color]= HIGH;
    else
     ledState = LOW;
 digitalWrite([color=blue]ledPin[/color], [color=blue]ledState[/color]); // 
}

Not sure if this part is correct or even necessary.

Also I'm unsure about what to put here. Would RandNum replace ledPin and RandNumState replace ledState?

Weaver14:
Not sure if this part is correct or even necessary.

You can't have spaces inside variables.

Also I'm unsure about what to put here. Would RandNum replace ledPin and RandNumState replace ledState?

Are you trying to blink an LED? No? Then why did you just copy the blink without delay example into your code? It demonstrates the CONCEPTS of how to use millis(), it's not the exact code you need.

Overall, you should be using arrays, and getting a random number that indexes the array. That way, you can easily refer to both output and input pins. See my first post.

So you can link multiple pins to one variable like you did in your first post arrch?

like "int ledPins[] = { 12, 11, 10, 9 };"

And I'm not trying to blink an LED I'm trying to have it on for 4 seconds and then go off for 4 seconds then another one comes one. It goes off before 4 seconds if its sensor sends a signal. So I should be using array with parts of blinkwithoutdelay added into it?

This needs to be inside your loop() function.

int RandNum = random(9, 12);

And you should maybe have a while loop that looks to see the 4 second "on/off" has occured.

void loop() 
{
unsigned long previousMillis = millis (); // I change this line from your code

 while(done == false)
 {
   /* led on */
  if(millis() - previousMillis > interval) 
   {
     /* led off */
     done = true; // break out of while loop
   }
 } // EOW

 done = false; // reset state to reenter while loop
} // EOL

Untested, but just to give you an idea.

Isn't while loop just for analog signals? The vibration sensor is digital.

And do I need to do "int TLLED = 12;" and "const int TLLED = 12;" is this not repetitive?

I feel like I would be fine with just "int"? I could be wrong though.

Weaver14:
Isn't while loop just for analog signals? The vibration sensor is digital.

No...

And do I need to do "int TLLED = 12;" and "const int TLLED = 12;" is this not repetitive?

You can't have both

I feel like I would be fine with just "int"? I could be wrong though.

Pins don't (usually) change during execution, so using const is better practice.

Ok guys so here's where I'm at now

const int TlLED = 12;
const int TrLED = 11;
const int BlLED = 10;
const int BrLED = 9;
const int TlVS = 8;
const int TrVS = 7;
const int BlVS = 6;
const int BrVS = 5;
const int TlLEDState = LOW;
const int TrLEDState = LOW;
const int BlLEDState = LOW;
const int BrLEDState = LOW;
long previousMillis = 0;
long interval = 4000;
cons tint timer = 100;


void setup()
{
  pinMode(TlLED, OUTPUT);
  pinMode(TrLED, OUTPUT);
  pinMode(BlLED, OUTPUT);
  pinMode(BrLED, OUTPUT);
  pinMode(TlVS, INPUT);
  pinMode(TrVS, INPUT);
  pinMode(BlVS, INPUT);
  pinMode(BrVS, INPUT);
}

void loop()
{
 int RandNum = random(9, 12);
 digitalWrite(RandNum, HIGH); // Turn on an LED between pin 9-12
 unsigned long previousMillis = millis ();
 if(currentMillis - previousMillis > interval) {
 previousMillis = currentMillis;
 if (ledState == LOW)
     ledState = HIGH;
    else
     ledState = LOW;
 digitalWrite(ledPin, ledState); // 
}

I know that I need elements from the array function and the while loop function, and I understand these functions as a whole but I don't know how to put pieces of them together. Sorry if this seems remedial to you guys but I'm a little confused on where to head from here. If someone could point me in the right direction that would be great.