Pages: [1]   Go Down
Author Topic: Remote control flashing lights  (Read 322 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

I have just started to learn how to program arduino and am a little confused and could do with some help.

I am trying to make some led lights start flashing randomly. which is working fine using the code bellow.
But now I would like to switch them on and off using bluetooth from my phone.
I have the bluetooth working using the other code bellow and I am able to switch a single led on and off no problem.

But I'm not sure how I would get the bluetooth program to start and stop the void loop part of the led program.
so that I can start the leds flashing by typing A and turn the leds off when I type a

Please can someone help.

bluetooth code

Code:

/*
simple LED test
 */

char val;         // variable to receive data from the serial port
int ledpin = 2;  // LED connected to pin 2 (on-board LED)

void setup()
{
  pinMode(ledpin = 2, OUTPUT); // pin 2 (on-board LED) as OUTPUT

    Serial.begin(115200);       // start serial communication at 115200bps
}
void loop() {
  if( Serial.available() )       // if data is available to read
  {
    ;
  }
  val = Serial.read();         // read it and store it in 'val'

  if( val == 'a' )               // if 'a' was received led 2 is switched off

  {
    digitalWrite(ledpin, LOW);    // turn Off pin 2 off
}

  if( val == 'A' )               // if 'A' was received led 2 on
  {
    digitalWrite(ledpin = 2, HIGH);  // turn ON pin 2 on
  }
   
}


Flashing led code

Code:
*/

int timer = 30;           // The higher the number, the slower the timing.
int ledPins[] = {
  12, 9, 2, 7, 10, 4, 6, 13, 5, 11, 8, 3 };       // an array of pin numbers to which LEDs are attached
int pinCount = 12;           // the number of pins (i.e. the length of the array)

void setup() {
  int thisPin;
  // the array elements are numbered from 0 to (pinCount - 1).
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 0; thisPin < pinCount; thisPin++)  {
    pinMode(ledPins[thisPin], OUTPUT);     
  }
}

void loop() {
  // loop from the lowest pin to the highest:
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);   
    delay(timer);                 
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);   

  }

  // loop from the highest pin to the lowest:
  for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);
  }
}


Thanks
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your loop() function (not your void loop() function) does something. You want it to do something only when it receives some character. It really doesn't seem that difficult, especially since you managed to accomplish that feat once.

You try something, and if it doesn't work, let is know what you tried, and what the results were.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

Thanks for you response.

I have spent a bit more time on the code but am still getting some error messages.
Quote
remote_Random_Fairy_lights.cpp: In function ‘void setup()’:
remote_Random_Fairy_lights.cpp:26:2: error: expected initializer before ‘if’

I keeping looking through the program but am unable to spot the problems.

here is a copy of the program.
Code:

char val;         // variable to receive data from the serial port
int timer = 30;           // The higher the number, the slower the timing.
int ledPins[] = {
  12, 9, 2, 7, 10, 4, 6, 13, 5, 11, 8, 3 };       // an array of pin numbers to which LEDs are attached
int pinCount = 12;           // the number of pins (i.e. the length of the array)

void setup()

{
  int thisPin;
  // the array elements are numbered from 0 to (pinCount - 1).
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 0; thisPin < pinCount; thisPin++) 
  {
    pinMode(ledPins[thisPin], OUTPUT); 
 pinMode(thisPin = 0, OUTPUT); // pin 2 (on-board LED) as OUTPUT

    Serial.begin(115200);       // start serial communication at 115200bps
}

void loop()

 if( Serial.available() )       // if data is available to read
  {
    ;
  }
  val = Serial.read();         // read it and store it in 'val
 
  {
     if( val == 'A' )               // if 'A' was received  // turn the pin on:
    // loop from the lowest pin to the highest:
  for (int thisPin = 0; thisPin < pinCount; thisPin++)
    digitalWrite(thisPin = 0, HIGH); 
    delay(timer);                 
 
   if( val == 'a' )               // if 'a' was received // turn the pin off:
   digitalWrite(thisPin, LOW);     

  }
}

Please could I have some advice on where the problem is.

Thanks
Logged

Leeds, England
Offline Offline
God Member
*****
Karma: 14
Posts: 586
Quick, chuck it in the bin before the boss finds out...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Check your curly brackets? Especially within the loop section.
Logged

Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 108
Posts: 4014
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Before any code in a function there needs to be a {
Code:
  void loop()

    if( Serial.available() )       // if data is available to read
Check your { and } pairs carefully.  You have the loop() function inside the setup() function
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
pinMode(thisPin = 0, OUTPUT); // pin 2 (on-board LED) as OUTPUT
You don't ever want that for loop to end, do you? Why are you assigning a value to thisPin in this function call?

Code:
  int thisPin;
  // the array elements are numbered from 0 to (pinCount - 1).
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 0; thisPin < pinCount; thisPin++) 
The variable thisPin used in the for loop is not the same variable that will be in scope when the for loop ends. What is the other uninitialized thisPin for?

Where does your for loop end? Why is loop() defined in setup(), inside the for loop?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

Just want to say thanks and let you know I got it working.
Here is a copy of the code.


Code:
char val;         // variable to receive data from the serial port
int timer = 30;           // The higher the number, the slower the timing.
int ledPins[] = {
  12, 9, 2, 7, 10, 4, 6, 13, 5, 11, 8, 3 };       // an array of pin numbers to which LEDs are attached
int pinCount = 12;           // the number of pins (i.e. the length of the array)

void setup()
{
    int thisPin;
  //pinMode(thisPin = 0, OUTPUT); // pin (on-board LED) as OUTPUT
    Serial.begin(115200);       // start serial communication at 115200bps
     
  // the array elements are numbered from 0 to (pinCount - 1).
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 0; thisPin < pinCount; thisPin++)  {
    pinMode(ledPins[thisPin], OUTPUT); 
   }
}

void loop()
{
  if( Serial.available() )       // if data is available to read

  val = Serial.read();         // read it and store it in 'val
  if( val == 'A' )               // if 'A' was received  // turn the pin on:
  {
  // loop from the lowest pin to the highest:
  for (int thisPin = 0; thisPin < pinCount; thisPin++)
    {
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);   
    delay(timer);                 
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);   
    }
  }
}
Logged

Pages: [1]   Go Up
Jump to: