Show Posts
Pages: [1] 2 3
1  Using Arduino / Programming Questions / Re: Timing, PWM, and input buttons... is this feasible? on: April 28, 2013, 08:48:49 am
you are correct that those lines are redundant and incorrect.

const doesn't mean "this variable will be stored in a non-volatile area".  It means "this is hard-coded into the program and will not be changed".
PROGMEM is a method to preserve variables between runs... I could, for example, store speed and pattern choice in progmem, so that the last options selected would be the ones that came up next time the program started.

I am not an expert, but declaring as const should allow the compiler to optimize those arrays, by pre-calculating the value and choosing the best fit area in memory to store.
2  Using Arduino / Programming Questions / Re: digitalWrite with else/if complication on: April 27, 2013, 09:50:22 pm
It's the custom because typos make up about half of the debugging problems in code.  You can describe your changes, but you can't guarantee that your actual code doesn't have a silly semicolon, or a period instead of a comma, or a missing capital, or a missing brace, without actually posting it.  In addition, it speeds up visual debugging for the person helping you, as they don't have to mentally change your code, before identifying issues.

It's all part of the learning process.  Glad you got your problems sorted out.

3  Using Arduino / Programming Questions / Re: Switch LED On/OFF with the same command on: April 27, 2013, 05:22:33 pm
I'm fairly certain that you can do this:

Code:
int LED_13 = 13;

void setup() {
  pinMode(LED_13, OUTPUT);
  Serial.begin(9600);        //  Porneste Serialul
}

void loop() {
  int val;
  if (Serial.available()) {
    delay(10);
    while (Serial.available() > 0) {
      val = Serial.read();
          if (val == '1') {
            digitalWrite(LED_13, !digitalRead(LED_13));
          }
    }
  }
}


although the other solutions are WAY better
4  Using Arduino / Programming Questions / Re: Timing, PWM, and input buttons... is this feasible? on: April 27, 2013, 01:16:02 pm
so, here's the code I've prototyped:

Code:
/**********************************
 *      PWM Pattern Control       *
 *         by: gschoppe           *
 *          4/27/2013             *
 **********************************/

//DEFINE PATTERNS
const int pattern00[]  = {0xFF};
const int pattern01[]  = {0xFF,0xFF,0xFF,0xBB,0xBB};
const int pattern02[]  = {0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF,0xEE,0xDD,0xCC,0xBB,0xAA};
const int pattern03[]  = {0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF,0xFF,0xFF};
const int pattern04[]  = {0xFF,0xFF,0xEE,0xDD,0xCC};
const int patternLen[] = {sizeof(pattern00)/sizeof(pattern00[0]), sizeof(pattern01)/sizeof(pattern01[0]), sizeof(pattern02)/sizeof(pattern02[0]), sizeof(pattern03)/sizeof(pattern03[0]), sizeof(pattern04)/sizeof(pattern04[0])};
const int*  patterns[] = {pattern00,pattern01,pattern02,pattern03,pattern04};
//END PATTERN DEFINITION

//Tweakable Values
const int  defaultPulse       = 500;    // this is the value speed compares to (milliseconds)
const double speedMultiplier  = 1.2;    //difference in every speed step
const long debounce           = 200UL;  // debounce interval (milliseconds)


const int prevPattern   = 2;     // pin for control button
const int nextPattern   = 3;     // pin for control button
const int speedDown     = 4;     // pin for control button
const int speedUp       = 5;     // pin for control button
const int pwmPin        = 9;     // pin for pwm output


double speed       = 1; //geometric speed control setting (defaultPulse)/speed = delay
int currentPattern = 0;
int walkVal        = 0x80; //for bonus mode2
int i = 0;

void setup() {  
    pinMode(prevPattern, INPUT); // the buttons are the inputs
    pinMode(nextPattern, INPUT);
    pinMode(speedDown  , INPUT);
    pinMode(speedUp    , INPUT);
    
    digitalWrite(prevPattern, HIGH);
    digitalWrite(nextPattern, HIGH);
    digitalWrite(speedDown  , HIGH);
    digitalWrite(speedUp    , HIGH);
    
    pinMode(pwmPin, OUTPUT);
    
    randomSeed(analogRead(6));
}

void loop() {
    if (!digitalRead(prevPattern) ||
        !digitalRead(nextPattern) ||
        !digitalRead(speedUp    ) ||
        !digitalRead(speedDown  )   )
        readDebounced();
    //BONUS PATTERNS: RANDOM MODE
    if(currentPattern >= sizeof(patterns)/sizeof(patterns[0])) {
        bonusModes();
    }else{ // iterate through defined patterns
        if(i>=patternLen[currentPattern])
            i=0;
        analogWrite(pwmPin, patterns[currentPattern][i]);
        i++;
    }
    delay(defaultPulse/speed);    // wait one step
}

void readDebounced() {
    int pressed = 0;
    unsigned long lastPress = millis();
    while(millis() - lastPress < debounce) {
        if(!digitalRead(prevPattern)) {
            if(pressed != 1) {
                pressed = 1;
                lastPress = millis();
            }
        } else if(!digitalRead(nextPattern)) {
            if(pressed != 2) {
                pressed = 2;
                lastPress = millis();
            }
        } else if(!digitalRead(speedUp    )) {
            if(pressed != 3) {
                pressed = 3;
                lastPress = millis();
            }
        } else if(!digitalRead(speedDown  ))
            if(pressed != 4) {
                pressed = 4;
                lastPress = millis();
            }
        }
    }
    switch(pressed) {
        case 1://prevPattern
            if(currentPattern<=0) {
                currentPattern = sizeof(patterns)/sizeof(patterns[0])+1;
            } else {
                currentPattern--;
                i=0;
            }
            break;
        case 2://nextPattern
            if(currentPattern>=sizeof(patterns)/sizeof(patterns[0])+1) {
                currentPattern = 0;
            } else {
                currentPattern++;
                i=0;
            }
            break;
        case 3://speedUp
            speed = speed * speedMultiplier;
            break;
        case 4://speedDown
            speed = speed / speedMultiplier;
            break;
        default:
    }
}

void bonusModes() {  //These modes use random values
    if(currentPattern == sizeof(patterns)/sizeof(patterns[0])) {
        switch(random(1,3)) {
            case 1: //ramp down by one step
                if(walkVal > 0x80) { //keep above 50%
                    walkVal = walkVal-0x08;
                    analogWrite(pwmPin, walkVal);
                }
                break;
            case 2: // maintain constant
                break;
            case 3: // ramp up by one step
                if(walkVal < 0xFF) { //keep below 100%
                    walkVal = walkVal+0x08;
                    analogWrite(pwmPin, walkVal);
                }
                break;
            default:
        }
    } else {
        analogWrite(pwmPin, random(0x40,0xFF)); //random setting 25-100%
    }
}

I'm still building my hardware, but this seems like it should work.
5  Using Arduino / Programming Questions / Re: can I have an array of arbitrary-length arrays? on: April 27, 2013, 11:37:08 am
but wait... could I do this instead?

Code:
//DEFINE PATTERNS
const int pattern00[]  = {100};
const int pattern01[]  = {100,100,100, 80, 80};
const int pattern02[]  = {60, 70, 80, 90,100, 90, 80, 70};
const int pattern03[]  = {60, 70, 80, 90,100};
const int pattern04[]  = {  0, 10, 10, 20, 30, 50, 70,100,100,100,
                          100,100,100,100, 80, 60, 60, 60, 80,100,
                          100,100,100,100,100,100,100,100,100,100};
