Show Posts
Pages: [1] 2
1  Using Arduino / Programming Questions / Re: How to prompt for a button press on: October 14, 2013, 02:39:42 pm
Quote
The modes are very similar, and it's preferable for the program to get trapped permanently in one mode or the other.

Why should you lock up your processor?

Quote
But how do you get it to run either function1() or function2() in an endless loop?  I don't want them to return

Why?

PS:
If you want to have the same screen you do not have to endless loop! You only must not change the display data!

Yours,
Stefan
2  Using Arduino / Programming Questions / Re: send data to the Serial port with visual studio, and use it in Arduino IDE on: October 14, 2013, 02:14:21 pm
Quote
but with arduino's Serial.read() function I can only read one character at a time, which is very confusing for me. :/

Try:

Code:
void read_serial (String &serial_indata) {
  serial_indata="";

  if (Serial.available() > 0) {
    int h=Serial.available();   
    // if you are getting escape -characters try h--; here
    for (int i=0;i<h;i++){
      serial_indata += (char)Serial.read();
    }
    // if you are getting escape -characters try Serial.read(); here
  }
 
  // trim the white space off the string:
  serial_indata.trim();
}

Yours,
Stefan
3  Using Arduino / Programming Questions / Re: How to prompt for a button press on: October 14, 2013, 02:02:27 pm
Hi,
Quote
but I'm not sure how to get the Arduino to ask the question and wait forever for the button press?

e.g:
Code:
int bottonState1=digitalRead(YOURBOTTON1);
int bottonState2=digitalRead(YOURBOTTON2);

if (bottonState1-oldbottonState1==-1) { //if the user has pressed and left the botton1
  function1(); //execute your function to to something
}
oldbottonState1=bottonState1;

if (bottonState2-oldbottonState2==-1) { //if the user has pressed and left the botton2
  function2(); //execute your function to to something
}
oldbottonState2=bottonState2;

For timed actions, please read: arduino.cc/en/Tutorial/BlinkWithoutDelay‎

EDIT:

Don't to forget to debounce your buttons...
Have a look at: http://playground.arduino.cc/code/bounce

Yours,
Stefan
4  Using Arduino / Programming Questions / Re: ChronoDot 2.1 : says 2000/1/1 ? on: October 14, 2013, 01:53:02 pm
Quote
rtc.adjust(DateTime(__DATE__, __TIME__));

What is the output of

Code:
Serial.print(F("__DATE__    = "));

in void setup()?

PS:
Some info about these variables is given at:
http://playground.arduino.cc/Main/ShowInfo

Yours,
Stefan
5  Using Arduino / Programming Questions / OOP, include files and Arduino on: October 14, 2013, 01:18:06 pm
Hello,

Is there any solution if an Arduino libray is used and if there are several include files which use one instance of the library?

What is the better solution:

