Show Posts
Pages: 1 2 3 [4] 5 6 ... 8
46  Forum 2005-2010 (read only) / Troubleshooting / Re: Up and Down counter with Debounce on: January 27, 2010, 03:38:52 pm
Basically, the code sends a channel # Ex. "+TSC04" and if it echos back, the Arduino will display the current channel on the BCD/7-segment display...so if I could loop both serial pins it would act like I have it connected to the radio I plan to change channels too.

But it was a no-go when I did the mentioned hoop up wire from Pin0 to Pin1.
47  Forum 2005-2010 (read only) / Troubleshooting / Re: Up and Down counter with Debounce on: January 27, 2010, 03:26:27 pm
OK!!!

So once millis reaches the maximum value, then it rolls over to 0, good to know!!!!

My code works perfectly when powered on USB to the terminal window.

My issue now is:

I power the Arduino Duemilanove ATMEGA168 board externally with 12V, since Rx and Tx are TTL, I thought I could loop Pin0 and Pin1, but it does not seem to work...

How do you make a Loop on the Arduino Pin0 and Pin1?
48  Forum 2005-2010 (read only) / Troubleshooting / Re: Up and Down counter with Debounce on: January 27, 2010, 02:57:22 pm
I just edited the post above...
49  Forum 2005-2010 (read only) / Troubleshooting / Re: Up and Down counter with Debounce on: January 27, 2010, 02:41:17 pm
I read on http://cboard.cprogramming.com/c-programming/89575-int-vs-long.html

That int = -32768 to 32767 and long = -2147483648 to 2147483647

So I changed

Code:
int chanPrevPoll = 0;

to

Code:
long chanPrevPoll = 0;

Now I get
Code:
+TSC?
millis() returned: 10018
chanPrevPoll = 10001chanPolling = 10000
+TSC?
millis() returned: 20020
chanPrevPoll = 20002chanPolling = 10000
+TSC?
millis() returned: 30020
chanPrevPoll = 30003chanPolling = 10000
+TSC?
millis() returned: 40022
chanPrevPoll = 40004chanPolling = 10000
+TSC?
millis() returned: 50023
chanPrevPoll = 50006chanPolling = 10000
+TSC?
millis() returned: 60024
chanPrevPoll = 60007chanPolling = 10000
+TSC?


And on, so it is fixed.

Also I was wondering, since Millis overflows after 9 hours or so per this post

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1167861718

How do I do about resetting millis to 0 or is there a better way instead of millis? I can't understand the commands they reference too... still trying...
50  Forum 2005-2010 (read only) / Troubleshooting / Re: Up and Down counter with Debounce on: January 27, 2010, 01:39:14 pm
PaulS:

I replaced the suggested code, now I call the function without the use of a variable, and I declare the char chan on top.

Now this is what I get on the serial window:

Code:
+TSC?
+TSC?
+Tmillis() returned: 10018
chanPrevPoll = 10001chanPolling = 10000
+TSC?
millis() returned: 20020
chanPrevPoll = 20002chanPolling = 10000
+TSC?
millis() returned: 30020
chanPrevPoll = 30003chanPolling = 10000
+TSC?
millis() returned: 40022
chanPrevPoll = -25532chanPolling = 10000
+TSC?
millis() returned: 40099
chanPrevPoll = -25456chanPolling = 10000
+TSC?
millis() returned: 40177
chanPrevPoll = -25378chanPolling = 10000
+TSC?
millis() returned: 40255
chanPrevPoll = -25300chanPolling = 10000
+TSC?
millis() returned: 40333
chanPrevPoll = -25223chanPolling = 10000
+TSC?
millis() returned: 40412
chanPrevPoll = -25145chanPolling = 10000
+TSC?
millis() returned: 40489
chanPrevPoll = -25066chanPolling = 10000
+TSC?
millis() returned: 40567
chanPrevPoll = -24988chanPolling = 10000
+TSC?
millis() returned: 40645
chanPrevPoll = -24910chanPolling = 10000
+TSC?
millis() returned: 40723
chanPrevPoll = -24832chanPolling = 10000
+TSC?
millis() returned: 40802
chanPrevPoll = -24755chanPolling = 10000
+TSC?
millis() returned: 40880
chanPrevPoll = -24677chanPolling = 10000
+TSC?
millis() returned: 40957
chanPrevPoll = -24598chanPolling = 10000


takes 10 seconds between:

Code:
millis() returned: 30020
chanPrevPoll = 30003chanPolling = 10000
+TSC?

and

Code:
millis() returned: 40022
chanPrevPoll = -25532chanPolling = 10000
+TSC?