const int patternLen[] = {sizeof(pattern00), sizeof(pattern01), sizeof(pattern02), sizeof(pattern03), sizeof(pattern04)};
const int*  patterns[] = {pattern00,pattern01,pattern02,pattern03,pattern04};
//END PATTERN DEFINITION
6  Using Arduino / Programming Questions / Re: can I have an array of arbitrary-length arrays? on: April 27, 2013, 11:05:41 am
This "ugly" way will work for my needs... still complicated, but much better.  Thanks!
7  Using Arduino / Programming Questions / Re: can I have an array of arbitrary-length arrays? on: April 27, 2013, 10:33:53 am
Here is an example of what i need to do:
Code:
{
    {100},
    {100, 100, 100, 80, 80},
    {60, 70, 80, 90, 100, 90, 80, 70},
    {60, 70, 80, 90, 100},
    {0, 10, 10, 20, 30, 50, 70, 100, 100, 100, 100, 100, 100, 100, 80, 60, 60, 60, 80, 100, 100, 100, 100, 100, 100, 100, 100, 100}
}
8  Using Arduino / Programming Questions / Re: Timing, PWM, and input buttons... is this feasible? on: April 27, 2013, 10:23:22 am
Mark T: The lamp is actually a Velleman K8064 being controlled through a ULN2003 chip and a low-pass filter.  I was just trying to simplify the explanation, because the code is the part that is getting me stuck.  I started out on ANSI C, but I've been using high-level languages for so long, the arduino continually throws me for a loop.
9  Using Arduino / Programming Questions / can I have an array of arbitrary-length arrays? on: April 27, 2013, 10:14:35 am
I need to have several arrays of numbers, such that I can choose an array, and iterate through it.  However, these arrays need to be different lengths.  Can i define an array of arrays (matrix), where the sub-arrays (rows) have different lengths?  If not, is there a way to get similar functionality?
10  Using Arduino / Programming Questions / Re: Automatic Servo with Manual Override (delay problems) on: April 27, 2013, 09:46:48 am
Here is a modified version of your code that I reformatted and made some significant changes to.  I have not tested it, but it should be significantly easier to read, understand, and troubleshoot:

Code:
#include <Servo.h>
 
Servo myservo;
const int leftButton  = 2;     //variable to represent the pin that the left button is on
const int rightButton = 4;     //variable to represent the pin that the right button is on
int caseNum = 0;               //case number to tell the servo which case to run
int pos = 0;                   //position
int delayTime = 20;            // increasing this slows down the servo movement
unsigned long lastPress = 0;   // last time update
long interval = 3000UL;        // interval at which to do something (milliseconds)
 
void setup() {   
  myservo.attach(9);           // attaches the servo on pin 9 to the servo object
  pinMode(leftButton, INPUT ); // the buttons are the inputs
  pinMode(rightButton, INPUT);

}

void loop() {
  if (digitalRead(leftButton)) {         //check if left button pressed
    caseNum = 2;                         //if so it switches the caseNum
  } else if (digitalRead(rightButton)) { //checks right button (else skips if left is pressed)
    caseNum = 3;
  }
 
  switch (caseNum) {
    case 0:
      while( pos < 180 ) {   // pan right until 180 degrees
        myservo.write(pos);  // Move to next position
        delay(delayTime);    // Short pause to allow it to move
        pos++;               // increment
        if (digitalRead(leftButton) || digitalRead(rightButton))  // checks to see if either button is down
          break;             // break out of subloop to check inputs
      }
      caseNum = 1;  // if no buttons pressed, default to pan left
      break;
    case 1:
      while(pos > 0) {       // pan right until 0 degrees
        myservo.write(pos);  // Move to next position
        delay(delayTime);    // Short pause to allow it to move
        if (digitalRead(leftButton) || digitalRead(rightButton))  // checks to see if either button is down
          break;             // break out of subloop to check inputs
      }
      caseNum = 0;  // if no buttons pressed, default to pan right
      break;
    case 2:
      while (digitalRead(leftButton)) { //while the button is being pressed
        pos-=1;                         //position becomes one less
        if( pos < 0 ) pos = 0;          // only necessary if you want to restrict motion to 180
        myservo.write(pos);             //rotates servo to its current positon
        delay(delayTime);
        lastPress = millis();
      }
      if(millis() - lastPress >= interval)  // if it has been interval since last press
        caseNum = 0;                        // return to expected pan sequence
      break;     
    case 3:
      while (digitalRead(4)) {
          pos+=1;                         // position becomes one greater
          if( pos > 180 ) pos = 180;      // only necessary if you want to restrict motion to 180
          myservo.write(pos);
          delay(delayTime);
          lastPress = millis();
      }
      if ( millis() - lastPress >= interval)  // if it has been interval since last press
        caseNum = 1;
      break;
    default:
      caseNum = 0;
  }
}
11  Using Arduino / Programming Questions / Timing, PWM, and input buttons... is this feasible? on: April 27, 2013, 08:51:48 am
I have a lamp on a dimmer circuit, whose brightness is managed by a 0-5v control signal.  When the control is 0v, the lamp is off.  2.5v, and the lamp is at 50%, ect.  So, logically, I can control the lamp with a PWM pin, fed through a capacitor (to smooth the signal).  But I want to go a few steps further.

