Is there anything wrong with this code?

I have some problems with pwm hanging and weird errors Does anyone know if there could be anything wrong with this code?

int CurrentPWM = 5;
int SpeedPWM = 6;
int CurrentIn = 0;
int SpeedIn = 1;

void setup()
{
  pinMode(CurrentPWM, OUTPUT);
  pinMode(SpeedPWM, OUTPUT);
  pinMode(CurrentIn, INPUT);
  pinMode(SpeedIn, INPUT);


}

void loop()
{

    analogWrite(CurrentPWM,map(analogRead(CurrentIn),1023,0,0,255));

    analogWrite(SpeedPWM, map(analogRead(SpeedIn),1023,0,0,255));
    
    delay(10);

}

Thanks....

Look up map() in the Reference section. What are your inputs going to be?

Mark

The inputs will just be 0-5V and i wan't it te be reverted to 5-0V output

analogWrite(CurrentPWM,map(analogRead(CurrentIn),1023,0,0,255));
analogWrite (CurrentPWM, 255 - (analogRead (CurrentIn) / 4));

(no need to make PWM pins "OUTPUT", the analogWrite does it for you. analogRead also means you don't (and shouldn't) set the pinMode of the analogue inputs, because as in your case, you're screwing with the serial pins)

The inputs will just be 0-5V and i wan’t it te be reverted to 5-0V output

Have you got low-pass filters on the outputs?
If not then they’ll always be 5V or 0 V.

The inputs will just be 0-5V and i wan't it te be reverted to 5-0V output

.

  1. That's not what map does.

  2. Digital inputs can only ever be 0 and 1.

Mark

Pins 0 and 1 are used by the serial port on most Arduino boards.

Sorry the inputs should be A0 and A1

  1. That's not what map does.

@Holmes4: Explain, please?

The following should be equivalent, but is uncompiled, and untested.

const byte CurrentPWM = 5;
const byte SpeedPWM   = 6;
const byte CurrentIn  = A0;
const byte SpeedIn    = A1;

void setup()
{
}

void loop()
{
  analogWrite(CurrentPWM, 255 - (analogRead(CurrentIn) / 4));
  analogWrite(SpeedPWM,   255 - (analogRead(SpeedIn) / 4));
  delay(10);
}

Thank you I'll give that a go

It does kill the overhead that's a good solution

Thank you I will post my results :)

  analogWrite(CurrentPWM, 255 - (analogRead(CurrentIn) / 4));
  analogWrite(SpeedPWM,   255 - (analogRead(SpeedIn) / 4));

Could be reduced even further to

  analogWrite(CurrentPWM, 255 - (analogRead(CurrentIn) >> 2));
  analogWrite(SpeedPWM,   255 - (analogRead(SpeedIn) >> 2));

But then we would just be quibbling over a couple extra/less instructions.

Could be reduced even further to

Perhaps you meant:

analogWrite(CurrentPWM, ~(byte) (analogRead(CurrentIn) >> 2));
  analogWrite(SpeedPWM,   ~(byte) (analogRead(SpeedIn) >> 2));

8)

AWOL:

Could be reduced even further to

Perhaps you meant:

analogWrite(CurrentPWM, ~(byte) (analogRead(CurrentIn) >> 2));
  analogWrite(SpeedPWM,   ~(byte) (analogRead(SpeedIn) >> 2));

8)

Show off...

(I was actually quite surprised that it really did come out smaller XD - sometimes, you can over-optimise)

Damn! That's just awesome! :)

saved another 82 bytes :)

The code looks good but still my program keeps hanging at i think random times :~

What I've got connected is for the analog inputs are 2 voltage dividers of 2 10K resistors (0-10 to 0-5V) and for the output two tip127 transistors that i wan't to switch 5v with, i used a 2,4K base resistor (i wan't to be able to switch 5V-1,5A)

Any idea what it could be?

Apart from possible brown-outs, I can't see why it would ever hang. Can you post a schematic? Maybe add some debug prints?

Do you think the program can hang because i didn't connect the voltage reference maybe?

I've connected the reference and placed a 100nF C on the Vcc to GND of the controller and now it seems to work! I think my ADC was screwed by leaving out these connections :~

This was my first ADC field project and now i know...

I will place a low pass filter on the pwm to make my signals now and then i think it'll be perfect!

Thanks for your help