Right after this line is all nuts...



51  Forum 2005-2010 (read only) / Troubleshooting / Re: Up and Down counter with Debounce on: January 27, 2010, 12:13:51 pm
Thank you PaulS for the Function Idea, now it only runs the code on channel variable change.

Now the crazy polling, it takes exactly 42 seconds every time I reset the Arduino.  This is what it looks like

Code:
+TSÿ
!Z'á+TSC?
+TSC?
+TSC?
+TSC?

Right before it goes nuts...

Here is the complete code at the moment...

Code:
const int inPinUp = 6;
const int inPinDown = 7;
int channel = 1;
int buttonUpState = 0;
int buttonDownState = 0;
int prevBtnUp = LOW;
int prevBtnDwn = LOW;
unsigned long lastBtnUp = 0;
unsigned long lastBtnDwn = 0;
int transInt = 50;
int chanPolling = 10000;
int chanPrevPoll = 0;
int TSC = channelFunction();


void setup()
{
  Serial.begin(9600);
  pinMode(inPinUp, INPUT);
  pinMode(inPinDown, INPUT);
  for (int updown = 6; updown < 8; updown++) {
         pinMode(updown, INPUT);
       }
  for (int BCD = 2; BCD < 6; BCD++) {
         pinMode(BCD, OUTPUT);
       }
  
}

int channelFunction()
{
  switch (channel)
      {
      case 1:
        Serial.println("+TSC01");
        break;
      case 2:
        Serial.println("+TSC02");
        break;
      case 3:
        Serial.println("+TSC03");
        break;
      case 4:
        Serial.println("+TSC04");
        break;
      case 5:
        Serial.println("+TSC05");
        break;
      case 6:
        Serial.println("+TSC06");
        break;
      case 7:
        Serial.println("+TSC07");
        break;
      case 8:
        Serial.println("+TSC08");
        break;
      case 9:
        Serial.println("+TSC09");
        break;
       }
}
void loop()
{  
  buttonUpState = digitalRead(inPinUp);
  buttonDownState = digitalRead(inPinDown);
  if (buttonUpState == HIGH && prevBtnUp == LOW)
  {
    if (millis() - lastBtnUp > transInt)
    {
    channel++;
    if (channel > 9)
      {
      channel = 1;
      }
    lastBtnUp = millis();
    TSC;
    }
   }
  prevBtnUp = buttonUpState;
  
  if (buttonDownState == HIGH && prevBtnDwn == LOW)
  {
    if (millis() - lastBtnDwn > transInt)
    {
    channel--;  
    if (channel < 1)
    {
      channel = 9;
    }
    lastBtnDwn = millis();
    TSC;
  }
  }
  prevBtnDwn = buttonDownState;

      if (millis() - chanPrevPoll > chanPolling)
  {
    chanPrevPoll = millis();
    Serial.println("+TSC?");
  }
  if (Serial.available() > 0)
  {
     int chan = Serial.read();
     switch ((char)chan){
     case '+TSC01':    
       digitalWrite(2, LOW);
       digitalWrite(3, LOW);
       digitalWrite(4, LOW);
       digitalWrite(5, HIGH);
       break;
     case '+TSC02':    
       digitalWrite(2, LOW);
       digitalWrite(3, LOW);
       digitalWrite(4, HIGH);
       digitalWrite(5, LOW);
       break;
     case '+TSC03':    
       digitalWrite(2, LOW);
       digitalWrite(3, LOW);
       digitalWrite(4, HIGH);
       digitalWrite(5, HIGH);
       break;
     case '+TSC04':    
       digitalWrite(2, LOW);
       digitalWrite(3, HIGH);
       digitalWrite(4, LOW);
       digitalWrite(5, LOW);
       break;
     case '+TSC05':    
       digitalWrite(2, LOW);
       digitalWrite(3, HIGH);
       digitalWrite(4, LOW);
       digitalWrite(5, HIGH);
       break;
     case '+TSC06':    
       digitalWrite(2, LOW);
       digitalWrite(3, HIGH);
       digitalWrite(4, HIGH);
       digitalWrite(5, LOW);
       break;
     case '+TSC07':    
       digitalWrite(2, LOW);
       digitalWrite(3, HIGH);
       digitalWrite(4, HIGH);
       digitalWrite(5, HIGH);
       break;
     case '+TSC08':    
       digitalWrite(2, HIGH);
       digitalWrite(3, LOW);
       digitalWrite(4, LOW);
       digitalWrite(5, LOW);
       break;
     case '+TSC09':    
       digitalWrite(2, HIGH);
       digitalWrite(3, LOW);
       digitalWrite(4, LOW);
       digitalWrite(5, HIGH);
       break;
     default:
       for (int BCD = 2; BCD < 6; BCD++) {
         digitalWrite(BCD, LOW);
       }
     }
  }
}