I want to include an array of arrays in my program, that would each look something like this:
Code:
[00, 7F, 7F, FF, FF, 7F, FF, FF, 7F, 7F, 00]
Each array would define a different pattern for the light.

The program take two button inputs, one for forward and one for back, that would choose the pattern to run.  Then the pattern would be looped through, setting the PWM output to each value for a short time (~100ms) or so.  The pattern would continue to loop, until a button press changes the selected pattern array.

So, the question is: how advanced are timing events and PWM in arduino?  Can I poll for input without effecting the timing of the loop that reads the pattern?  Can I read the pattern without effecting PWM timing?

Any help or advice would be really appreciated... I'm kinda new to using the arduino for anything not strictly iterative.
12  Forum 2005-2010 (read only) / Syntax & Programs / serial questions... ir remote? on: April 21, 2007, 07:32:29 pm
ok, so here's the basic idea...

infrared remote control via arduino

the standard frequency is 36000 hertz

I need to send control codes made up of ascii values like" ¬ã;Ë"
AKA ['RS', 'ETX', 'ETX', 'SOH', '¬', 'ã', ';', SOH, 'Ë']

I figured I could use the serial output and set the baud rate to the frequency...

How would I go about this?  Is there a better way?
13  Forum 2005-2010 (read only) / Syntax & Programs / Re: can the arduino generate frequencies accuratel on: May 22, 2007, 11:19:29 pm
Actually, I'm looking to strobe at a chosen frequency, so the time HIGH has to be much shorter than the time spent LOW...
14  Forum 2005-2010 (read only) / Syntax & Programs / Re: can the arduino generate frequencies accuratel on: May 22, 2007, 12:06:22 am
Thanks everybody... I'll now need to look into this library.

I need a non-square wave, so I'll probably end up just borrowing from it, but it looks really useful.
15  Forum 2005-2010 (read only) / Syntax & Programs / can the arduino generate frequencies accurately? on: May 19, 2007, 06:20:30 pm
I am looking to generate timed pulses (similar application to generating tones) with PWM via delayMicroseconds.  I can get the basic code workings, but a few questions came up that rudimentary testing with my untrained ear couldn't answer.

1.  Is there an audible difference in pitch between a tone generated with a delayMicroseconds loop and one generated by a tuning fork (or other "accurate measure")?
2.  Will polling for button presses in the loop alter the tone noticably (assume the case where no buttons are depressed, i.e. if statement evaluates as false)?  There may be as many as thirteen buttons to poll.
3.  Is there a better way to exit the loop and perform an action on button press than polling all buttons on each iteration of the loop?
4.  Will delayMicroseconds be effected by the timer overflow the Arduino sometimes gets (i.e. can I use this message to generate a frequency indefinitely, or is there a limit to the runtime of such a loop)?

anyone know the answers?

thanks in advance
Pages: [1] 2 3