Show Posts
Pages: 1 [2] 3 4 ... 295
16  Using Arduino / Programming Questions / Re: Reading serial data into an array and then parsing it. on: September 29, 2014, 02:53:25 am
Not part of your problem, or a solution to it, but do you really need to do this
  int voltIn = analogRead(10);   // READ ADC
  float outputVoltIn = map(voltIn,0,1023,0,5);  // Temporary MAPING 0 - 5
  float currentIn=1.50;  // Temp hold for second data to be sent via serial
  float outputCurrentIn=currentIn;  //  Temp variable to match final variable name
every time through loop() on the transmitter ?  Why not collect the data only when you need it and while you are at it use a more controlled method to determine the frequency of the readings ?

  float outputCurrentIn=currentIn;  //  Temp variable to match final variable name
The receiver will not know the name of the variable on the transmitter so why the need to copy the variable to another name ?

float outputVoltIn = map(voltIn,0,1023,0,5);  // Temporary MAPING 0 - 5
The map() function uses integer maths so will not generate a float.
17  Using Arduino / Programming Questions / Re: Servo Problems on: September 29, 2014, 02:23:23 am
Oh dear, its a mess, but it can be sorted out

while(1 == 1)
Why ?  Is the loop() function not good enough at repeating code ?
Sends the servo fully one way every time through the loop not just when the program starts
    if(switchState == LOW)
This is redundant. Test for the condition you are looking for and act on it.
    else if(switchState == HIGH)
Even assuming you needed the else, which you don't, what other value could switchState have anyway ?
You don't say how the switch is wired but from the logic in your program activating takes the input pin HIGH.  Have you got a pulldown resistor holding LOW when the switch is not activated so that you know its state at all times ?  A better alternative is to use the internal pullup resistor and
  pinMode(pinNumber, INPUT_PULLUP);
The logic for reading the pin state and the wiring will need to be revised but it is a much tidier solution.

    else if(switchState == HIGH)
      Serial.println("Button Pressed");
      //^changed from 179 just to try to make it more stable
Why the delay() ?  The servo will not turn 160 degrees in 50 milliseconds and even if it did you want it to stay there while the switch is turned on.  As it is, after the delay() the code loops back to
and off the servo goes back to 0.

Instead of an if you need to use a while so the servo stays at 160 while the button is pressed, but you must test the switch state inside the while loop so that it can end when the switch is opened.

Now for the big question, but I fear that I already know the answer.  How is the servo powered ?  The Arduino cannot provide enough power for any reasonable servo so you need to use an external power supply and a common GND
18  Using Arduino / Programming Questions / Re: Adjusting the DC motors speed on: September 27, 2014, 05:17:18 am
Could I solve this problem by using concept in the late post ?
The whole point of measuring the wheel rotations, comparing them and adjusting the speed of one or both wheels then doing the measurement/adjustment over and over again is to keep the robot running as straight as possible. 

My first program uses a crude method to adjust the speed of one motor because the adjustment factor is fixed.  My second program uses a PID which does the correction by applying a correction that is proportional to the difference between the 2 encoder readings.  That way a bigger difference applies a bigger correction and when tuned correctly the difference reduces over time and smaller and smaller corrections are applied.  The PID parameters can be adjusted to make the correction more or less aggressive.

Now, when it comes to turning the robot you have a different set of problems because the 2 motors will deliberately have a different PWM value applied to them so there is bound to be a difference in encoders readings and you do not want the program to adjust the PWM value(s) to make the robot run straight.  So, what can you do ?  Well, one approach is to make one motor run at a fixed speed and the other to run at a speed that is a known speed faster, say twice as fast.  Then when you read the encoders you would expect one value to be twice the other and you can apply a correction based on the ratio of the 2 wheel speeds by dividing the reading of the faster wheel in the turn by the ratio of the 2 wheel speeds.  If the robot is working perfectly the difference will be zero, if not then use the difference to calculate the adjustment to be applied, preferably using a PID.

My second program will only work to keep the robot running straight but you can apply the principle outlined above, perhaps by having a variable to hold the current ratio of wheel speeds, normally set to 1 and multiplying the difference in encoder values by that value.  When you turn set the variable to the ratio of wheel speeds (PWM values).