One file pair (*.cpp/*.h) instancing the library and serveral funtions that are calling the libraries functions (->to have access to the instance of the library and the functions in it)?

Is it better to instance the library in the *.ino file and forget all the other include files?

Is there a better way to do so?

Should everything be written in OOP?

What about memory usage?

Please give me an answer!

Yours,
Stefan
6  Using Arduino / Programming Questions / Re: integer or array? on: September 26, 2013, 12:47:11 pm
Sorry,

Unless you're using a Due, not quite. On most Arduinos, an 'int' consumes two bytes, so 'byte test1[2]' takes the same space as 'unsigned int test2'.

you are right.

Thanks all!

Yours,
Stefan
7  Using Arduino / Programming Questions / Re: integer or array? on: September 26, 2013, 12:36:36 pm
Hello,

as I can see in the previous posts it does not matter to use:

Code:
byte test1[4];
,or:
Code:
unsigned int test2;

.

Correct?

Yours,
Stefan
8  Using Arduino / Programming Questions / integer or array? on: September 25, 2013, 11:26:21 am
Hello,

what's the better (faster, less memory using) version to store bool varibles which are related?

e.g.:

Code:
int test1; //setting and reading bits per bit operations
bool test2[16];//setting and reading bits per array element access

Please help!

Yours,

Stefan
9  Using Arduino / Programming Questions / Avoiding delay in a nested code segment on: April 25, 2013, 01:32:31 pm
This code is measuring the back emf of a dc motor every 200ms and regulates the rpm of the motor shaft.
My problem is how to avoid the delayMicroseconds (for measuring the back emf) in my code:

The current code (uploaded here: http://bazaar.launchpad.net/~tuxstef/mrsd/trunk/view/head:/DrillBackEmf/arduino/DrillBackEMF.ino):

Code:
#include <PID_v1.h>

const byte ain2 = A2;
const byte motor = 3;
const byte up = 7;
const byte down = 8;
const byte led_red = 13;
const byte led_green = 12;
const byte ain0 = A0;
const byte ain1 = A1;

int old_emf = 0;
int cur_emf = 0;
int current = 0;
int current_old = 0;
int i = 0;
int pulse = 0;

long previousMillis[5] = {0};
long previousMicros[1] = {0};

int buttonState[2] = {0};             // the current reading from the input pin
bool lastButtonState[2] = {LOW};
long lastDebounceTime[2] = {0};  // the last time the output pin was toggled
long debounceDelay = 50;

bool up_state = LOW;
bool down_state = LOW;

bool read_emf = LOW;

double Setpoint, Input, Output;
PID myPID(&Input, &Output, &Setpoint,0.05,0.5,0.009, DIRECT);

byte cur_motor_speed = 0;                                          //current motor speed

void setup() {

  pinMode(motor, OUTPUT);
  analogWrite(motor, 0);
  pinMode(led_red, OUTPUT);
  pinMode(led_green, OUTPUT);

  pinMode(up, INPUT);
  pinMode(down, INPUT);

  Serial.begin(115200);
 
  Input = 0;
  Setpoint = 0;

  myPID.SetMode(AUTOMATIC);
 
  digitalWrite( led_green, HIGH );
}

void loop() {
 
  long currentMillis = millis();
 
  up_state = digitalRead(up);
  down_state = digitalRead(down); 
 
  if (up_state != lastButtonState[0]) {
    lastDebounceTime[0] = currentMillis;
  }
  if (down_state != lastButtonState[1]) {
    lastDebounceTime[1] = currentMillis;
  }

  if ((currentMillis - lastDebounceTime[0]) > debounceDelay) {
    buttonState[0] = up_state;
  }
  lastButtonState[0] = up_state;
  if ((currentMillis - lastDebounceTime[1]) > debounceDelay) {
    buttonState[1] = down_state;
  }
  lastButtonState[1] = down_state;
 
  if ( currentMillis > previousMillis[1] ) {
    if ( buttonState[0] == HIGH ) {
      if ( Setpoint <= 700 - 20 ) {
        Setpoint += 20;
      }
    }
    if ( buttonState[1] == HIGH ) {
      if ( Setpoint >= 0 + 20 ) {
        Setpoint -= 20;
      }
    }
    //previousMicros[0] = micros() + 2;
    //read_emf = HIGH;
    analogWrite( motor , 0 );
    delayMicroseconds(800);
    cur_emf = analogRead(ain1) - analogRead(ain0);
    if (cur_emf < 0 ) {
      cur_emf = 0;
    }
    previousMillis[1] = currentMillis + 200;
  }

  /*if ( micros() > previousMicros[0] && read_emf == HIGH ) {
    Input = analogRead(ain1) - analogRead(ain0);
    //analogWrite( motor , Output );
    if (Input < 0 ) {
      Input *= -1;
    }
    read_emf = LOW;
  }*/

/*  if (read_emf == HIGH ) {
    Output = 0;
    analogWrite( motor , Output );
  }
  else {
    myPID.Compute();
    analogWrite( motor , Output );
  }*/
  //analogWrite( motor , 130 );

/*  if ( i <  5) {
  old_emf += cur_emf;
  i++;
  }
  else {
    Input =  old_emf / 5;
    old_emf = 0;
    i = 0;
  }*/
  Input = cur_emf;


  myPID.Compute();
  analogWrite( motor , Output ); 
 
  pulse = pulseIn(motor, HIGH);
  current_old = analogRead ( ain2 );
  if ( current_old != 0 ) {
    current = current_old;
  }

 
  if ( currentMillis > previousMillis[0] && current > 638) {
    digitalWrite( led_red, digitalRead ( led_red ) ^ 1);
    previousMillis[0] = currentMillis + 500;
  }
  if ( current < 638 ) {        // ca. 3 ampere
    digitalWrite( led_red, LOW);
  }
 
  if ( currentMillis > previousMillis[3] ) {
    digitalWrite( led_green, digitalRead ( led_green ) ^ 1);
    previousMillis[3] = currentMillis + 700 - Setpoint;
  }
 
  if ( Output == 0 ) {
    digitalWrite( led_green, HIGH );
  }
 
  if ( currentMillis > previousMillis[2] ) {
    Serial.print("Input: ");
    Serial.println(Input, DEC);
    Serial.print("Setpoint: ");
    Serial.println(Setpoint, DEC);
    Serial.print("Output: ");
    Serial.println(Output, DEC);
    Serial.print("current: ");
    Serial.println(current, DEC);
    Serial.print("pulse: ");
    Serial.println(pulse, DEC);
    Serial.print("read_emf: ");
    Serial.println(read_emf, DEC);
    Serial.println(" "); 
     previousMillis[2] = currentMillis + 1500;
  }
 

 
}

