Show Posts
Pages: [1] 2 3 ... 10
1  Using Arduino / Programming Questions / Re: blink without delay example for simultaneous functions on: August 18, 2013, 04:43:03 pm
Perfect!  For the ALD (Arduino Learning Disabled), solving this puzzle seems both necessary and impossible.  Thanks again. 
2  Using Arduino / Programming Questions / Re: blink without delay example for simultaneous functions on: August 17, 2013, 05:29:17 pm
Added  "solOneinterval = 0;" below else.
3  Using Arduino / Programming Questions / Re: blink without delay example for simultaneous functions on: August 17, 2013, 04:24:15 pm
Obviously not resetting the random correctly. Also, what are code tags (dumb question).

Code:

const byte solOne = 2;
const byte solTwo = 4;

// Time periods of blinks in milliseconds (1000 to a second).
unsigned long solOneinterval = 0;//500
unsigned long solTwointerval = 0;//1000

// Variable holding the timer value so far. One for each "Timer"
unsigned long solOnetimer;
unsigned long solTwotimer;

void setup ()
{
  pinMode (solOne, OUTPUT);
  pinMode (solTwo, OUTPUT);
  solOnetimer = millis ();
  solTwotimer = millis ();
}  // end of setup

void toggleSolOne ()
{
  if (digitalRead (solOne) == LOW)
    digitalWrite (solOne, HIGH);
  else
    digitalWrite (solOne, LOW);

  // remember when we toggled it
  solOnetimer = millis ();
  solOneinterval = 0; [font=Verdana]//////[/font]
}  // end of toggleGreenLED

void toggleSolTwo ()
{
  if (digitalRead (solTwo) == LOW)
    digitalWrite (solTwo, HIGH);
  else
    digitalWrite (solTwo, LOW);

  // remember when we toggled it
  solTwotimer = millis ();
  solTwointerval = 0;[font=Verdana]///////// [/font]
}  // end of toggleRedLED

void loop ()
{
  solOneinterval = random (100,500);
  if ( (millis () - solOnetimer) >= solOneinterval)
    toggleSolOne ();

  solTwointerval = random (500,1000);
 
  if ( (millis () - solTwotimer) >= solTwointerval)
    toggleSolTwo ();

 


4  Using Arduino / Programming Questions / Re: blink without delay example for simultaneous functions on: August 17, 2013, 03:58:27 pm
After looking at web examples, I assumed this would generate random solenoids. I am puzzled why it doesn't. The two ignore the random and fire at 1/2 and one second just as before.


const byte solOne = 2;
const byte solTwo = 4;

// Time periods of blinks in milliseconds (1000 to a second).
 unsigned long solOneinterval = 0;//500
 unsigned long solTwointerval = 0;//1000

// Variable holding the timer value so far. One for each "Timer"
unsigned long solOnetimer;
unsigned long solTwotimer;

void setup ()
  {
  pinMode (solOne, OUTPUT);
  pinMode (solTwo, OUTPUT);
  solOnetimer = millis ();
  solTwotimer = millis ();
  }  // end of setup

void toggleSolOne ()
  {
   if (digitalRead (solOne) == LOW)
      digitalWrite (solOne, HIGH);
   else
      digitalWrite (solOne, LOW);

  // remember when we toggled it
  solOnetimer = millis (); 
  }  // end of toggleGreenLED

void toggleSolTwo ()
  {
   if (digitalRead (solTwo) == LOW)
      digitalWrite (solTwo, HIGH);
   else
      digitalWrite (solTwo, LOW);

  // remember when we toggled it
  solTwotimer = millis (); 
  }  // end of toggleRedLED

void loop ()
  {

  // Handling the blink of one LED.
  solOneinterval = random (100,500); ?????????
  if ( (millis () - solOnetimer) >= solOneinterval)
     toggleSolOne ();
  solTwointerval = random (500,1000); ????????
  // The other LED is controlled the same way. Repeat for more LEDs
  if ( (millis () - solTwotimer) >= solTwointerval)
    toggleSolTwo ();

/* Other code that needs to execute goes here.
   It will be called many thousand times per second because the above code
   does not wait for the LED blink interval to finish. */

}  // end of loop
5  Using Arduino / Programming Questions / Re: blink without delay example for simultaneous functions on: August 17, 2013, 03:23:25 pm
Great advise, great because I understand what you are describing, which is not always the case. First time I've actually gotten two of anything to function independently. Now for the random business which I assume is just a matter of editing. Thanks again.
6  Using Arduino / Programming Questions / blink without delay example for simultaneous functions on: August 17, 2013, 02:36:00 pm
I have been advised time and again to embrace the "blink without delay" example to perform functions independently.  This sketch is a failed attempt to program two solenoids to fire randomly, not consecutively. Although they are random, one activates after the other in sequence. Am I asking Arduino to do something it wasn't designed to do? 

Code:
int solOne =2;
int solTwo =4;
int solState = LOW;           
long previousMillis = 0;
long interval = 10;
long randOn1 = 0;                  // Initialize a variable for the ON time
long randOff1 = 0;
long randOn2 = 0;                  // Initialize a variable for the ON time
long randOff2 = 0;


void setup() {
  randomSeed (analogRead (0));
  pinMode (solOne, OUTPUT);
  pinMode (solTwo, OUTPUT);
}
void loop()
{
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (solState == LOW)
      solState = HIGH;
    else
      solState = LOW;
    OneSol();
    TwoSol();
    delay(1000);
  }
}
void OneSol()
{
  randOn1 = random (100, 1200);    // generate ON time between 0.1 and 1.2 seconds
  randOff1 = random (200, 900);    // generate OFF time between 0.2 and 0.9 seconds
  digitalWrite(solOne, HIGH);   // sets the solenoid on
  delay(randOn1);                // waits for a random time while ON
  digitalWrite(solOne, LOW);    // sets the solenoid off
  delay(randOff1); 
}
void TwoSol()
{
  randOn2 = random (150, 500);    // generate ON time between 0.1 and 1.2 seconds
  randOff2 = random (100, 1900);    // generate OFF time between 0.2 and 0.9 seconds
  digitalWrite(solTwo, HIGH);   // sets the solenoid on
  delay(randOn2);                // waits for a random time while ON
  digitalWrite(solTwo, LOW);    // sets the solenoid off
  delay(randOff2); 
}


