Tilt Sensor and LED's

Hi,

I am trying to integrate the use of an analogue tilt sensor, with a random blink LED code. For example I want the tilt sensor (Pushbutton) value to determine the state of the LEDs. If the button state is LOW, I want it to trigger the random flashing, but when the button state is HIGH all LEDs remain on.

I have a code to attempt this to happen,based on tips from an arduino technician and this forum. (I am a student, only just starting out with Arduino, with a project to make a disco mask…). I can get the random blinking to work well, but struggling to integrate the tilt sensor.

I would be really grateful if anyone could look, and let me know what i’m missing.
Thanks,

long nextFlash[8];
int ledPin[] = {3, 4, 5, 6, 7, 8, 9, 10}; // LED pins to use.
int ledState[8];
int buttonPin =2;

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup(){
for(int i = 0; i<8; i++){
 pinMode(ledPin[i],OUTPUT);
 ledState[i] = LOW;
 digitalWrite(ledPin[i], LOW); // all LEDs off
   nextFlash[i] = millis() +random(100, 1000);

   // initialize the pushbutton pin as an input:
 pinMode(buttonPin, INPUT); 
 
} 
}

void loop(){
  //read the state of the pushbutton value:
  buttonState= digitalRead(buttonPin);
  
   // check if the pushbutton is pressed.
 // if it is, the buttonState is HIGH:

 
 
  if (buttonState == LOW) { 
for(int i = 0; i<8; i++){
if(millis() > nextFlash[i]){
if(ledState[i] == LOW) ledState[i] = HIGH; else ledState[i] = LOW;
digitalWrite(ledPin[i],ledState[i]);
nextFlash[i] = millis()+random(100, 1000) ; // next toggle random time
} }


 if (buttonState == HIGH) {
//turn all LED on:
ledState[8] = HIGH; } }
digitalWrite(ledPin [8], HIGH); //all leds on
}

[/code]

For example I want the tilt sensor (Pushbutton) value to determine the state of the LEDs.

Determine, to me, implies read. Define, on the other hand, implies write.

I'm sure that your code is not in italics. There are stickies at the top of the forum that you were supposed to read before posting here. Now would be a good time to do so, and fix your initial post.

If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the [code] and [/code] metatags.

Then read these two posts:

How to use this forum - please read.
and
Read this before posting a programming question …

Thanks, have amended my first post. This is my other code, which I hoped I could get to have the same function with the tilt switch. Thanks.

int buttonPin =2;
int ranNum;
int ranDel;

int buttonState = 0;

void setup() {
// Seed RNG from analog port.
randomSeed(analogRead(0));
// Setup 8 output ports for LED's
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);

// initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);    
  }
void loop() {
   //read the state of the pushbutton value:
   buttonState= digitalRead(buttonPin);
   
    // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  
   if (buttonState == LOW) { 
   //Generate random number between 8 and 10
   ranNum=random(3,11);
   // Generate random delay time
   ranDel=random(20,200);
   //Turn on the LED
   digitalWrite(ranNum, HIGH);
   delay(150);
   //Turn off the LED
   digitalWrite(ranNum, LOW);  
}
else {
if (buttonState == HIGH)
//turn all LED on:
digitalWrite(ranNum, HIGH);

}
}

