Show Posts
Pages: [1] 2 3 4
1  Using Arduino / Motors, Mechanics, and Power / Re: Difference between motor controllers on: June 03, 2014, 10:54:55 am
I'm thinking about using optocouplers to prevent my Arduino board from getting harmed by an excessive voltage. I have not started yet but thinking about it.

I guess you have to provide a seperate voltage to the receiving end of the optocoupler(s) which is a transistor activated by light hitting the base. Now your controller, ok driver, can't damage your board.

You don't even have to use common ground so you can really seperate the circuits.

It's an idea, if you like to use them please study them yourself prior to using them.
2  Using Arduino / Motors, Mechanics, and Power / Re: (still) unable to read an encoder on: June 02, 2014, 10:30:15 pm


This is how the setup is made.
3  Using Arduino / Motors, Mechanics, and Power / Re: (still) unable to read an encoder on: June 02, 2014, 12:53:56 pm
Ok, thanks.

I have changed the code and the code doesn't stall anymore.
I also receive different numbers now instead of only zero's.

Code:
#define encoder0PinA 2

volatile int val = 0;
volatile int PinB = 3;
volatile int valA;
volatile int valB;
int valold;

void setup()
{
  pinMode(encoder0PinA, INPUT);
  pinMode(PinB, INPUT);
  digitalWrite(encoder0PinA, HIGH);
  digitalWrite(PinB, HIGH);
  attachInterrupt(0, doEncoderA, CHANGE);  // encoder pin on interrupt 0 (pin 2)
  Serial.begin (9600);
  Serial.print("Encoderpositie = ");
  Serial.println(val);
  valold = val;
}

void loop()
{
if (valold != val)
  {
    Serial.print("Encoderpositie = ");            
    Serial.println(val);
    valold = val;  
  }
}

void doEncoderA()
{
valA = digitalRead(encoder0PinA);
valB = digitalRead(PinB);
  if ((valA == HIGH) && (valB == LOW))
    {
      val = val + 1;
    }
  else
    {
      if ((valA == HIGH) && (valB == HIGH))
        {
          val = val - 1;
        }
      else
        {
          if ((valA == LOW) && (valB == HIGH))
            {
              val = val + 1;
            }
          else
            {
              if ((valA == LOW) && (valB == LOW))
              {
              val = val -1;
              }
            }}}  
}

But the numbers are erratic. The trends seem to be ok (overall rising or falling numbers) but not every step gives the same difference. The differences can be really high (higher then some missed values by the serial monitor). Sometimes the trend can even shortly reverse while rotating in one direction???

Why is it so hard to read an encoder?

EDIT:
I have added two 0,1 uF capacitors between the A and B and ground and it seems to be a bit better now. (maybe it's only better in my mind but I have a feeling it really is a bit better now) Maybe the erratic behaviour is caused by bouncing?
4  Using Arduino / Motors, Mechanics, and Power / (still) unable to read an encoder on: June 02, 2014, 12:12:43 pm
Hi,

After thinking about a good code I "came up" with the following code:

Code:
#define encoder0PinA 2

volatile int val = 0;
volatile int PinB = 3;

void setup()
{
  pinMode(encoder0PinA, INPUT);
  pinMode(PinB, INPUT);
  attachInterrupt(0, doEncoderA, CHANGE);  // encoder pin on interrupt 0 (pin 2)
  Serial.begin (9600);
  Serial.print("Encoderpositie = ");
  Serial.println(val);
}

void loop()
{
//Do stuff here
}

void doEncoderA()
{
  if ((encoder0PinA == HIGH) && (PinB == LOW))
    {
      val = val + 1;
    }
  else
    {
      if ((encoder0PinA == HIGH) && (PinB == HIGH))
        {
          val = val - 1;
        }
      else
        {
          if ((encoder0PinA == LOW) && (PinB == HIGH))
            {
              val = val + 1;
            }
          else
            {
              if ((encoder0PinA == LOW) && (PinB == LOW))
              {
              val = val -1;
              }
            }}}
Serial.print("Encoderpositie = ");            
Serial.println(val);          
}
 

I now think this isn't such a good code after all.

This is what happens:



After rotating for a short while the uC crashes and stalls.

This is the setup I use. I'm using a stock rotary encoder. The resistors are pull down resistors.



2 problems so far
1) the code stalls
2) as long/short as the code is working no adding or subtraction

In my mind the code should be ok?

What am I missing?

Leo
5  Using Arduino / Motors, Mechanics, and Power / Re: trouble with controlling a single servo on: June 01, 2014, 06:13:43 am
Hi Mark,

