i want to make the stuff, which the output is written from the analogread, but when the A3 == HIGH, the output pin 9 is used, but when the A3 == LOW, the output pin 10 is used, and when i simulate it on proteus, it seems not the same as i mean, what's wrong with the code, please need your help.

  int hasil
  int reado;
  int z;
  int baca;
  bool en = false;
  void setup() {
  // put your setup code here, to run once:

  TCCR1A = 0b00100010;
  TCCR1B = 0b00011001;
  TCCR0A = 0; TCCR0B = 0;
  ICR1 = 100000;//disable millis, delay and mciroseconds function 
  //disable millis, delay and mciroseconds function 

   // put your setup code here, to run once:
  pinMode(A2, INPUT); pinMode(A3, INPUT);
  pinMode(9,OUTPUT); pinMode(10,OUTPUT);

  void loop() {
  // put your main code here, to run repeatedly:

 hasil = analogRead(A2);
 baca = digitalRead(A3);
 for (z=0; z<=hasil; z++)
 reado = map (hasil, 0, 255, 0, ICR1);
 if (hasil > 30 && baca == HIGH)
 {analogWrite(9,hasil); analogWrite(10,0);}
 else if (hasil >30 && baca == LOW)
 {analogWrite(9,0); analogWrite(10,hasil);}
 else if (hasil<30)
 {analogWrite(9, 0);
 analogWrite(10, 0);}}

enter image description here

  • 1
    analogWrite starts the PWM signal. you don't let it run and start it again and again. so instead of |_|_|_ you get |||||| – Juraj Apr 27 '19 at 3:25
  • so how to do pwm properly sir @Juraj – john caren Apr 27 '19 at 9:34
  • 1
    analogWrite will start PWM signal. and then call analogWrite again only if you want to change it or stop it – Juraj Apr 27 '19 at 9:42
  • Re “bool en = false;”: Yes, we did figure out that your variable names are not in English, no need to write it explicitly. – Edgar Bonet Apr 27 '19 at 19:47

You wrote:

when i simulate it on proteus, it seems not the same as i mean

The attached picture is not very clear, but it seems to me that it's doing exactly what you asked for: a PWM output that switches between two different output pins. The program should work as expected, with only a few caveats. Let me comment it in some detail:

ICR1 = 100000;

Here you are setting ICR1 to 34464 (i.e. 100000 modulo 216). This is likely not what you mean...


There is no point in initializing the serial interface if you are not going to use it.

for (z = 0; z <= hasil; z++)
    reado = map(hasil, 0, 255, 0, ICR1);

There is no point in calculating many times the exact same value. Actually, there is no point in calculating it even a single time, since you are not using the result anywhere in your program.

analogWrite(9, hasil);

Here you are setting the duty cycle to some number between 31 and 1023. Given that the top value of the timer is 34464, the actual duty cycle will always be less than 3%. This is the reason why it looks like a series of narrow spikes on the virtual scope.

But maybe what you really meant is

reado = map(hasil, 0, 1023, 0, ICR1); // note: 1023, not 255
analogWrite(9, reado);

I would not recommend using analogWrite() with a non-standard top counter value. There is a special case handled in analogWrite() where, if the argument happens to be 255, you get instead a digitalWrite(pin, HIGH). This is most likely not what you want. Now that you started controlling the timer with raw IO access, you should continue this way.

else if (hasil < 30)

Did you consider what the program should do if hasil happens to be exactly 30? None of the if/else cases would then match. You probably meant

else if (hasil <= 30)

but, as suggested by user11249082's answer, a plain else is the best way to ensure you did cover all possible cases.

  • very brief explanation sir, but still don't know how to make the output the same as analog input, would you like to teach me how to do it? – john caren Apr 28 '19 at 3:04
  • @johncaren: Fix the issues I showed you, then try again. – Edgar Bonet Apr 28 '19 at 8:32

The problem as i can see exist in the if else statements . You use multiple else if statements but in the end you must end it with else statement . Use the “if/else if” if you need to choose either one or none of a series of options. You could write the above program using separate if statements such as show below but the if/else if approach is cleaner and better indicates that the program should choose either one or none of the options Here how you do it If (){}.... Else if (){}..... Else if(){} ...... Else{} here this can help you enter link description here

  • thnaks sir, i'll try it – john caren Apr 27 '19 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.