Note that neither method can take account of slippage of one or both wheels on the surface they are running on.  The problem is likely to be greater when turning as the wheels will naturally need to skid sideways a little which will be different at different wheel speeds unless you spin on the spot with both wheels at the same speed in which case the skidding should cancel out.
19  Using Arduino / Programming Questions / Re: Wii camera project servos not moving correctly on: September 27, 2014, 01:40:50 am
what about UKheliBob's comment about the type of servo?  They are 9g(SG90) micro servos
The online details that I can find make it almost certain that they are normal, real servos and as such can be commanded to go to a particular angle and stay there.

My query was because of the names and use of the #defined constants in the program.  For instance

#define S1_LEFT_SLOW 1550
Effectively this means that wherever S1_LEFT_SLOW appears in the program it will be replaced with 1550

which in turn means that when
void move_servo1(int val)
is executed it becomes

Normally servo.write() takes a value between 0 and 180 to set the angle.  It will accept values greater than that and will treat them as microseconds as if the writeMicroseconds() method had been used, but it is unusual to see it used that way instead of just using writeMicroseconds() in the first place.

So, the code will work to move the servo but the #defined names are still strange as the code does not change the speed of the servos, only their position, hence my query about the type of servos.  So called continuous rotation servos cannot be moved to a defined angle rather the parameter passed to servo.write() determines the speed and direction of rotation which matches the #defined names better.

Using a normal servo and fixed values for servo.write() as the program does means that the servos could only move to fixed positions.  Is that what is supposed to happen ?
20  Using Arduino / Programming Questions / Re: Converting serial data into an Array on: September 26, 2014, 03:46:59 pm
The inputArry array is declared in setup(), as are other variables, but you are trying to use is in loop(), hence the error message.

By the way, try Auto Formatting your program and another problem will become apparent.
21  Using Arduino / Programming Questions / Re: Time and TimeAlarms Libraries – Ask here for help or suggestions on: September 26, 2014, 01:39:54 pm
I will check how many bytes the code increases by after compiling with the addition of each new alarm. I just thought someone (like the creator of the library) might already know this.
From the TimeAlarms readme
Q: How many alarms can be created?
A: Up to six alarms can be scheduled. 
The number of alarms can be changed in the TimeAlarms header file (set by the constant dtNBR_ALARMS,
note that the RAM used equals dtNBR_ALARMS  * 11)
22  Using Arduino / LEDs and Multiplexing / Re: ARDUINO UNO & LOL SHIELD (LED MATRIX DISPLAY) ERROS MESSAGES on: September 26, 2014, 10:11:08 am
Calm down and look at the code you posted.  Much of it is displayed in italics in your post but it is not like that in the IDE.  If you had read which you could hardly miss as it is at the top of the forum page in bold red you would have seen the advice to use code tags when posting code here to avoid it being munged like yours.

As to the use of capitals.  Please don't do it.  It is just plain rude.

Because your code is munged it is difficult to read an understand but as has been suggested faulty installation of the fix_fft library.  I get the same error as you when compiling the code from the site you linked to without the library installed.  Where do you have it installed exactly ?

23  Using Arduino / Programming Questions / Re: Using the 2x16 LCD to control things on: September 26, 2014, 09:30:02 am
You do know another way to do what you want, which is to detect when a pushbutton becomes pressed.  You have done it earlier in your program.
24  Using Arduino / Programming Questions / Re: Sketch won't "Serial.print" on: September 26, 2014, 04:35:10 am
Stand by to kick yourself.

I can't see a Serial.begin() in your program
25  Using Arduino / Project Guidance / Re: How to use a dc motor to control blinds at different light intensity? on: September 26, 2014, 02:55:12 am
I'd think a servo or stepper would be better, probably difficult to position a dc motor accurately.
I agree wholeheartedly but the OP asked about using a dc motor.
26  Using Arduino / Project Guidance / Re: How to use a dc motor to control blinds at different light intensity? on: September 26, 2014, 02:34:51 am
An analogue input reading a light dependant resistor the value of which controls a motor shield to move the blinds.

What have you tried ?
27  Using Arduino / Programming Questions / Re: Wii camera project servos not moving correctly on: September 26, 2014, 01:49:22 am
The names used in the #defines would seem to indicate that they relate to the speed at which the servos move rather than the position they move to.  This in turn implies that continuous rotation "servos" are being used rather than normal ones.