I have a seperate power supply (5V 1A DC) for the servo with common ground with/to the arduino which is fed by the USB cable of the laptop.



Leo
6  Using Arduino / Motors, Mechanics, and Power / Re: trouble with controlling a single servo on: June 01, 2014, 04:57:50 am
After uploading this version of the sketch it now works fine?????

Code:
#include <Servo.h>

Servo myservo;     // create servo object to control pwm signal

int potPin = 2;    // select the input pin for the potentiometer
int val;           // variable to store the value coming from the potentiometer
int servopos;      // variable to set the value of the pwm signal

void setup()
{
  myservo.attach(7);   // attaches the servo on pin 7 to the servo object
  Serial.begin (9600);
}

void loop ()
{
  val = analogRead(potPin);               // read the value from the potentiometer
  servopos = map (val, 0, 1023, 10, 169); // scale the value from the potentiometer to pwm value to be written to digital pin 7
  myservo.write(servopos);                // set servo position to scaled value
  Serial.print("servopos = ");
  Serial.println(servopos);
  Serial.print("val = ");
  Serial.println(val);
  delay(15);
}

It's a complete mistery to me but at least it's working now.

Thanks,

Leo
7  Using Arduino / Motors, Mechanics, and Power / trouble with controlling a single servo [solved] on: June 01, 2014, 03:43:43 am
Hi,

After an absence of about 2 years I have started again with Arduino.
I'm repeating my previous experiments quickly to get familiar with the coding again.

I have trouble controlling a RC servo ending up in 1 dead servo until now.

I'm really puzzled because this code worked fine 2 years ago (since then I have thrown away the joystick but the code should still be fine)

Code:
// Controlling servo positions using a hacked joystick (Logitech WingmanAttackII)
// by Leo Groeneveld

#include <Servo.h>
 
Servo myservo2;   // create servo object to control servo2 (pitch servo in my case)
Servo myservo3;   // create servo object to control servo3 (roll servo in my case)

int potpin2 = 2;  // analog pin used to connect the potentiometer2
int val2;         // variable to read the value2 from the analog pin2
int potpin3 = 3;  // analog pin used to connect the potentiometer3
int val3;         // variable to read the value3 from the analog pin3
int servopos2;    // variable to set the postition of servo2
int servopos3;    // variable to set the postition of servo3

void setup()
{
  myservo2.attach(2);  // attaches the servo on pin 2 to the servo object
  myservo3.attach(3);  // attaches the servo on pin 3 to the servo object
  Serial.begin (9600); // start serial connexion to monitor
}
 
void loop()
{
  val2 = analogRead(potpin2);                  // reads the value of potentiometer2 (value between 0 and 1023)
  val3 = analogRead(potpin3);                  // reads the value of potentiometer3 (value between 0 and 1023)

  if (val2 > 510 and val2 < 610) {             // dead-band
  servopos2 = 89;                              // when in dead-band servo2 postition is neutral
  myservo2.write(servopos2);                   // sets servo2 position to neutral
  }
  else {
    if (val2 > 240 and val2 <= 510) {
      servopos2 = map(val2, 240, 510, 15, 89); // scale it to use it with servo2 (value between 15 and 89)
      myservo2.write(servopos2);               // sets servo2 position according to the scaled value
    }
    else {
      if (val2 <=240){                         // when joystick potentiometer2 gets "overturned"
        servopos2 = 15;                        // postition of servo2 must be 15
        myservo2.write(servopos2);             // keeps servo2 in maximum position
      }
      else {
        if (val2 >= 610 and val2 < 880) {
          servopos2 = map(val2, 610, 880, 89, 164);    // scale it to use it with servo2 (value between 89 and 164)
          myservo2.write(servopos2);                   // sets servo2 position according to the scaled value
        }
        else {
          if (val2 >= 880) {                           // when joystick potentiometer gets "overturned"
            servopos2 = 164;                           // position of servo2 must be 164
            myservo2.write(servopos2);                 // keeps servo2 in maximum position
          }}}}}
          
// seperator between servos (DIRECTION OF SERVO3 IS REVERSED!!!)

if (val3 > 460 and val3 < 560) {               // dead-band
  servopos3 = 89;                              // when in dead-band servo3 postition is neutral
  myservo3.write(servopos3);                   // sets servo3 position to neutral
  }
  else {
    if (val3 > 190 and val3 <= 460) {
      servopos3 = map(val3, 191, 460, 164, 89);  // scale it to use it with servo3 (value between 164 and 89)
      myservo3.write(servopos3);                 // sets servo3 position according to the scaled value
    }
    else {
      if (val3 <=190){                           // when joystick potentiometer3 gets "overturned"
        servopos3 = 164;                         // postition of servo3 must be 164
        myservo3.write(servopos3);               // keeps servo3 in maximum position
      }
      else {
        if (val3 >= 560 and val3 < 830) {
          servopos3 = map(val3, 560, 829, 89, 15);     // scale it to use it with servo3 (value between 89 and 15)
          myservo3.write(servopos3);                   // sets servo3 position according to the scaled value
        }
        else {
          if (val3 >= 830) {                           // when joystick potentiometer gets "overturned"
            servopos3 = 15;                            // position of servo3 must be 15
            myservo3.write(servopos3);                 // keeps servo3 in maximum position
          }}}}}          
          
  delay(15);                                           // waits for the servos to get in the desired positions
   Serial.print("potstand2 = ");
   Serial.print(val2);
   Serial.print(" servo2 stand = ");
   Serial.println(servopos2);
   //   Serial.print("potstand3 = ");
     // Serial.print(val3);
   //   Serial.print(" servo3 stand = ");
     // Serial.println(servopos3);
}

