Show Posts
Pages: [1] 2
1  Using Arduino / Programming Questions / Re: Pause for loop without delay on: January 10, 2013, 08:51:53 am
Thanks for the replies.
Yes, I am familiar with the blink without delay example, but unfortunately this method does not work in a for loop.

If we work with the millis() timer in void loop() then the if condition will be triggered constantly, because  void loop() is constantly looping.
Quote
void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  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 (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}


However, what happens with the same method in the for loop below is that the for loop will enter once in the if condition and then loop very very quickly through the remaining 79 times, never entering the if condition again.

Quote
byte dTime; // Speed of drive's reset in milliseconds
unsigned long previousMillis;

void resetOne(){
   digitalWrite(pin+1,HIGH);  // Go in reverse
   
   for(byte s = 0;s < 80; s++){ // Loop through all tracks
     
     unsigned long currentMillis = millis();
     
      if (currentMillis - previousMillis > (dTime*1000)){
         previousMillis = millis();
         
         digitalWrite(pin,HIGH);           
         digitalWrite(pin,LOW);  }
    }
  
      currentPosition[pin] = 0; // We're reset.
      digitalWrite(pin+1,LOW);
      currentState[pin+1] = 0; // Ready to go forward. 
}
  


Anybody an idea how it is possible to pause a for loop without a delay?

Thanks!
2  Using Arduino / Programming Questions / Pause for loop without delay on: January 09, 2013, 04:53:09 am
Hi everybody,
the code below resets a floppy drive.
I want to reset individual drives without slowing down all the other routines going on at the same time.
Any suggestions how I could pause this for loop without the use of a delay?

Quote
byte  dTime; //Speed of drive's reset

void resetOne(){
   for(byte s=0;s<MaxTracks;s++){ // Loop through all 80 tracks of the floppy drive
      digitalWrite(pin+1,HIGH);  // Go in reverse
      digitalWrite(pin,HIGH);
      digitalWrite(pin,LOW);
      delay(dTime);  }
  
      currentPosition[pin] = 0; // We're reset.
      digitalWrite(pin+1,LOW);
      currentState[pin+1] = 0; // Ready to go forward.   
}
  

3  Using Arduino / Programming Questions / Writing directly to ports on: August 28, 2012, 07:58:58 am
Hi folks,
for speeding up my code I want to avoid the good old digitalWrite() funtion.
Therefore I directly write to the ports.

But it turned out that the really simple line of code shown below is quite tricky to write for direct port access.

Quote

digitalWrite(pin, value[pin]);


What I intend to do here is controlling the even pins 2-12 on an Arduino Uno sending them either HIGH or LOW. Below is my current version of that:
Quote

byte value[6]; // Value can be either 0 or 1
byte pin;   // Pins are ONLY even pins 2-12 on an Arduino Uno


 if (value[pin] == 0) {
    if (pin > 1 && pin < 8 ) PORTD = PORTD &= ~(1 << pin);   //=digitalWrite(pin,LOW) for pins 2-6
      else PORTB = PORTB &= ~(1 << (pin-8));  }            //=digitalWrite(pin,LOW) for pins 8-12
      
      else {
        if (pin > 1 && pin < 8 ) PORTD = PORTD |= (1 << pin); //=digitalWrite(pin,HIGH) for pins 2-6
          else PORTB = PORTB |= (1 << (pin-8));  }           //=digitalWrite(pin,HIGH) for pins 8-12
    
   


It works, but seems really a lot of code to me.

Is there any other and less code consuming way to accomplish the same thing?

Thanks for your help!
4  Using Arduino / Programming Questions / Re: digitalWriteFast: compiling error on: August 28, 2012, 04:04:28 am
I got the folder "dwf.zip" from this link:
http://code.google.com/p/digitalwritefast/downloads/list
5  Using Arduino / Programming Questions / Re: digitalWriteFast: compiling error on: August 27, 2012, 01:33:39 pm
Thanks for the response.

Unfortunatey using the modified code below I get the same error message.

I am puzzled at the moment.

Any further suggestions???


Quote
#include <digitalWriteFast.h>
#define led 13

void setup() {               
  pinMode(led, OUTPUT);     
}