What type of servos are you using and how are they powered ?
28  Using Arduino / Programming Questions / Re: Need help, prime number generator has quirks - school project on: September 25, 2014, 01:01:27 pm
  while(1);//we're done so halt
Or just move the whole program into setup() and leave loop() empty
29  Using Arduino / Programming Questions / Re: Adjusting the DC motors speed on: September 25, 2014, 12:20:39 pm
1- first I notice that as I increases the  "trackInterval" , the difference (error) increases .
No surprise there.  If the difference between the counts is ay 10 in a period of 200 milliseconds than what would you expect it to be over a period of say 400 milliseconds ?

this problem could be solved by many ways one of them  the code above that adjust the speed of two motors and make robot run in straight line .
Who cares what PWM values make the robot run straight as long as it does.  You can improve the simple straight running program that I suggested by slowing the fast motor and speeding up the slow motor at the end of every measurement period and by making the measurement period shorter so that the adjustment happens more often. 

The best solution is to use a PID which takes an error (the difference between the two counts) and applies a correction (change of motor speed) proportional to the size of the difference.  This makes the adjustment smoother and you can adjust the frequency of the adjustment and also how aggressively the correction is applied.

Here is a program that uses a PID derived from my previous one .  As before it uses a library to control the motor and will need to be adapted to suit your hardware.

#include <PID_v1.h>
#include <HUBeeBMDWheel.h>

HUBeeBMDWheel leftWheel;
HUBeeBMDWheel rightWheel;

//PID variables.  All doubles
double trackAdjustValue;
double trackSetpoint;
double trackError;

double Kp = 3;  //Determines how aggressively the PID reacts to the current amount of error (Proportional)
double Ki = 2;  //Determines how aggressively the PID reacts to error over time (Integral)
double Kd = 1;  //Determines how aggressively the PID reacts to the change in error (Derivative)

PID trackPID(&trackError, &trackAdjustValue, &trackSetpoint, Kp, Ki, Kd, DIRECT);

const byte rightFasterLED = A0;
const byte leftFasterLED = A4;
const byte equalSpeedLED = A2;

const byte forward = 1;
const byte reverse = 0;

const byte hard = 1;    //type of braking
const byte soft = 0;

double leftSpeed = 100;      //intital speeds.  Using doubles for use with PID
double rightSpeed = 100;

volatile double leftCount =  0;
volatile double rightCount =  0;

void setup()

  attachInterrupt(1, updateLeftCount, CHANGE);
  attachInterrupt(0, updateRightCount, CHANGE);

  pinMode(rightFasterLED, OUTPUT);
  pinMode(leftFasterLED, OUTPUT);
  pinMode(equalSpeedLED, OUTPUT);

  leftWheel.setupPins(13, 12, 10); 
  rightWheel.setupPins(8, 11, 9);



  trackAdjustValue = 0;
  trackSetpoint = 0;
  trackError = 0;


void loop()
  leftWheel.setMotorPower(leftSpeed);      //set motor speeds to current values

  trackError = rightCount - leftCount;

  if (trackPID.Compute()) //true if PID has triggered
    rightSpeed += trackAdjustValue;
    leftCount = 0;
    rightCount = 0;
    if (trackError > 0)
      digitalWrite(rightFasterLED, HIGH);
      digitalWrite(leftFasterLED, LOW);
      digitalWrite(equalSpeedLED, LOW);
    else if (trackError < 0)
      digitalWrite(rightFasterLED, LOW);
      digitalWrite(leftFasterLED, HIGH);
      digitalWrite(equalSpeedLED, LOW);
      digitalWrite(rightFasterLED, LOW);
      digitalWrite(leftFasterLED, LOW);
      digitalWrite(equalSpeedLED, HIGH);


void updateLeftCount()
  leftCount = leftCount + 1;  //++ not valid for doubles

void updateRightCount()
  rightCount = rightCount + 1;  //++ not valid for doubles
30  Using Arduino / Programming Questions / Re: Need help, prime number generator has quirks - school project on: September 25, 2014, 09:57:59 am
You could make the program run faster by only testing the odd numbers to see if they are prime as by definition even ones (except 2) are not.
for ( x = primeMin+1; x <= primeMax;  x += 2)
Pages: 1 [2] 3 4 ... 295