Show Posts
Pages: 1 ... 14 15 [16] 17
226  Using Arduino / General Electronics / Re: Can't get a digital potentiometer to work on: January 31, 2011, 07:17:28 pm
I do seem to be getting repeatable responses. The potentiometer does not seem to have been ruined.

I'm thinking that my SPI codes may be incorrect. I'm supposed to send 10-bit commands. Perhaps by sending them as two 8-bit words I am screwing up the order of the bits.

I'm not able to experiment further right now, but will later. Maybe better luck then.
227  Using Arduino / General Electronics / Re: Can't get a digital potentiometer to work on: January 31, 2011, 04:39:34 pm
1) You did not mention where you have the "low" side of the potentiometer connected. Should be GND. Else you will read noise. (which you are doing)
2) You did not mention where you have the "high" side of the potentiometer connected. Should be +5V.  Else you will read noise. (which you are doing)
3) You mentioned that you have the wiper connected to an Arduino analog input, plus a 2.5K ohm resistor to GND.   
a) You have no reason for this resistor, and I see none, at least from what you have revealed. Remove it.
b) That resistor will dramatically overload the output impedance of the pot and you will NEVER read the kind of voltages you are expecting.

Thanks for the reply.
1) I had the low side of the potentiometer floating. I have now connected it to GND.
2) I had the high side of the potentiometer already connected to +5 Volts.
3) I removed the 2.5k Ohm resistor.

The data still looks rather like noise.

Code:
Pot Level:  0 Pot Value:  4
Pot Level:  10 Pot Value:  1023
Pot Level:  20 Pot Value:  1023
Pot Level:  30 Pot Value:  455
Pot Level:  40 Pot Value:  4
Pot Level:  50 Pot Value:  5
Pot Level:  60 Pot Value:  1023
Pot Level:  70 Pot Value:  1023
Pot Level:  80 Pot Value:  1023
Pot Level:  90 Pot Value:  1023
Pot Level:  100 Pot Value:  1023
Pot Level:  110 Pot Value:  1023
Pot Level:  120 Pot Value:  1023
Pot Level:  130 Pot Value:  1023
Pot Level:  140 Pot Value:  1023
Pot Level:  150 Pot Value:  1023
Pot Level:  160 Pot Value:  1023
Pot Level:  170 Pot Value:  1023
Pot Level:  180 Pot Value:  1023
Pot Level:  190 Pot Value:  1023
Pot Level:  200 Pot Value:  337
Pot Level:  210 Pot Value:  4
Pot Level:  220 Pot Value:  217
Pot Level:  230 Pot Value:  1023
Pot Level:  240 Pot Value:  1023
Pot Level:  250 Pot Value:  1023

Pot Level:  255 Pot Value:  1023
Pot Level:  245 Pot Value:  1023
Pot Level:  235 Pot Value:  1023
Pot Level:  225 Pot Value:  1023
Pot Level:  215 Pot Value:  1023
Pot Level:  205 Pot Value:  1023
Pot Level:  195 Pot Value:  1023
Pot Level:  185 Pot Value:  1023
Pot Level:  175 Pot Value:  1023
Pot Level:  165 Pot Value:  1023
Pot Level:  155 Pot Value:  1023
Pot Level:  145 Pot Value:  1023
Pot Level:  135 Pot Value:  1022
Pot Level:  125 Pot Value:  1023
Pot Level:  115 Pot Value:  378
Pot Level:  105 Pot Value:  4
Pot Level:  95 Pot Value:  5
Pot Level:  85 Pot Value:  1023
Pot Level:  75 Pot Value:  1023
Pot Level:  65 Pot Value:  1023
Pot Level:  55 Pot Value:  312
Pot Level:  45 Pot Value:  4
Pot Level:  35 Pot Value:  4
Pot Level:  25 Pot Value:  1023
Pot Level:  15 Pot Value:  1023
Pot Level:  5 Pot Value:  299
Pot Level:  0 Pot Value:  4
Pot Level:  10 Pot Value:  1023
Pot Level:  20 Pot Value:  1023
Pot Level:  30 Pot Value:  1023
Pot Level:  40 Pot Value:  1022
Pot Level:  50 Pot Value:  314
Pot Level:  60 Pot Value:  4
Pot Level:  70 Pot Value:  4
Pot Level:  80 Pot Value:  1023
Pot Level:  90 Pot Value:  1023
Pot Level:  100 Pot Value:  1023
Pot Level:  110 Pot Value:  1023
Pot Level:  120 Pot Value:  1023
Pot Level:  130 Pot Value:  1023
Pot Level:  140 Pot Value:  1023
Pot Level:  150 Pot Value:  1023
Pot Level:  160 Pot Value:  1022
Pot Level:  170 Pot Value:  373
Pot Level:  180 Pot Value:  4
Pot Level:  190 Pot Value:  3
Pot Level:  200 Pot Value:  1023
Pot Level:  210 Pot Value:  1023
Pot Level:  220 Pot Value:  1023
Pot Level:  230 Pot Value:  1023
Pot Level:  240 Pot Value:  1022
Pot Level:  250 Pot Value:  1023