void loop() {
  digitalWriteFast(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWriteFast(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

6  Using Arduino / Programming Questions / Re: digitalWriteFast: compiling error on: August 27, 2012, 05:25:56 am
Hi Nick, hi everybody,
unfortunately yes, I restarted my IDE.

I also just restarted my computer and I got the same error message:

Blink_with_digitalWriteFast.cpp: In function 'void loop()':
Blink_with_digitalWriteFast:9: error: invalid type argument of 'unary *'
Blink_with_digitalWriteFast:11: error: invalid type argument of 'unary *'


I know this is weird, as I also expected it to work.

Can it have something to do with my operating system???
I am currently working with Windows XP.
7  Using Arduino / Programming Questions / digitalWriteFast: compiling error on: August 27, 2012, 04:06:38 am
Hi everybody,
I was curious and wanted to test the digitalWriteFast function for later use in time critical applications.
Therefore I did as suggested: I put the folder called digitalWriteFast, containing digitalWriteFast.h and a keyword file, into my arduino library alongside the EEPROM, Ethernet, Firmata, etc folders..

However, if I compile the simple sketch below I get the following error messages:

Blink_with_digitalWriteFast.cpp: In function 'void loop()':
Blink_with_digitalWriteFast:14: error: invalid type argument of 'unary *'
Blink_with_digitalWriteFast:16: error: invalid type argument of 'unary *'

I really don't understand that. Any ideas?
I am working with an Arduino UNO and arduino 1.0.1 at the moment.

Any help or hints would be highly appreciated. Thanks!

Quote
#include <digitalWriteFast.h>
 
int led = 13;

void setup() {               
  pinMode(led, OUTPUT);     
}

void loop() {
  digitalWriteFast(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWriteFast(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}


8  Using Arduino / Interfacing w/ Software on the Computer / Max/MSP to arduino_Serial object_problem after re-opening on: August 15, 2012, 06:50:49 am
Hi everybody,
I have an arduino code that enables me to send 3-byte MIDI messages (MIDI channel, MIDI note number, Velocity) from Max/MSP to arduino over the serial port. Just to make sure I seperate the MIDI messages with a carriage return (13). This basically works like expected and fine.

However, when closing the Max [serial] and opening it again, I ALWAYS get a wrong result the first time. Assuming I close and re-open [serial] and then send it a message like [144, 66, 66, 13] I will get a result like [254, 254, 144]. Sending the same message a second time outputs the correct result.

I really don't understand that. Does anybody have a clue why it behaves like that?

I also tested it with Pure data and got the same result.
Below the arduino code with the simple Max patch.
I currently work under Windows XP with Max 5 and Arduino 1.0.1.

Thanks a lot for your time and help!

Quote

byte buffer[] = {1, 2, 3};  // Serial Input-Buffer containing our 3-byte MIDI messages
byte SerialData = 0; 
byte index;   

void setup(){
  Serial.begin(57600); }

void loop(){/* empty */}

void serialEvent(){
   SerialData = Serial.read();
   buffer[index] = SerialData;
   index++;
   if(SerialData == 13) // Just to make sure we send a carriage return after each MIDI message
     evalSerialData();
   }

void evalSerialData(){
    Serial.write(buffer[0]);
    Serial.write(buffer[1]);
    Serial.write(buffer[2]);
   
     buffer[0] = 0; buffer[1] = 0; buffer[2] = 0;
     index = 0; 
   
 }
 
 /*
 
----------begin_max5_patcher----------
727.3ocwW0yaaCCDc1AH+GNnYWCQpu6VQ6PyPlR2BBJnkNayBIRCIpJmFz+6
kjRxw1Io9K4jA6y9HEuGeu6HO8z0WMxYpbEV4.eFtGFM5IsmQVeFOi5cLxof
sJMmUYmnSprn.EJmwcCpvUJ6.eMWVg.SjAxkn.pvRNKGjS+ElprtaXpzE.Th
U04JfKfaYq.EVVvEr70qWNWfoxZgcQo8dE05YkiJKFH8dmIEpJ9ePqS5D2d+
KMQhKl+yRcra2djjP83.Ip0j3ZLd9SbgG1HFxZUePb2LHBVgMHNewroViUdl
0odO9IB0w57uWekwpMiOeJ8GK3Ufcy.r7bYSEnjZhUykEXUEaNVAyJkEsDoD
XkY0bgbB7cYC9arbLjp0DMOrVUL+Vs.2QbZ344PJqVKepELkcFy3kJc33EHz
fvRsnUAx5R31a91M8Q2Lh8YmiZEFZJk5kuSdumF3OFreQ78eXxqqu9aRxyXo
3Vx6YK5w9FUlFa0ZRjwDRGNI28BH42YT2sH4mEVmAiXnjDaY.0qsnvRMT2Ai
ZRNclQfM5U3kDSWJKBAQgttCGUPhBra9DhwDGtOlX8QRs9TOtDaWHGtQOAGm
mexCLOZ3IqkkZv.2gaExghp77ZOCkDOvYMmwQncEKujIzG9nK5Ca+P7dcxfd
JjQ64I8kPtsl32lLHuYhyQmw3M7YLJXJLjoJcrSf0DEe5UUSYh4mTYk+vSRY
XN6QsVeLMqbN4Vw1So8I6qWEx+m8NVlK3BTGZ5DAGtpOJs8JKhka7idGK9Bu
.rio2rAjbRnaPNdumjSztjS6BXKT18EMrH2LvNTVktOyzdDzdA4XXCvmgUJ8
qLn3RwFSht8jVvyxPwVWBUvyVJ02K1gC3g2VGOXn4qiJYePi7g.sfCg0NZnQ
ngSBLM1aMDcG18+aP.c3EAzTuNPmrEnSFHP6cHf1+CK+buPK3CAZQWDo1ykb
nRswg17OwTd2t.
-----------end_max5_patcher-----------

*/


9  Forum 2005-2010 (read only) / Troubleshooting / Re: error: 'printInteger' was not declared... on: February 18, 2009, 02:26:11 am
Thanks a lot for your kind advice.
I replaced printInteger with Serial.print and now it compiles again and the code works again.

Posted it also to the bugs section, a bit annoying when functions are removed  from the source code :-/
10  Forum 2005-2010 (read only) / Troubleshooting / error: 'printInteger' was not declared... on: February 17, 2009, 03:33:18 pm
Hi,
I tried to compile an old arduino code that was working earlier, but now I get the following error message:

In function 'void loop()':
error: 'printInteger' was not declared in this scope


This is weird, because I have used this code earlier without any problems and I haven`t changed it since. Isn`t 'printInteger' a arduino function and should therefore be part of the arduino software?

How can I solve this problem?

Thanks a lot for your help.
11  Forum 2005-2010 (read only) / Bugs & Suggestions / error: 'printInteger' was not declared in this sco on: February 18, 2009, 02:09:56 am
Hi,
I tried to compile an old arduino code that was working earlier, but now I got the following error message:

In function 'void loop()':
error: 'printInteger' was not declared in this scope


This is weird, because I have used this code earlier without any problems and I haven`t changed the code since.
Isn`t 'printInteger' an arduino function and should therefore be part of the arduino software?
Was that function removed from arduino?

I got an advice to replaced printInteger with Serial.print and now it compiles again and the code works again.

Back to top    
12  Forum 2005-2010 (read only) / Syntax & Programs / Re: pulseIn timeout not working on: November 06, 2010, 10:03:01 am
This might come a bit late, but did you finally figure out how to do that?
I have precisely the same problem: I want to limit the amount of time PING))) is waiting with timeout. How can I rewrite the pulseIn() function so that  the sensor gives up waiting for an echo after timeout's duration in microseconds?
13  Forum 2005-2010 (read only) / Syntax & Programs / Re: maximum duration of unsigned long on: November 18, 2010, 05:13:48 am
Hi  BKnight760,
thanks for your response. That was a fantastic explanation of the topic. Just in case other people want to follow that topic: at the end you used millis() while you were still talking about micros(). I did the same mistake myself smiley

If I understood you correctly: in case micros() overflows after startTime has stored its value, we'd receive a decreasing instead of an increasing result. This is a possibility at least for one iteration of the loop, as after that startTime would be reset to the new "overflown" micros() value. A small risk, but knowing that it may occur I prefer to switch to another solution.

It's a pity, but it seems that the only solution is to abandon counting in micros() and use this instead:
Code:
pinMode(ultraSoundSignal, INPUT);        

val = digitalRead(ultraSoundSignal);

while(val == LOW) {                      
   val = digitalRead(ultraSoundSignal); }

while(val == HIGH) {                    
   val = digitalRead(ultraSoundSignal);
   timecount = timecount++;    
    
     if(timecount > 600) {              
        val = LOW;                        
        timecount = 0;    }              
                   }
   return timecount;  
 }

I discovered one weird thing though: counting a distance of 47,5 cm with the previous timecount im micros() results in 2700 microseconds,
Code:
timecount = micros() - startTime;
but with this code
Code:
timecount = timecount++;  

a distance of 47,5 cm results in 600.

Why is this method of counting time slower than the counting in micros()?
14  Forum 2005-2010 (read only) / Syntax & Programs / Re: maximum duration of unsigned long on: November 17, 2010, 12:57:43 pm
Hi PaulS,
you are absolutely right. I have to apologize.
I made a mistake formulating my question.
In my question I refer to micros() and NOT to millis().
This is why I came up with 71 minutes.
Sorry, but now I'd like to ask the updated version of the question:
What happens after 4,294,967,295 microseconds in the code below?
Will I get problems after 71 minutes?
How could I avoid that?

Code:
val = digitalRead(ultraSoundSignal);

while(val == LOW) {                      
   val = digitalRead(ultraSoundSignal);
   startTime = micros();
                            }

while(val == HIGH) {                    
   val = digitalRead(ultraSoundSignal);
   timecount = micros() - startTime;    
    
     if(timecount > 2700) {              
        val = LOW;                      
        timecount = 0;      }              
                   }
15  Forum 2005-2010 (read only) / Syntax & Programs / maximum duration of unsigned long on: November 17, 2010, 11:13:14 am
Hi folks,
I was just wondering today:
What happens with an unsigned long number that counts millis() after 71 minutes? Will it stop working properly?
I assume so, as in the definition of unsigned long I read that it has a range from 0 to 4,294,967,295.
Thus it will reach  4,294,967,295 microseconds after 71 minutes and what will it do then?
In my code I use something like that
Code:
unsigned long startTime = 0;          
         startTime = micros();  

Is my guess correct or won't there be any problem?
If yes, how could I solve it?
Pages: [1] 2