7  Using Arduino / Programming Questions / Re: Activating audio from bent circuit on: July 25, 2013, 02:22:20 pm
With considerably more experience, I may be able to decipher your clues but am presently at a loss where to take this.  "state1 = (2,3,4);" was intended to direct the sketch to these "case" when the threshold was surpassed. The "delay" are there to permit the audio that much time to play. As for the brackets { }, I am not sure how to respond. I am not asking anybody to write the sketch for me, but a few specific directions would certainly help.  The "blink without delay" example is especially baffling: How can the sensor be included along with all the sounds? A web search for examples doesn't yield similar problems (or, I don't grasp their meaning).
8  Using Arduino / Programming Questions / Re: Activating audio from bent circuit on: July 24, 2013, 06:20:46 pm
Unfortunately, she didn't appreciate my embrace, but threw me at once to the dogs! Being ugly, old, and lame, I fear she will need considerable persuasion. A mere hint from your majesty would be most kind.

Code:
int sensorPin = A0;
int sensorValue = 0;

int siren = 3;
long randOn =0;
long randOff =0;

int snd1 = 5;
int snd2 = 6;
int snd3 = 7;
int snd4 = 8;
int snd5 = 9;

int sirenState = LOW;
int snd1State = LOW;
int snd2State = LOW;
int snd3State = LOW;
int snd4State = LOW;
int snd5State = LOW;

// ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  pinMode(siren, OUTPUT);
  randomSeed (analogRead (0));

  pinMode(snd1, OUTPUT);
  pinMode(snd2, OUTPUT);
  pinMode(snd3, OUTPUT);
  pinMode(snd4, OUTPUT);
  pinMode(snd5, OUTPUT);
  digitalWrite(snd1,LOW);
  digitalWrite(snd2,LOW);
  digitalWrite(snd3,LOW);
  digitalWrite(snd4,LOW);
  digitalWrite(snd5,LOW);

  Serial.begin(9600);
  delay(100);       
}
void loop()
{
  sensorValue = analogRead(sensorPin);

  Serial.println(sensorValue);
  delay(500);
  unsigned long currentMillis = millis();

  if (sensorValue > 15) {

    if(currentMillis - previousMillis > interval) {

      previousMillis = currentMillis;     
      if (sirenState == LOW)
        sirenState = HIGH;
      delay(1000);
    }
    else
    {
      sirenState = LOW;
      digitalWrite(siren, sirenState);
      if (snd1State == LOW){
        snd1State = HIGH;
        delay(1000);
      }
      else
        snd1State = LOW;
      {
      }
    }
  }
}




9  Using Arduino / Programming Questions / Re: Activating audio from bent circuit on: July 24, 2013, 05:42:39 pm
This attempt is not responding to sensor input.  The audio begins immediately without end.