For your help I would be very grateful!

Yours,
Stefan
10  Topics / Science and Measurement / Re: PWM shunt current measurement on: July 22, 2012, 12:01:44 am
Hello,

I want to ask new, because the main question could be confusing.

The situation:

There's a pwm driven Motor. On the high side current is measured with a shunt and the corresponding voltage is amplified with an opamp circuit to 0,2 - 4V. This voltage goes to the ain input of the arduino.

So, you know that pwm current isn't smooth so the simplest measurement method is to measure at the right time -> if the digital port of the pwm driver is high.

Now my question again:

Is there a function (or any way) to call to find out that the digital port (pwm driven) is just now high?


Also I could take some true rms ics like the AD636. I won't need it because I don't need any absolute measurements here.

The 3rd method would be to measure like the ad636:

Vrms = (Avg * ( Vin^2) )^(1/2)

Any code suggestions?

Thanks!
11  Topics / Science and Measurement / Re: PWM shunt current measurement on: July 18, 2012, 01:13:06 pm
Thanks to winner10920 !

But this question is unanswered.

Quote
Is there a function to call to get the pwm pin is just now high?

Please help
12  Topics / Science and Measurement / PWM shunt current measurement on: July 17, 2012, 10:52:16 pm
Hello,

How do you get the right time if pwm is used for measuring a shunt voltage? I won't use some electronic circuits except the voltage gain amplifier to scale from e.g. 0-20 mV to 0-4V.
Is there a function to call to get the pwm pin is just now high?

Please help!

Yours, Stefan
13  Using Arduino / LEDs and Multiplexing / Re: Using millis() multiple times on: July 08, 2012, 11:45:39 pm
Quote
Is there a way to start easy a timer based on millis only once
No, but there is no need, you simply copy the value in millis to your own variable as a mark to measure everything else.[/quote]

Could you add a example please?
And what about my break[1] problem (see commented code), any other ideas?

Quote
Is there a way for using an operating system on the AtMega 328?
There is but please don't go there, operating system on boards such as an arduino is death.

You code does seem backwards, you are stepping through each state with a case statement, where as you should be scanning through timers to see if it is time to do anything and doing it if it is.

Could you please explain the last sentence you wrote, because I don't understand it.

Should I take two dimensional array for walking throuh the cases? I think if one port has eg mode[3] active one other mode[4] and the the next also mode[3] that the currentMillis[] are interferecing another.