Perhaps I have ruined the potentiometer with my tinkering.
228  Using Arduino / General Electronics / Can't get a digital potentiometer to work on: January 31, 2011, 04:05:44 pm
I'm trying out a different digital potentiometer. A month ago some people on this forum helped me out with another digital potentiometer that worked fine, but had some linearity issues. Now this one does not work at all.

The potentiometer is an Analog Devices AD 8402A10, which is 0 to 10k Ohms in 255 steps. The data sheet is: www.analog.com/static/imported-files/data_sheets/AD8400_8402_8403.pdf

I use the following code to step the potentiometer through steps of 10 up to 250, then down from 255. The output measured by running a wire from the wiper to an Arduino analog input pin. That same wiper pin has a 2.5 k Ohms resistor connected to ground.

Code:
// include the SPI library:
#include <SPI.h>
#define potInputPin 0

// set pin 10 as the slave select for the digital pot:
const int slaveSelectPin = 10;
int potValue;

void setup() {
    // start serial port at 9600 bits per second
  Serial.begin(9600);

  // clear the serial port buffer
  Serial.flush();
// set the slaveSelectPin as an output:
  pinMode (slaveSelectPin, OUTPUT);
  // initialize SPI:
  SPI.begin();
}

void loop() {
    // change the resistance from min to max:
    for (int level = 0; level < 255; level = level + 10) {
      digitalPotWrite(0, level);
      Serial.print("Pot Level:  ");
      Serial.print(level);
      Serial.print("\t");     
      delay(1000);
      potValue = analogRead(potInputPin);
      Serial.print("Pot Value:  ");
      Serial.print(potValue);
      Serial.print("\n");     
    }
    // wait a second at the top:
    Serial.print("\n");     
    delay(1000);
    // change the resistance from max to min:
    for (int level = 0; level < 255; level = level + 10) {
      digitalPotWrite(0, 255 - level);
      Serial.print("Pot Level:  ");
      Serial.print(255 - level);
      Serial.print("\t");     
      delay(1000);
      potValue = analogRead(potInputPin);
      Serial.print("Pot Value:  ");
      Serial.print(potValue);
      Serial.print("\n");     
    }
     delay(1000);
 }

int digitalPotWrite(int command, int value) {
  // take the SS pin low to select the chip:
  digitalWrite(slaveSelectPin,LOW);
  //  send in the value via SPI:
  SPI.transfer(command);
  SPI.transfer(value);
  // take the SS pin high to de-select the chip:
  digitalWrite(slaveSelectPin,HIGH);
}


The Arduino analog input pin shows no sign of a step change. It bounces around between a few values, but there seems no rhyme or reason to them.

Any thoughts?

229  Using Arduino / Programming Questions / Re: adding decimal points and zero padding for display. on: January 31, 2011, 02:11:43 pm
Sounds like you may be bumping into the upper limit for an unsigned integer, which is 65,535.