Code:
int siren = 3;
long randOn =0;
long randOff =0;

int snd1 = 5;
int snd2 = 6;
int snd3 = 7;
int snd4 = 8;
int snd5 = 9;
int sonarPin = A0;
int sonarVal =0;

void setup()
{
  pinMode(siren, OUTPUT);
  randomSeed (analogRead (0));

  pinMode(snd1, OUTPUT);
  pinMode(snd2, OUTPUT);
  pinMode(snd3, OUTPUT);
  pinMode(snd4, OUTPUT);
  pinMode(snd5, OUTPUT);
  digitalWrite(snd1,LOW);
  digitalWrite(snd2,LOW);
  digitalWrite(snd3,LOW);
  digitalWrite(snd4,LOW);
  digitalWrite(snd5,LOW);
  Serial.begin(9600);
  pinMode(sonarPin,INPUT);
}
void loop()
{
  static int state1 = 1;
  static int state2 = 1;
  sonarVal = analogRead (sonarPin);
  Serial.println (sonarVal);
  delay (50);
  switch (state1)
  { 
  case 1:
    if (sonarVal >5)
    {
      digitalWrite(siren,LOW);
      digitalWrite(snd1,LOW);
      digitalWrite(snd2,LOW);
      digitalWrite(snd3,LOW);
      digitalWrite(snd4,LOW);
      digitalWrite(snd5,LOW);
    }
    else
    {
      state1 = (2,3,4);   

    }
    break;
  case 2:
    if (sonarVal < 15)
    {
      randOn = random (50, 5000);
      randOff = random (1, 900);
      digitalWrite(siren,HIGH);
      delay(randOn);

      digitalWrite(siren,LOW);
      delay(randOff);

    }
    else
    {
      state1 = 1;         
    }
    break;
  case 3:

    if (sonarVal < 15)
    {
      digitalWrite(snd1,HIGH);
      delay(5000);
      digitalWrite(snd1,LOW);
    }
    else
    {
      state1 = 1;
      {       

        if (sonarVal < 15)
        {
          break;
        case 4:
          digitalWrite(snd2,HIGH);
          delay(5000);
          digitalWrite(snd2,LOW);
        }
        else
        {
          state1 = 1;
        }
        break;
      }

    }

  }
}















10  Using Arduino / Programming Questions / Re: Activating audio from bent circuit on: July 24, 2013, 01:05:09 pm
Thanks. The sketch is working. Well, not exactly as it should. Firstly, there is a long, ten second delay after running through the sounds before starting once again.  And, when the sensor goes below the threshold, the sketch continues to the end before stopping.  To stop immediately, is an interrupt necessary? I have no experience with interrupts but will dive into it if advised this is the solution.   



Code:
int siren = 3;
long randOn =0;
long randOff =0;

int snd1 = 5;
int snd2 = 6;
int snd3 = 7;
int snd4 = 8;
int snd5 = 9;

int sensorPin = A0;
int sensorValue = 0;

void setup(){
  pinMode(drummer, OUTPUT);
  pinMode(siren, OUTPUT);
  randomSeed (analogRead (0));

  pinMode(snd1, OUTPUT);
  pinMode(snd2, OUTPUT);
  pinMode(snd3, OUTPUT);
  pinMode(snd4, OUTPUT);
  pinMode(snd5, OUTPUT);
  digitalWrite(snd1,LOW);
  digitalWrite(snd2,LOW);
  digitalWrite(snd3,LOW);
  digitalWrite(snd4,LOW);
  digitalWrite(snd5,LOW);

  Serial.begin(9600);
  delay(500); 
}
void loop()
{
  sensorValue = analogRead(sensorPin);
  Serial.println(sensorValue);
  delay(500);

  if (sensorValue > 15)
  {
    randOn = random (50, 5000);
    randOff = random (1, 900);
    digitalWrite(siren,HIGH);
    delay(randOn);

    digitalWrite(siren,LOW);
    delay(randOff);

    digitalWrite(snd1,HIGH);
    delay(5000);
    digitalWrite(snd1,LOW);
    //delay(3000);

    digitalWrite(snd2,HIGH);
    delay(5000);
    digitalWrite(snd2,LOW);
    // delay(3000);
    digitalWrite(siren,HIGH);
    delay(randOn);
    digitalWrite(siren,LOW);
    delay(randOff);
   

    digitalWrite(snd3,HIGH);
    delay(5000);
    digitalWrite(snd3,LOW);

    digitalWrite(snd4,HIGH);
    delay(5000);
    digitalWrite(snd4,LOW);

    digitalWrite(snd5,HIGH);
    delay(5000);
    digitalWrite(snd5,LOW);
    //delay(1000);
  }   
  }