I gotten this far thanks to you  smiley-wink
52  Forum 2005-2010 (read only) / Troubleshooting / Re: Up and Down counter with Debounce on: January 26, 2010, 11:51:07 pm
It does after I use the buttons for channel up or down or after like a minute, very odd.
53  Forum 2005-2010 (read only) / Troubleshooting / Re: Up and Down counter with Debounce on: January 26, 2010, 05:56:18 pm
Now, I have another issue, after this my code is finished.

I like this part of my code to only run when I push a button , that is so it prints +TSC01 or +TSC02 etc only when I change the variable channel. But it prints it indefinitely, also after some time it starts polling indefinitely "+TSC?" , not sure why...still looking.

Code:
switch (channel)
      {
      case 1:
        Serial.println("+TSC01");
        break;
      case 2:
        Serial.println("+TSC02");
        break;
      case 3:
        Serial.println("+TSC03");
        break;
      case 4:
        Serial.println("+TSC04");
        break;
      case 5:
        Serial.println("+TSC05");
        break;
      case 6:
        Serial.println("+TSC06");
        break;
      case 7:
        Serial.println("+TSC07");
        break;
      case 8:
        Serial.println("+TSC08");
        break;
      case 9:
        Serial.println("+TSC09");
        break;
       }

Here is the whole code

Code:
const int inPinUp = 6;
const int inPinDown = 7;
int channel = 1;
int buttonUpState = 0;
int buttonDownState = 0;
int prevBtnUp = LOW;
int prevBtnDwn = LOW;
unsigned long lastBtnUp = 0;
unsigned long lastBtnDwn = 0;
int transInt = 50;
int chanPolling = 10000;
int chanPrevPoll = 0;


void setup()
{
  Serial.begin(9600);
  pinMode(inPinUp, INPUT);
  pinMode(inPinDown, INPUT);
  for (int updown = 6; updown < 8; updown++) {
         pinMode(updown, INPUT);
       }
  for (int BCD = 2; BCD < 6; BCD++) {
         pinMode(BCD, OUTPUT);
       }
  
}


void loop()
{
  buttonUpState = digitalRead(inPinUp);
  buttonDownState = digitalRead(inPinDown);
  if (buttonUpState == HIGH && prevBtnUp == LOW)
  {
    if (millis() - lastBtnUp > transInt)
    {
    channel++;
    if (channel > 9)
      {
      channel = 1;
      }
    lastBtnUp = millis();
    }
   }
  prevBtnUp = buttonUpState;
  
  if (buttonDownState == HIGH && prevBtnDwn == LOW)
  {
    if (millis() - lastBtnDwn > transInt)
    {
    channel--;
    if (channel < 1)
    {
      channel = 9;
    }
    lastBtnDwn = millis();
  }
  }
  prevBtnDwn = buttonDownState;

      switch (channel)
      {
      case 1:
        Serial.println("+TSC01");
        break;
      case 2:
        Serial.println("+TSC02");
        break;
      case 3:
        Serial.println("+TSC03");
        break;
      case 4:
        Serial.println("+TSC04");
        break;
      case 5:
        Serial.println("+TSC05");
        break;
      case 6:
        Serial.println("+TSC06");
        break;
      case 7:
        Serial.println("+TSC07");
        break;
      case 8:
        Serial.println("+TSC08");
        break;
      case 9:
        Serial.println("+TSC09");
        break;
       }
    if (millis() - chanPrevPoll > chanPolling)
  {
    chanPrevPoll = millis();
    Serial.println("+TSC?");
  }
  if (Serial.available() > 0)
  {
     int chan = Serial.read();
     switch ((char)chan){
     case '+TSC01':    
       digitalWrite(2, LOW);
       digitalWrite(3, LOW);
       digitalWrite(4, LOW);
       digitalWrite(5, HIGH);
       break;
     case '+TSC02':    
       digitalWrite(2, LOW);
       digitalWrite(3, LOW);
       digitalWrite(4, HIGH);
       digitalWrite(5, LOW);
       break;
     case '+TSC03':    
       digitalWrite(2, LOW);
       digitalWrite(3, LOW);
       digitalWrite(4, HIGH);
       digitalWrite(5, HIGH);
       break;
     case '+TSC04':    
       digitalWrite(2, LOW);
       digitalWrite(3, HIGH);
       digitalWrite(4, LOW);
       digitalWrite(5, LOW);
       break;
     case '+TSC05':    
       digitalWrite(2, LOW);
       digitalWrite(3, HIGH);
       digitalWrite(4, LOW);
       digitalWrite(5, HIGH);
       break;
     case '+TSC06':    
       digitalWrite(2, LOW);
       digitalWrite(3, HIGH);
       digitalWrite(4, HIGH);
       digitalWrite(5, LOW);
       break;
     case '+TSC07':    
       digitalWrite(2, LOW);
       digitalWrite(3, HIGH);
       digitalWrite(4, HIGH);
       digitalWrite(5, HIGH);
       break;
     case '+TSC08':    
       digitalWrite(2, HIGH);
       digitalWrite(3, LOW);
       digitalWrite(4, LOW);
       digitalWrite(5, LOW);
       break;
     case '+TSC09':    
       digitalWrite(2, HIGH);
       digitalWrite(3, LOW);
       digitalWrite(4, LOW);
       digitalWrite(5, HIGH);
       break;
     default:
       for (int thisPin = 2; thisPin < 6; thisPin++) {
         digitalWrite(thisPin, LOW);
       }
     }
  }
}

