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
. 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?
Here is the new code.
I wonder if I code subtract analogRead (analogpin)/4 by 28?
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 );
}
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 ;
}