11  Using Arduino / Programming Questions / Activating audio from bent circuit on: July 23, 2013, 07:10:54 pm
This sketch has been edited a dozen different ways with the same result; sounds 1~5 fire as they should but the random buzzer emits only a single beep.  The sounds are salvaged from children't toys triggered by a photocell. In a separate sketch, the buzzer is functional but not here.  I assume this is a software error.  Appreciate any suggestions.  "randOn" begins code for the buzzer. 

Code:
int drummer = 12;
int siren = A3;
long randOn =0;
long randOff =0;

int snd1 = 5;
int snd2 = 6;
int snd3 = 7;
int snd4 = 8;
int snd5 = 9;

int sensorPin = A0;
int sensorValue = 0;

void setup(){
  pinMode(drummer, OUTPUT);
  pinMode(siren, OUTPUT);
  randomSeed (analogRead (0));

  pinMode(snd1, OUTPUT);
  pinMode(snd2, OUTPUT);
  pinMode(snd3, OUTPUT);
  pinMode(snd4, OUTPUT);
  pinMode(snd5, OUTPUT);
  digitalWrite(snd1,LOW);
  digitalWrite(snd2,LOW);
  digitalWrite(snd3,LOW);
  digitalWrite(snd4,LOW);
  digitalWrite(snd5,LOW);

  Serial.begin(9600);
  delay(500); 
}
void loop()
{
  sensorValue = analogRead(sensorPin);
  Serial.println(sensorValue);
  delay(500);

  if (sensorValue > 15)
  {
   
    digitalWrite(snd1,HIGH);
    delay(5000);
    digitalWrite(snd1,LOW);
    //delay(3000);

    digitalWrite(snd2,HIGH);
    delay(5000);
    digitalWrite(snd2,LOW);
    // delay(3000);

    digitalWrite(snd3,HIGH);
    delay(5000);
    digitalWrite(snd3,LOW);

    digitalWrite(snd4,HIGH);
    delay(5000);
    digitalWrite(snd4,LOW);

    digitalWrite(snd5,HIGH);
    delay(5000);
    digitalWrite(snd5,LOW);
    delay(1000);
    randOn = random (50, 500); ----------buzzer
    randOff = random (1, 900);
    analogWrite(siren,50);
    delay(randOn);
    analogWrite(siren,200);
    delay(randOn);

    analogWrite (siren, LOW);
    delay(randOff);
  }
}







12  Using Arduino / Project Guidance / Re: audio from bent circuits on: July 10, 2013, 06:47:39 pm
Thanks so much. Your suggestions have helped me avoid many, many dead ends.
13  Using Arduino / Project Guidance / audio from bent circuits on: July 10, 2013, 12:44:58 pm
I would like to include recorded audio from four different boards salvaged from children's toys.  These "bent circuits" are battery powered with different voltages and are easily burned.  Frankly, I don't know how to approach this problem. Could I use an LM317 regulator for each board to deliver precise voltage? Are transistors necessary?  Can the output of the boards be wired to a single amplifier circuit? 
14  Using Arduino / Programming Questions / Re: analog values stop at threshold on: July 08, 2013, 01:58:11 pm
Finally!  The Serial.println in each "while" statement was necessary.  Thanks for the help. 

#include <Stepper.h>

#define STEPS1 200
#define STOP 0

int rightSwitchPin =7;
int leftSwitchPin =8;
int sonarPin = A0;
int sonarValue=0;

Stepper stepper1(STEPS1, 3,5,4,6);//bipolar

void setup()
{
  stepper1.setSpeed(30);
  Serial.begin(9600);

}
void loop()
{

  sonarValue = analogRead (sonarPin);
  Serial.println (sonarValue);

  if (sonarValue >900)
  {
    while (digitalRead(rightSwitchPin) == LOW) //keep going until limit switch reached
    {   
      stepper1.step(-1);  //move right
      Serial.println (sonarValue); 
    }

    while (digitalRead(leftSwitchPin) == LOW) //keep going until limit switch reached
    {
      stepper1.step(1);  //move left
      Serial.println (sonarValue);

      {
      }
    }
  }
}







15  Using Arduino / Programming Questions / Re: analog values stop at threshold on: July 08, 2013, 12:18:37 pm
Forgot to mention, this sketch, when uploaded to the board in question, displays values as intended. Would seem it doesn't like the "while" code?
Pages: [1] 2 3 ... 10