Now I have written this code to control a single servo using a potentiometer.

Code:
#include <Servo.h>

Servo myservo;     // create servo object to control pwm signal

int potPin = 2;    // select the input pin for the potentiometer
int val;           // variable to store the value coming from the potmeter
int servopos;      // variable to set the value of the pwm signal

void setup()
{
  myservo.attach(7);   // attaches the servo on pin 7 to the servo object
  Serial.begin (9600);
}

void loop ()
{
  val = analogRead(potPin);               // read the value from the potmeter
  servopos = map (val, 0, 1023, 10, 169); // scale the value from the potmeter to fade value to be written to digital pin 9
  myservo.write(servopos);                // set servo position to scaled value
  Serial.print("servopos = ");
  Serial.println(servopos);
  Serial.print("val = ");
  Serial.println(val);
  delay(1500);
}

All it does is putting the servo in one max angle and overloading the thing.
When I look at the values using the serial monitor everything seems to be fine.

The setup is:
HP 8710p laptop running windows 8.1 (is this the problem?, my previous OS was windows XP)
Arduino MEGA 1280 clone, used it the previous time

What am I doing wrong?

Thanks for your help.

Leo
8  Using Arduino / Microcontrollers / analogRead function in ATtiny chip on: January 12, 2012, 01:24:03 pm
Hi,

When using this code in my Arduino ATmega it works fine and the moment of engagement can be trimmed with a potmeter.

Code:
/*
code to use 2 leds as brake lights on RC car
If the esc/throttle servo has to be trimmed
the pot meter can be used to set the moment
of engagement to the desired value
by Leo Groeneveld, dec 2011
*/

const int ledPin = 0;

int ledState = LOW;
int x;                          // variable to store trigger value
int y;                          // variable to store pulse width

void setup()
{
pinMode(1, INPUT);              // Pin 1 (3) set as input to read the PPM signal from RC receiver for the UNO use the number between parentheses
pinMode(2, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledState); // turn/keep leds off at start
}

void loop()
{
int sensorValue = analogRead(2);      // Read value from resistor
x = map (sensorValue, 0, 1023, 1000, 1500);
y = pulseIn(1, HIGH);

if(y < x && ledState == LOW){
  ledState = HIGH;
  digitalWrite(ledPin, ledState);
}
  else
    if(y > x && ledState == HIGH){
      ledState = LOW;
      digitalWrite(ledPin, ledState);
    }
}

When using the same code in an ATtiny45 the potmeter does nothing.
The ATtiny should be able to do an analogRead on pin 2 but it doesn't.

Should the code be changed for an ATtiny?

EDIT: solved by using pin 3, but pin 2 should be able to work also according to this image:


9  Using Arduino / Microcontrollers / Re: blinking brake lights for RC cars not working in ATtiny45 on: January 12, 2012, 01:07:57 pm
Hi,

It should be 8MHz because I "burned" the bootloader.

10  Using Arduino / Microcontrollers / blinking brake lights for RC cars not working in ATtiny45 on: January 11, 2012, 05:54:26 pm
Hi,

I have made a code for blinking brake lights.

The code works fine with my ATmega1280 as can be seen here:


When I upload the code in an ATtiny45 it doesn't work. When the transmitter is off the led slowly blinks and when I turn the transmitter on the led just fades and does nothing.
When the transmitter is turned on again the led starts blinking slowly again.