How can I achieve this?

54  Forum 2005-2010 (read only) / Troubleshooting / Re: Up and Down counter with Debounce on: January 26, 2010, 04:12:35 pm
PaulS

Your suggested lines of code worked flawlessly

this is my current code, It is far from finished, but this current code can count from 1 to 9 and back and print them on the serial port as you press the up and down buttons.

Code:
const int inPinUp = 6;
const int inPinDown = 7;
int channel = 1;
int buttonUpState = 0;
int buttonDownState = 0;
int prevBtnUp = LOW;
int prevBtnDwn = LOW;
unsigned long lastBtnUp = 0;
unsigned long lastBtnDwn = 0;
int transInt = 50;

void setup()
{
  Serial.begin(9600);
  pinMode(inPinUp, INPUT);
  pinMode(inPinDown, INPUT);
  
}


void loop()
{
  
  buttonUpState = digitalRead(inPinUp);
  buttonDownState = digitalRead(inPinDown);
  
  if (buttonUpState == HIGH && prevBtnUp == LOW)
  {
    if (millis() - lastBtnUp > transInt)
    {
    channel++;
    if (channel > 9)
    {
      channel = 1;
    }
    lastBtnUp = millis();
    Serial.println(channel);
    }
  }
  prevBtnUp = buttonUpState;
  
  if (buttonDownState == HIGH && prevBtnDwn == LOW)
  {
    if(millis() - lastBtnDwn > transInt)
    {
    channel--;
    if (channel < 1)
    {
      channel = 9;
    }
    lastBtnDwn = millis();
    Serial.println(channel);
    
    }
  }
  prevBtnDwn = buttonDownState;
}


Thank you greatly for your help,  smiley-wink
55  Forum 2005-2010 (read only) / Troubleshooting / Re: Up and Down counter with Debounce on: January 26, 2010, 03:15:49 pm
So far, this is the code without the debounce, so as I press either up or down, it will go very far very quick...

Code:
const int inPinUp = 6;
const int inPinDown = 7;
int channel = 1;
int buttonUpState = 0;
int buttonDownState = 0;
void setup()
{
  Serial.begin(9600);
  pinMode(inPinUp, INPUT);
  pinMode(inPinDown, INPUT);
  
}


void loop()
{
  buttonUpState = digitalRead(inPinUp);
  buttonDownState = digitalRead(inPinDown);
  
  if (buttonUpState == HIGH)
  {
   channel++;
  }
  if (buttonDownState == HIGH)
  {
   channel--;
  }
  Serial.println(channel);
}

How do you suggest I do about the debounce for this ?
56  Forum 2005-2010 (read only) / Troubleshooting / Up and Down counter with Debounce on: January 26, 2010, 12:37:16 pm
Greetings!

I need to change a counter variable to go up or down upon the state change of 2 digital input pins on the Arduino. I'm having trouble interfacing it to a debounce code.

The idea is to have counter++ run when pin6 is HIGH and counter-- when pin7 is HIGH, it would be connected to momentary switches.

Any tips or ideas? I have tried the tutorial on Arduino site and seems I need a bit more code than that.
57  Forum 2005-2010 (read only) / Development / Timing diference 2 input pins on: June 29, 2010, 03:36:46 pm
Current idea:

I being wondering if I could use the Arduino to build a project requiring some very precise timing.

The idea is that I want to use a Arduino (or many Arduinos) to tell me the time difference between a pulse received on 2 input pins down to the microseconds.