Quote
Unsigned ints (unsigned integers) are the same as ints in that they store a 2 byte value. Instead of storing negative numbers however they only store positive values, yielding a useful range of 0 to 65,535 (2^16) - 1).
230  Using Arduino / Programming Questions / Re: adding decimal points and zero padding for display. on: January 31, 2011, 01:49:04 pm
Here is an example of the output. Notice the first value is unique, then it oscillates around at some other odd numbers..

Where is that output from? The problem seems to be with your Serial.println(withDot); statement. I ran the other code and it generates exactly what you want it to. That is, you get a character array withDot that has the following in it:
Code:
- withDot 0x001ff7d0 "01.238" char [7]
[0] 48 '0' char
[1] 49 '1' char
[2] 46 '.' char
[3] 50 '2' char
[4] 51 '3' char
[5] 56 '8' char
[6] 0 char
231  Using Arduino / Programming Questions / Re: weird calculation issue on: January 28, 2011, 04:39:39 pm
Quote
is there an easy way to convert a float value to a char array?

This may work: http://www.arduino.cc/playground/Main/FloatToString
232  Using Arduino / Programming Questions / Re: weird calculation issue on: January 28, 2011, 03:03:05 pm
You are right, your types are not consistent.

Quote
  stepAmount = (radius/360)*(wholeTurn/wheelCircumference)*1600*gearRatio;

This will do integer arithmetic, I think. Should be 360.0 and 1600.0 instead.

Quote
float gearRatio = 11/3;

Also, this should be 11.0/3.0

