DIY Electric Car Forums banner
1 - 9 of 9 Posts

· Registered
Joined
·
230 Posts
Discussion Starter · #1 ·
So I have an arduino micro controller and I was trying out some new code on it b/c my old code is so crude. Each time I programmed the chip and plugged it into my board replacing the old chip the motor took off by itself a few seconds after I turned the key the truck was in neutral of coarse :eek:. I tried a lot of different things to prevent this b/c thought my board may be receiving interference but none of them worked until I tried a different potentiometer one with shorter leads on it and it worked perfectly. The old pot had about four feet of wire so I thought shortening it and twisting the three wires together would work but it did not the motor just took off again but by then I just turned the key on and off really quickly to charge the caps and then all I got was the motor nudging a little using up all the energy in the caps instead of letting the motor go full bore.
My question is could I put some shielding around the pot wire to limit interference?

Here is the old code. There is a calibration number of -28 on the pot the new code does not have this though maybe that could have something to do with it?
Code:
    int potPin = 2;    // select the input pin for the potentiometer
    int ledPin = 11;   // note onlly 5,6, 9,10,11 are PWM
    int wait = 0;       // variable to store the value coming from the sensor


    void setup() {
   //   Serial.begin(9600);
      pinMode(ledPin, OUTPUT);  // declare the ledPin as an OUTPUT
    }

    void loop() {
      wait = analogRead(potPin) - 28; // read the value from the sensor
      if(wait > 950){ 
        digitalWrite(ledPin, LOW);
      }else{
         digitalWrite(ledPin, HIGH); // turn the ledPin on
         delayMicroseconds(1023 - wait); // stop the program for some time
         digitalWrite(ledPin, LOW); // turn the ledPin off
         delayMicroseconds( wait); // stop the program for some time
      }
   //   Serial.println( wait );
    }
Here is the new code.
Code:
/* 
Timer/Counter2 PWM mode = 3
Counting sequence : WGM21& WGM20 in TCCR2 

Compare Output mode , Fast PWM mode.
COM21 = 1; COM20 = 0 ( non-inverting mode ) 

TCNT2 inc or dec 
OCR2 compare with Timer/Counter
when TCNT2 == OCR2 
PWM output == OC2 ( PD2 ???), will go low until counter reach MAX. 
Clock source == CS22:0

Frequency = Fclk/( N.256) 
== 16 x 2^20 /( 8 * 256) 
== 2^24 / ( 2^3 * 3^8 ) 
== 2^13 == 8K cycles 
*/
#include <avr/io.h>
#include <avr/interrupt.h>

int OCR2APin = 11; 
int analogpin = 0;
int dutyCycle = 0; 

#define bit_on(BYTE, BIT)  BYTE |= 1 << BIT;

void setup() {
  pinMode ( analogpin, INPUT);
  pinMode ( OCR2APin, OUTPUT);
  // set dutycycle == 50 
  OCR2A = 128 ; 

  // SET PRESCALE == 8 page 119 ( 16M / 8/ 256) == 7812 ( frequency  )
  TCCR2B &= 0xF0 ; // preserve 4 msb bits 
   TCCR2B |= _BV(CS21) ;  //prescale = 8
  
  // SET MODE 3 ( waveform Generation, fast pwm ) page 117
  bit_on ( TCCR2A, WGM21) ;
  bit_on ( TCCR2A, WGM20) ;
  
  // SET COMPARE OUTPUT, FAST PWM MODE ( PAGE 118) 
  // CLEAR OC2 ON COMPARE MATCH, non-inverting mode
  bit_on( TCCR2A, COM2A1);   
  
  // TIMER/COUNTER INTERRUPT MASK REGISTER (TIMSK) 
  // SET OUTPUT COMPARE MATCH
  bit_on ( TIMSK2, OCIE2A) ; 

  // START FAST PWM MODE 
  bit_on( SREG, SREG_I);
}

void loop() {
; 
}

ISR( TIMER2_COMPA_vect) {
  int analog = analogRead( analogpin)/4;
  OCR2A = analog ;
}
I wonder if I code subtract analogRead (analogpin)/4 by 28?
 

· Registered
Joined
·
746 Posts
Have you put a .1 or .01 cap at the pot terminals? Have you scoped it to see if you have noise? Does it cause run away as long as key is on or does it settle down... Have you a diagram of hookup and wires used in the Arduino... I have not looked at your code closely but need to see the physical setup to see more.... little more please... :confused:
 

· Registered
Joined
·
230 Posts
Discussion Starter · #3 ·
Could you recommend a drawing program? By putting a cap across the terminals do you mean across the 5v and ground? Because if you do I have a 10 uf electrolytic across the 5v and ground supply of the Arduino but not directly on the pot should that make a difference? I am scoping out the output of the arduino tomorrow. It causes runaway as long as the key is on.
Diagram is pretty simple though the outside pot wires are hooked to 5v and ground and the middle wire goes to analog pin 1 and pin 11 is the
pwm output. I think it may be in the code though because the old code did not do this with the same pot :confused:
 

· Registered
Joined
·
746 Posts
The .1 or .01 was meant to be between the ground side and wiper of the pot AT the pot... But it may not be noise but in the code - like I said I like to see more of the inputs and outputs in a drawing...

Make sure your code has no PWM on start - I will try to look at it closer as something does not jive. Key on is sensed on what pin?

Use the schematic draw part of PCB Express - nice free proto software and quick board turn-a-round....
 

· Registered
Joined
·
118 Posts
By dividing analogRead by 4 I am converting analog values of 0 to 1023 to digital of 0 to 255. Is that what you are asking? There is no key sense to the micro.
A better way of doing this is to use the map command to remap your input from one range to another. This gets round any /0 errors which result in fatal crash. - info on function here... http://arduino.cc/en/Reference/Map

Also, may well be worth looking at providing a hardware cutoff of the PWM output until the chip has fully booted as sometimes the chip can output strangeness until its all fully booted.
 
1 - 9 of 9 Posts
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top