I have successfully used Pulse in as an "interrupt" to make a frequency counter, so I'm thinking some kind of Interrupt might work to do the time stamps, but I don't know how precise I can make it with an Arduino.

Long term project:

I'm trying to build some timing units that are "extremely" precise. The idea is that I use GPS time on both units that are linked over wireless UHF Serial Link. I also use PPS (Pulse Per Second) from GPS for better precision on both units.

One unit will be set as a Master, another as Slave. Master will decide an event time Ex. (in 3 seconds do an event) and so both are set to do so and when the time comes both units event at "Virtually" the exact time. The Arduino in question I'm working on will be showing the time difference between the events so that adjustments can be done using very precise Oscilloscopes.



My main question really is that I want to know how precise I can get the time difference of 2 pulses on 2 difference input pins, and if the Arduino is not capable of doing it, what is?



58  Forum 2005-2010 (read only) / Interfacing / Re: Code runs every other serial read? on: January 17, 2011, 10:29:17 am
I believe I have found my problem.

I tried what PaulS suggested and added a Serial.Println here

Code:
 if (Serial.available() > 0) {
    incomingByte = Serial.read();
    Serial.println(incomingByte);
  }

To my surprise, it started working with this debugging code added. Now I started to think, maybe it is too much time to wait @ 9600 for the full character to come in and and compare, so maybe that's why only on the second run of the loop is when it reads it, so I figure I try a higher baud rate, 115200, and it started to work very well. Does this make sense?

Graynomad,

As always thanks for the suggested approach, could you explain to me why the while works better with the timing? I'm interested as I will be working a lot more with timing in the near future.

My currently working code

Code:
char incomingByte = 0;
int ppsin = 8;
const uint32_t c = 1;
int CTB = 9;
void setup() {
  Serial.begin(115200);
}

void loop() {

  if (Serial.available() > 0) {
    incomingByte = Serial.read();
  }
  if(incomingByte == 'x')
  {
    for (uint32_t i=c; i>0; i--)
      pulseIn(ppsin,HIGH);
    delayMicroseconds(1895);
    digitalWrite(CTB, HIGH);
    delay(5);
    digitalWrite(CTB, LOW);
    incomingByte = 0;
  }
}


I'll still be working on this for some time until the project starts so I will update as it goes.
59  Forum 2005-2010 (read only) / Interfacing / Re: Code runs every other serial read? on: January 14, 2011, 03:26:39 pm
Thank you for your interest, I will give you some background on my project.

This unit is going to be integrated into a seismic boat mapping oil pockets. Seismic boats work based on creating a sound wave and recording the bounce from the bottom of the ocean, like a sonogram. On the past the crew would use cables with hydrophones that extended for miles that would record the sound waves bounce and therefore create a map. This new boat we are trying to work on uses floating devices that are not connected, instead of recording when a sound wave came in like the wired, this devices record constantly at 2ms intervals, they are sync with GPS PPS. So in order for me to trigger the sound wave, I have to be able to fit the sound on a specific time slot.

The prototype I'm building takes a 10 PPS pulse and based of it, I generates 2ms pulses on one Arduino, that are off only by 20 us at worst case which is acceptable. I have a second Arduino that receives a command to shot(the one I'm working on now), the "x" over serial. When it receives the "x" it will count a certain amount of pulses before outputting a pulse, exactly at one of the 2m pulses I'm generation from Arduino Chip 1.

To answer your question, I have it set to wait for one pulse so I could fit it on the oscilloscope and compare, the value will be closer to 220 pulses or 450 ms from receiving the "x" and outputting a pulse. This will happen every 5-10 second. It all works fine except for the Arduino only pulsing every other "x". I don't need to use the time generated by pulse in, but I use pulse in to count the pulses "or time slots" before outputting my pulse.

I included a screenshot of my picoscope. The Yellow is the rs232 "x" coming in, the Red is the 50us every 2 ms pulses and the Green one is the pulse I'm trying to generate on the time slot, which as you can see it right on it. The time increments or scope of that image is 1ms.

60  Forum 2005-2010 (read only) / Interfacing / Re: Code runs every other serial read? on: January 13, 2011, 11:39:08 pm
I have a 50us pulse coming in every 2 ms on ppsin pin.

CTB pin is my output pulse which is triggered on the incoming x from a Laptop serial.

The whole idea is to be able to output a pulse on CTB pin in sync to the pulses coming in ever 2ms triggered by the x. Everything works great, but it only works on every other x I get. I'm using a quality oscilloscope to check timings and I see the x is coming in every time but not my output pulse on CTB. Nothing is connected to CTB at the moment.
Pages: 1 2 3 [4] 5 6 ... 8