Quote
void doTurn(double radius){ //turn around certain radius

This radius should be float instead of double.
233  Using Arduino / Programming Questions / Re: Looking for general PID code for the Arduino on: January 28, 2011, 11:47:57 am
Thank you.

I see I have been "smitten" using the new forum function. Well deserved, I guess, for not having found the PID library myself. My only excuse is that I have not visited this forum for a few weeks, and was surprised to find the new look. Plus I think I typed PDI in the search box by mistake.

But the links Coding Badly posted helped. There is a full and expertly written PID library that seems to get good reviews. But for me, the PID library requires more knowledge than I had. I wanted to understand what was being done, and why. That was a little hard to do from that code. (I don't mean to criticize the PID library by saying this.)

These two things helped me more:

http://www.eetimes.com/ContentEETimes/Documents/Embedded.com/2000/f-wescot.pdf  This article "PID Without a PhD" goes into the theory behind PID and gives sample code.

http://www.atmel.com/dyn/products/app_notes.asp?family_id=607  You need to scroll down to Atmel Application Note 221 "Discrete PID controller on tinyAVR and megaAVR devices". Then you can find download links for a pdf file with notes and a software file with sample code.

With these two resources, I was quickly able to write a simple PID program that meets my needs. And that I know how to tune and work with.

Others with more demanding applications may find the PID library more helpful.
234  Using Arduino / Programming Questions / Looking for general PID code for the Arduino on: January 28, 2011, 02:42:43 am
I'm looking for the code for a general PID (proportional, integral, differential) control algorithm for the Arduino. Anyone know of any?

Thank you.
235  Forum 2005-2010 (read only) / Interfacing / Re: Send two-byte data from Arduino to PC via USB on: December 29, 2010, 02:48:54 am
The code for both the Arduino and the PC are below.

Your point about putting an identifier on the value (like T255 for throttle) is a good one.

But that does not solve the problem of the bytes getting mixed up in order. That's the tricky thing about this. I cannot figure out why the two bytes are getting switched in their order, but only some of the time.

Here's the Arduino code:

Code:
int potPin = 2; // select the input pin for the potentiometer

float Temp;
int potValue;
unsigned char outputPotValue, Throttle, Brake, WriteBuffer[2];

void setup()
{
  // start serial port at 19200 bits per second
  Serial.begin(19200);

  // clear the serial port buffer
  Serial.flush();
}

void loop()
{
  Serial.flush();
  potValue = analogRead(potPin);
//  Serial.print("PotValue is: ");
//  printNum(potValue);
  if (potValue > 1023) {
    potValue = 1023;
  }
  if ((potValue <= 1023) && (potValue >= 894)) {          // between 894 and 1023 are brake command
    Throttle = 0;
    Temp = (float(potValue) - 894.0) * 255.0 / (1023.0 - 894.0);
    if (Temp > 255.0) {
      Temp = 255.0;
    }
    if (Temp < 0.0) {
      Temp = 0.0;
    }
    Brake = int(Temp);  // scale brake command from 0 (none) to 255 (full)
  }
  if ((potValue < 894) && (potValue > 846)) {
    Throttle = 0;
    Brake = 0;
  }
  if ((potValue <= 846) && (potValue >= 467)) {
    Temp = (846.0 - potValue) * 255.0 / (846.0 - 467.0);
    if (Temp > 255.0) {
      Temp = 255.0;
    }
    if (Temp < 0.0) {
      Temp = 0.0;
    }
    Throttle = int(Temp);
    Brake = 0;
  }
  if (potValue < 467) {
    Throttle = 255;
    Brake = 0;
  }
 WriteBuffer[0] = Throttle;
 WriteBuffer[1] = Brake;
 Serial.write(WriteBuffer, 2);

  delayMicroseconds(500);
}

Here's the relevant Windows code on the PC:

Code:
int DriverControlUnit()
{
      HANDLE hSerial;                                                            // set up COM7 as serial port for writing to Arduino

      hSerial = CreateFile(_T("COM7"),
                                    GENERIC_READ | GENERIC_WRITE,
                                    0,
                                    0,
                                    OPEN_EXISTING,
                                    FILE_ATTRIBUTE_NORMAL,
                                    0);

      if(hSerial==INVALID_HANDLE_VALUE){
            if(GetLastError()==ERROR_FILE_NOT_FOUND){
                  cout<<"serial port does not exist.\n";
            }
      cout<<"some other error occurred.\n";
      }
      DCB dcbSerialParams = {0};

      dcbSerialParams.DCBlength = sizeof(dcbSerialParams);

      if (!GetCommState(hSerial, &dcbSerialParams)) {
            cout<<"error getting state.\n";
      }

      dcbSerialParams.BaudRate = CBR_19200;
      dcbSerialParams.ByteSize = 8;
      dcbSerialParams.StopBits = ONESTOPBIT;
      dcbSerialParams.Parity = NOPARITY;

      if(!SetCommState(hSerial, &dcbSerialParams)){
            cout<<"error setting serial port state.\n";
      }
      COMMTIMEOUTS timeouts={0};
      timeouts.ReadIntervalTimeout=50;
      timeouts.ReadTotalTimeoutConstant=50;
      timeouts.ReadTotalTimeoutMultiplier=10;
      timeouts.WriteTotalTimeoutConstant=50;
      timeouts.WriteTotalTimeoutMultiplier=10;

      if(!SetCommTimeouts(hSerial, &timeouts)){
            cout<<"error setting timeouts.\n";
      }
      DWORD queueNum = 2;
      SetupComm(hSerial, queueNum, queueNum);

      unsigned char ReadBuff[3];
      DWORD BytesRead;
      int CommandCount = 0;

      unsigned char Stee, Throt, Brak;

      SetCommBreak(hSerial);
      PurgeComm(hSerial, PURGE_RXCLEAR);
      ClearCommBreak(hSerial);
      ReadFile(hSerial, &ReadBuff, 2, &BytesRead, NULL);

      Throt = ReadBuff[0];
      Brak = ReadBuff[1];
      CommandString[CommandCount++] = 'T';
      CommandString[CommandCount++] = Throt;
      CommandString[CommandCount++] = 'B';
      CommandString[CommandCount++] = Brak;
      CommandString[CommandCount++] = 'S';
      CommandString[CommandCount++] = 128;
      CommandString[CommandCount++] = NULL;
      CommandString[CommandCount++] = NULL;
      CloseHandle(hSerial);
      return 0;
}
236  Forum 2005-2010 (read only) / Interfacing / Send two-byte data from Arduino to PC via USB on: December 29, 2010, 01:10:35 am
I want to use a potentiometer to send a throttle and brake signal to my PC using the USB serial data port. One byte (0 to 255) is throttle and one byte (0 to 255) is brake.

On the Arduino end, I send the two bytes using Serial.write(WriteBuffer, 2). On the PC end I use ReadFile(hSerial, &ReadBuff, 2, &BytesRead, NULL) to get the data. I take the two bytes read by ReadFile and process them.

I had a few problems getting the communications to work. But now they do work well, except for one problem. About half the time, the order of the two bytes is reversed.

It's no problem is data is occasionally lost. But this reversal of bytes is a real problem. I cannot figure out how it is happening.

If it will help, I can post either or both of the C++ (PC end) or Arduino code.

If there is any better way to do this, I'm all ears. I've looked over the forum, but found no solution to sending two bytes like this.

Any ideas greatly appreciated.
237  Forum 2005-2010 (read only) / Troubleshooting / Laptop keeps sending through the COM port on: December 02, 2009, 09:55:46 pm
I've just started with the Arduino. The examples I have tried all worked well if I uploaded the code and then turned the laptop off. (Leaving the USB cable connected so the Arduino still gets power.)

But the code does not work right if I leave the laptop on and keep the Arduino connected to it through the USB port. The laptop keeps sending to the Arduino every now and then. That disrupts the code and the example does not work right.

What can I do to my laptop so that it does not send anything on the COM port unless I upload code?
238  Forum 2005-2010 (read only) / Interfacing / Re: Non-linear resistance in a digital potentiometer on: January 08, 2011, 09:42:50 pm
Quote
If their speed input signal pin indeed needs up to 50ma of driving impedenace then a digital pot is not going to work either, as if you look at digital pot datasheets you most likey will find that they can handle near that kind of load.
Looks like I'll need to lean on Alltrax for more information about their controller. Then I won't be shooting in the dark with circuits that might not handle the current.

Thanks for the help on this, Lefty and others.
239  Forum 2005-2010 (read only) / Interfacing / Re: Non-linear resistance in a digital potentiometer on: January 07, 2011, 09:16:36 pm
Quote
Daanii   can you please supply a sketch of your hook-up so that we can better understand your application.
Here is a webpage that shows the wiring diagram for the motor controller I want to control electronically:

http://www.alltraxinc.com/files/Doc100-047-A_DWG-AXE-PermMag-wire-dia.pdf

The throttle shown in the upper left corner is the one I want to create the circuit for. Now people use a mechanical potentiometer -- either an accelerator pedal or a twist-grip throttle. I want to use a digital potentiometer circuit instead.

An alternative wiring diagram shows the voltage (instead of resistance) speed input signal retrolefty suggests using:

http://www.alltraxinc.com/files/Doc100-049-A_DWG-AXE-Taylor-Dunn-wire-dia.pdf

(NOTE: This wiring diagram shows the +6.0V to +10.5V speed signal option. I will probably use the 0V to +5V option instead.)
240  Forum 2005-2010 (read only) / Interfacing / Re: Non-linear resistance in a digital potentiometer on: January 07, 2011, 08:57:37 pm
Quote
Daanii   can you please supply a sketch of your hook-up so that we can better understand your application.
Yes, I'll do that.

Quote
It would seem to me the easiest, simplest, and possibly most reliable one to use with an Arduino is the 0-5vdc option.
You are right. That simple approach may indeed work.

But the people at Alltrax have given me conflicting information about the "black box" circuit I will be hooking into. At one point, they told me that if I will be using the 0 to +5V option, I need to source at least 50 mA, which is more than the Arduino PWM pins put out.

So I am preparing two circuits. One circuit will use the Arduino PWM output pin, with a low-pass filter as you so rightly suggest. (I had not thought about the op amp voltage follower, but that may indeed be needed.) The other circuit will use a digital potentiometer (or rheostat).

Not knowing which circuit will work best for this project, having both available will be best. I've got some time before the motors and controllers will be set up and ready to hook my circuit up to.
Pages: 1 ... 14 15 [16] 17