Here's the code:
Code:

const int ledPin = 0;                      // number of the LED pin

int ledState = LOW;
int t;
int x;
int y;
int z;

long previousMillis = 0;                     // will store last time LED was updated

                                             // the follow variables is a long because the time, measured in miliseconds,
                                             // will quickly become a bigger number than can be stored in an int.
long interval = 100;                         // interval at which to blink (milliseconds)

void setup() {
  pinMode(ledPin, OUTPUT);                   // set the digital pin as output:
  t = 0;
  z = 1400;
}

void loop()
{
  y = pulseIn(1, HIGH);                      // read PPM signal on ch.2 from receiver
    if(y > z){
      x = 1;
    }
    else
    if(y <= z){
      x = 0;
    }

unsigned long currentMillis = millis();
 
 if(x == 0 && t == 0){
   previousMillis = currentMillis;
   ledState = HIGH;
   digitalWrite(ledPin, ledState);
   t = 1;
 }
 
if(x == 0 && t == 1 && currentMillis - previousMillis > interval && ledState == HIGH){
  previousMillis = currentMillis;
  ledState = LOW;
  digitalWrite(ledPin, ledState);
}

if(x == 0 && t == 1 && currentMillis - previousMillis > interval && ledState == LOW){
  previousMillis = currentMillis;
  ledState = HIGH;
  digitalWrite(ledPin, ledState);
}

if(x == 1 && t == 1){
  t = 0;
  if(ledState == HIGH){
    ledState = LOW;
    digitalWrite(ledPin, ledState);
  }
}
}

What am I doing wrong?

Leo
11  Using Arduino / Microcontrollers / Re: problem programming Attiny85 with UNO on: December 26, 2011, 04:16:54 am
I've made this configuration with a couple of resistors to get exactly 120 ohms.


Now I get this error message after the Rx led on the Uno flashes three times:


This is the orientation of the ATTiny in the bread board wrt it's connections.


(I previously bent the legs of the cap for a better fit in the bread board but no results there)

(I'm going to clear everything today to put an extra lamp on the ceiling tomorrow)

EDIT:

It's working now!!!

I made the rookie mistake of not connecting the ground of the chip after I used the ground lead for the resistors and I left the cap and resistors out and now it works.
So the auto reset is not disabled. I also connected a led to pin 9 to see the heartbeat.

Thanks guys for the support.

Here a short vid of the result.
Brake lights for my Team Associated SC10 short course race truck.
(you cannot see it clear on the vid but the leds burn when the brake is engaged, with the potmeter I can tune the moment of engagement to follow trim of the ESC if necessary)



12  Using Arduino / Microcontrollers / Re: problem programming Attiny85 with UNO on: December 26, 2011, 03:27:20 am
I have to check pin 9 with a led.

I used a 10 uF from an old VCR so it might be faulty or the legs are too short to use in the bread board. New ones are on their way.

I will put a led on pin 9 and try a resistor.

(I bought a clone Uno so I don't know what bootloader is on the Uno itself)

I'm going to try again, I'm new at this so my stock of resistors is limited but I will try.
13  Using Arduino / Microcontrollers / Re: problem programming Attiny85 with UNO on: December 26, 2011, 12:04:23 am
That's great!

Over here no succes yet.

Instead of that I get this:
After Burning Bootloader to ATtiny45

This should be ok.
And then it happened:
After trying to upload the sketch to the ATtiny


What went wrong?
14  Using Arduino / Microcontrollers / Re: When will the new code be available to use an UNO as an ISP? on: December 22, 2011, 11:23:39 pm
could you have a look to this thread?
http://arduino.cc/forum/index.php/topic,84095.0.html
Yikes!  Sounds like the ISP gods are very unhappy with you  smiley-roll-blue

I'm afraid that I cannot offer any advice.  I can only say again that my Uno board no longer requires that autoreset be disabled when programming with ArduinoISP.  Before I flashed the chip with the new version of optiboot, my Uno board would not work without physically disabling autoreset.  I have only used ArduinoISP to program ATmega328P chips, though.

Jim

Hi Jim,

Where can we find the latest version of optiboot and how to flash it to the Uno board?

(I'm going to try with my board as is because I have just bought it so it might be new but just in case it's an old one)

15  Using Arduino / Microcontrollers / Re: When will the new code be available to use an UNO as an ISP? on: December 21, 2011, 02:53:38 pm
Ok, that's good to read.

Thanks!
Pages: [1] 2 3 4