You can't read the switch while delay() is running. In fact you can't do anything. So you are trying to do several things at the same time.

    // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:

   if (buttonState == LOW) {

Does your code agree with your comments?

Yes, I ideally didn’t want to use delays, as in my first code:
I have achieved constant random seed with flashing leds as well as constant (when I made some alterations, just unfortunate cannot integrate the tilt switch.

long nextFlash[8];
int ledPin[] = {3, 4, 5, 6, 7, 8, 9, 10}; // LED pins to use.
int ledState[8];
int buttonPin =2;

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup(){
for(int i = 0; i<8; i++){
 pinMode(ledPin[i],OUTPUT);
 ledState[i] = LOW;
 digitalWrite(ledPin[i], LOW); // all LEDs off
   nextFlash[i] = millis() +random(100, 1000);

   // initialize the pushbutton pin as an input:
 pinMode(buttonPin, INPUT); 
 
} 
}

void loop(){
  //read the state of the pushbutton value:
  buttonState= digitalRead(buttonPin);
  
   // check if the pushbutton is pressed.
 // if it is, the buttonState is HIGH:

 
 
  if (buttonState == LOW) { 
for(int i = 0; i<8; i++){
if(millis() > nextFlash[i]){
if(ledState[i] == LOW) ledState[i] = HIGH; else ledState[i] = LOW;
digitalWrite(ledPin[i],ledState[i]);
nextFlash[i] = millis()+random(100, 1000) ; // next toggle random time
} }


 if (buttonState == HIGH) {
//turn all LED on:
ledState[8] = HIGH; } }
digitalWrite(ledPin [8], HIGH); //all leds on
}

Thanks for the link, I read through and tried many options but to no good.

Your code (still) looks like shit.

Put every { on a line BY ITSELF.

Put every } on a line BY ITSELF.

Fix the comments or the code so that the comments and code say the same thing.

Use Tools + Auto Format to fix the crappy indenting.

Maybe then we can read it. Maybe then you'll think to explain EXACTLY what the code does and EXACTLY how that differs from what you want.

Thanks for your help, this is my first time working with writing code. I hope that I have made the code readable now.

I know there will be big errors in code, in relation to what I actually want it to do. But any help would be appreciated.

My intentions for the code:

There are 8 LEDs (in pins 3, 4, 5, 6, 7, 8, 9, 10)
1 Tilt Switch (pushbutton , in pin 2)

Starts with all LEDS on:
When the tilt switch (buttonPinState) is HIGH I want all the leds to be on.

When the tilt switch (buttonPinState) is LOW, I want this to trigger the random sequence of the leds.

also able to revert back to having all LEDS on, when the tilt switch (buttonPinState) is high again

Currently:

The Leds start with the random sequence.

They do partially respond to the state of the tilt sensor (buttonPinState), but it almost freezes the sequence, so the leds that were on at that point in the random sequence, remain on.

I know that I have mixed some things up, (as the code does almost what I want but in reverse) but as I said any help would be appreciated.
Thanks again.

/*


  the buttonPin (tilt sensor) attached to pin 2, turns on and triggers a random sequence of flashing 8 light emitting diodes(LED) (connected to digital
  pins 3, 4, 5, 6, 7, 8, 9 and 10), when the buttonPinstate is LOW.
  when the buttonPinState is HIGH, all leds are constantly on.
  
 




*/

long nextFlash[8];
int ledPin[] = {3, 4, 5, 6, 7, 8, 9, 10}; // LED pins to use.
int ledState[8];
int buttonPin = 2;

// variables will change:
int buttonPinState = 0;         // variable for reading the pushbutton status,

void setup()
{
  for (int i = 0; i < 8; i++)
  {
    pinMode(ledPin[i], OUTPUT);
    ledState[i] = LOW;
    digitalWrite(ledPin[i], LOW); // all LEDs off
    nextFlash[i] = millis() + random(100, 1000);

    // initialize the pushbutton pin as an input:
    pinMode(buttonPin, INPUT_PULLUP);

  }
}

void loop()
{
  //read the state of the pushbutton value to determine if the leds should be continuously on, or to trigger random sequence:
  buttonPinState = digitalRead(buttonPin);

  // check if the pushbutton is not pressed.
  // if it is, the buttonPinState is LOW:



  if (buttonPinState == LOW) //trigger random sequence flashing
  {
    for (int i = 0; i < 8; i++)
    {
      if (millis() > nextFlash[i])
      {
        if (ledState[i] == LOW) ledState[i] = HIGH; else ledState[i] = LOW;
        digitalWrite(ledPin[i], ledState[i]);
        nextFlash[i] = millis() + random(100, 1000) ; // next toggle random time
      }
    }

    //check if pushbutton is pressed.
    //if it is, buttonPinState is HIGH:
    //leds continuously on


    if (buttonPinState == HIGH)
    {
      //turn all LED on:
      ledState[8] = HIGH;
    }
  }
  digitalWrite(ledPin [8], HIGH); //all leds on continuously

}
      //turn all LED on:
      ledState[8] = HIGH;

Nonsense. That is NOT what that code does.

int ledState[8];

The elements in the array are numbered 0 to 7. You are shitting in memory that does not belong to your array.

I made 6 comments. You dealt with the first 5.

I should have made 7, telling you to also ditch the

useless white space

in your code.