Thanks a lot!
14  Using Arduino / LEDs and Multiplexing / Re: Using millis() multiple times on: July 08, 2012, 11:58:38 am
Code:
void loop() {
  long currentMillis = millis(); //take the current runtime
 
  // You MUST call the NmraDcc.process() method frequently from the Arduino loop() function for correct library operation
  Dcc.process(); //run the nmra dcc library (decodes  bus signal - I'm getting the set_port[] variable set

  brightness[0] = Dcc.getCV(513); //read the brightness from the eeprom using the nmra dcc lib
  brightness[1] = Dcc.getCV(514);
  brightness[2] = Dcc.getCV(515);
  brightness[3] = Dcc.getCV(516);
  brightness[4] = Dcc.getCV(517);
  brightness[5] = Dcc.getCV(518);
  brightness[6] = Dcc.getCV(519);
  brightness[7] = Dcc.getCV(520);
  brightness[8] = Dcc.getCV(521);
  brightness[9] = Dcc.getCV(522);

  mode[0] = Dcc.getCV(523); // read the mode (0=on/off, 1=flicker fluorescent lamp damaged, 2=welding, 3=vapor lamp, 4=fluorescent lamp, 5 + time(max 2,5s)=blink, 11=nothing
  mode[1] = Dcc.getCV(524);
  mode[2] = Dcc.getCV(525);
  mode[3] = Dcc.getCV(526);
  mode[4] = Dcc.getCV(527);
  mode[5] = Dcc.getCV(528);
  mode[6] = Dcc.getCV(529);
  mode[7] = Dcc.getCV(530);
  mode[8] = Dcc.getCV(531);
  mode[9] = Dcc.getCV(532);


 
  if( FactoryDefaultCVIndex && Dcc.isSetCVReady())
  {
    FactoryDefaultCVIndex--; // Decrement first as initially it is the size of the array
    Dcc.setCV( FactoryDefaultCVs[FactoryDefaultCVIndex].CV, FactoryDefaultCVs[FactoryDefaultCVIndex].Value);
  }

  for ( i = 0; i < 10; i++ ) { //walk through the ports
    if ( set_port[i] == HIGH ) { // go on if defined port goes high
      switch ( mode[i] ) { //decide what mode to use
        case 0:  // on/off
          SoftPWMSet(port[i], brightness[i]);
        break;
        case 1: // flicker tube damaged, goes on/off randomly
          random_value[0] = random(200, 1000); //first random value
          random_value[1] = random(200, 1000); //second random value
 
          if ( currentMillis > previousMillis[0] ) { //if time is come
            SoftPWMSet(port[i], brightness[i]) ; // set to port the brightness on the led
            previousMillis[0] = currentMillis + random_value[0]; //calculate the new time for running the timer
          }

          if ( currentMillis > previousMillis[1] ) {
            SoftPWMSet(port[i], 0) ;
            previousMillis[1] = currentMillis + random_value[1];
          }
        break; 
        case 2: // welding - you know? random flashes of one led
          random_value[2] = random(50, 100);
          random_value[3] = random(50, 100);
          random_value[4] = random(500, 10000);

          if ( currentMillis > previousMillis[2] && break_[0] == HIGH) {
            SoftPWMSet(port[i], brightness[i]) ;
            previousMillis[2] = currentMillis + random_value[2];
          }

          if ( currentMillis > previousMillis[3] ) {
            SoftPWMSet(port[i], 0);
            previousMillis[3] = currentMillis + random_value[3];
          }

          if ( currentMillis > previousMillis[4] ) {
            break_[0] ^= 1;
            previousMillis[4] = currentMillis + random_value[4];
          }
        break;
        case 3: // vapor lamp, starts with a flash and then the brightness goes slowly to max brightness
          if ( break_[1] == LOW ) { //be shure to run once (the flash at the beginning)
            SoftPWMSet(port[i], 255); //set the led port to maximum
            previousMillis[5] = currentMillis + 20; //set the new timer time
            break_[1] = HIGH; //be shure this timer runs only once - I have problems with this how to start this case again with set_port[i] == true
            break_[2] = LOW; //set the second timer
            j = 0; // couter variable for fading
          }
          if ( currentMillis > previousMillis[5] && j < brightness[i] && break_[2] == LOW ) { //start the second timer if time is come and brightness isn't maximum
            SoftPWMSet(port[i], j++ ); //increase the brightness
            previousMillis[5] = currentMillis + 200; // set the next time the second timer should run
          }
          if ( j == brightness[i] ) { //stop the second timer, brightness maximum is reached
            break_[2] = HIGH;
          }
        break;
        case 4: // flourescent light, flashes once, then full light, like case 3 but simpler
          if ( break_[3] == LOW ) {
            SoftPWMSet(port[i], 255);
            previousMillis[6] = currentMillis + 100;
            break_[3] = HIGH;
            break_[4] = LOW;
          }
          if ( currentMillis > previousMillis[6] && break_[4] == LOW) {
            SoftPWMSet(port[i], 0 );
            previousMillis[7] = currentMillis + 300;
            break_[4] = HIGH;
            break_[5] = LOW;
          }         
          if ( currentMillis > previousMillis[7] && break_[5] == LOW) {
            SoftPWMSet(port[i], brightness[i] );
            break_[5] = HIGH;
          }
        break;
        case 5: // 5 + time(max 2,5s)=blink
          if ( currentMillis > previousMillis[8]) {
            if ( last[0] == 0 ) {
              last[0] = brightness[i];
              SoftPWMSet(port[i], last[0] );
            }
            else {
              last[0] = 0;
              SoftPWMSet(port[i], last[0] );
            }
            previousMillis[8] = currentMillis + 500;
          }         
        break;
        case 6: // 5 + time(max 2,5s)=blink
          if ( currentMillis > previousMillis[9]) {
            if ( last[0] == 0 ) {
              last[0] = brightness[i];
              SoftPWMSet(port[i], last[0] );
            }
            else {
              last[0] = 0;
              SoftPWMSet(port[i], last[0] );
            }
            previousMillis[9] = currentMillis + 1000;
          }         
        break;
        case 7: // 5 + time(max 2,5s)=blink
          if ( currentMillis > previousMillis[10]) {
            if ( last[0] == 0 ) {
              last[0] = brightness[i];
              SoftPWMSet(port[i], last[0] );
            }
            else {
              last[0] = 0;
              SoftPWMSet(port[i], last[0] );
            }
            previousMillis[10] = currentMillis + 1500;
          }         
        break;
        case 8: // 5 + time(max 2,5s)=blink
          if ( currentMillis > previousMillis[11]) {
            if ( last[0] == 0 ) {
              last[0] = brightness[i];
              SoftPWMSet(port[i], last[0] );
            }
            else {
              last[0] = 0;
              SoftPWMSet(port[i], last[0] );
            }
            previousMillis[11] = currentMillis + 2000;
          }         
        break;
        case 9: // 5 + time(max 2,5s)=blink
          if ( currentMillis > previousMillis[12]) {
            if ( last[0] == 0 ) {
              last[0] = brightness[i];
              SoftPWMSet(port[i], last[0] );
            }
            else {
              last[0] = 0;
              SoftPWMSet(port[i], last[0] );
            }
            previousMillis[12] = currentMillis + 2500;
          }         
        break;
      }
    }
    else {
      SoftPWMSet(port[i], 0);     
    }
    old_port[i] = set_port[i];
  }
if ( currentMillis > previousMillis[19] ) {

      Serial.print("set_port[0] :");
      Serial.println(set_port[0], DEC);
      Serial.print("break_[1] :");
      Serial.println(break_[1], DEC);
     
      previousMillis[19] = currentMillis + 1500;
}
 
 
}

I've added comments, and hope you could help me to do this right!

Thanks.
15  Using Arduino / LEDs and Multiplexing / Using millis() multiple times on: July 08, 2012, 01:36:12 am
Hello,

I've a annoying problem for you. I want to build a model railroad decoder that simulates serveral kinds of lamps, eg. a vapour lamp or a fluorescent lamp. So I have multiple timers on multiple times. I use the    
Model Railroading with  Arduino library for decoding the bus signal and set up parameters.

my current code (that doesn't really work) is attached.


Please take a look at the for loop. It should walk through the 10 ports with leds wired onto them and decides which function (eg. vapor lamp, case 3) should be simulated. But this code isn't working. Should I use two dimensional arrays for previousMillis[]? Is there a way to start easy a timer based on millis only once ( see:   case 3: // vapor lamp
          if ( break_[1] == LOW ) {
)? There's also a promlem: How could I set break_[1] to LOW if I want to stop the light and switch on again without influencing the other leds?
Is there a timer function possible which is easy to use like delay()?
Is there a way for using an operating system on the AtMega 328?

Please help!

Yours, Stefan
Pages: [1] 2