Mega Arduino 2560

Grumpy_Mike:
I think it works for everyone if you hold your mouth right.

Closed works for me.

I studied the code and I understand now how I can change the duty cycle.

Now, I want to change the PWM cycle time to minutes instead of Micro second. In other word, I want to be able to read it on the time axis in minutes. Please help me if you can to do that.

Thank You

Mohareb_heba:
I studied the code and I understand now how I can change the duty cycle.

Now, I want to change the PWM cycle time to minutes instead of Micro second. In other word, I want to be able to read it on the time axis in minutes. Please help me if you can to do that.

Thank You

That was already answered in reply #17.

Mohareb_heba:
I studied the code and I understand now how I can change the duty cycle.

Now, I want to change the PWM cycle time to minutes instead of Micro second. In other word, I want to be able to read it on the time axis in minutes. Please help me if you can to do that.

Thank You

Read up on the bit-bang approach to PWM, it is a form of Blink program. You can vary the blink on and off times at will. Use micros() to time, the 32 bit unsigned long value is good up to 71.some minutes so you can have cycles up to TWICE that long if you desired so.

Thank You for getting back to me, can i use something like this command for example

if ((millis () - when_eggs_started) >= (1000UL * 60 * 3))
{
// eggs are cooked
}

Now I got the code for incrementing the PWM signal but I got ("n" does not name a type) error. How I can Fix it. I attached for you the code.

int LedPin = 11;
void setup(void) {
   pinMode (LedPin, OUTPUT);  // set pin 11 as output (the LED pin)
 // Clear the control registers
 TCCR1B = 0;
 TCCR1A = 0;
 // Set the clock prescale to 1
 TCCR1B |= (1<<CS10);
 // Set TOP to 3999.  
 // Frequency is 16 MHz / (prescale * (TOP+1)) == 4 kHz.
 ICR1 = 3999;         
 // Start at 50% Duty Cycle
 OCR1A = 800;
 // Set mode to 14: FastPWM using ICR for TOP
 TCCR1A |= 1<<WGM11;
 TCCR1B |= (1<<WGM12) | (1<<WGM13);
         
 // Select non-inverting FastPWM  (higher numbers give higher duty cycle)
 TCCR1A |= (1<<COM1A1); 
}
int  n=0,N = 10;
int v=0;
void loop() {
 
//unsigned long tm;
unsigned long t3=180,t2=t3/2, t=(n/N)*t3;
//delay (4000);
//tm=millis();    //current timer
//t=t3;    // time from start of current cycle
if(t<=t2){
 v = v+(5/n);
} else if (t<=t3){ 
v =v-(5/n);
}else{n=-1;}
}
n=n+1;
analogWrite(LedPin,v);
//Serial.print(tm);   
Serial.print(" ");
Serial.print(t);   
Serial.print(" ");
Serial.print(v);
}

That line is outside the loop function, you have your braces wrong. All the lines following are outside the loop as well.

Click on a brace and it will highlight the matching one. Looks like you have too few opening braces.
EDIT :- you had too many closing braces, code should look like:-

v =v-(5/n);
}else{n=-1;}
n=n+1;
analogWrite(LedPin,v);

Hello,
the last code worked but it did not give me the result that I want. I want to do incrementing and decrementing loop.
I attached a picture of what I want to do with the math calculations.

for incrementing loop, when the time increase by 18 sec, record the new value until t=t2, and here the time and voltage will increase.

for decrementing loop, the time will keep increasing but the voltage will decrease until reach t3.

please, help me if you can. you can modify my previous code or write me a new one.

pic 1.pdf (645 KB)

pic 2.pdf (304 KB)

You can not get that waveform out of a low pass filter attached to that PWM output.

In order to get that output you have to run the PWM frequency very fast and then change the duty cycle in steps so that when filtered you get that signal. But as I say that is not the same as the PWM signal you have drawn.

Thank you for answering me back. This is the code that I am using to generate the PWM signal. could you please edit it to give me the stepwise signal that I am looking for. I am not really familiar with arduino. I tried to familiarize my self with it but I keep failing.
Thank you

void setup(void) {
 // Clear the control registers
 TCCR1B = 0;
 TCCR1A = 0;
 // Set the clock prescale to 1
 TCCR1B |= (1<<CS10);
 // Set TOP to 3999.  
 // Frequency is 16 MHz / (prescale * (TOP+1)) == 4 kHz.
 ICR1 = 3999;         
 // Start at 50% Duty Cycle
 OCR1A = 2000;
 // Set mode to 14: FastPWM using ICR for TOP
 TCCR1A |= 1<<WGM11;
 TCCR1B |= (1<<WGM12) | (1<<WGM13);
         
 // Select non-inverting FastPWM  (higher numbers give higher duty cycle)
 TCCR1A |= (1<<COM1A1); 
}
void loop() {
//  // Ramp duty cycle up at about 1 step per millisecond.  Reaches max in about 4 seconds.
//  for (int i=0; i<4000; i++) {
//      OCR1A = i;  
//      delay(1);
//  }
}

could you please edit the below code to convert it from C++ language to arduino code.

#include <iostream>
using namespace std;

int main(){
   
bool increase = true;
   
bool decrease = false;
   
const int NUMBER_OF_TIME_UNIT =30;
   
int time[NUMBER_OF_TIME_UNIT];
   
int voltage[NUMBER_OF_TIME_UNIT];
   
time[0]=voltage[0]=0;
   
for(int i=1;i<NUMBER_OF_TIME_UNIT;i++){
     
 if(increase){
      
time[i] = i;

  voltage[i]=voltage[i-1]+1;

          if(voltage[i]==5){
              
increase=false;

              decrease=true;
   
       }
       }
       
       
else{
           time[i] = i;

          voltage[i]=voltage[i-1]-1;
       
   if(voltage[i]==0){
               
increase=true;

              decrease=false;

           }
       }
   }
   
   
   
for(int i=0;i<NUMBER_OF_TIME_UNIT;i++){
       
cout<<"time: "<<16*time[i]<< "s,  voltage: "<<voltage[i]<<endl;
   }
}

what about this line it show as an error. what I should put instead of it.

#include

I wrote these two command at the end of the code but still I get an error.
could you please write for me the full command and tell me where it should be in the code

Serial.begin::

Serial.print <<"time: "<<16time_<< "s, voltage: "<<voltage<<endl;*_

mohareb_heba:
This is the code that I am using to generate the PWM signal. could you please edit it to give me the stepwise signal that I am looking for.

What about all that earlier stuff about a 4KHz PWM signal? You can not have that along with the full control of the PWM duty cycle. Just wtf do you want to do?
Now you seem to be off on another tangent.