Spark Logic

Generator ON/OFF SOLVED

Hi
This program has a generator and blinking LED.
How to modify it to get PWM signal on PB0 when the LED is ON and no signal when the LED is OFF ?
So far I have this error.

C:\Users\OWNER\AppData\Local\Temp\arduino_modified_sketch_892346\Blink.ino: In function ‘void loop()’:

Blink:34: error: lvalue required as left operand of assignment

if (PB1=1)

^

exit status 1
lvalue required as left operand of assignment

HardwareTimer pwmtimer3(3);
void setup() {
// pinMode(PA7, PWM);
pinMode(PB0, PWM);
pwmtimer3.pause();
pwmtimer3.setPrescaleFactor(400);       // Timer input clock Prescaler = 1 (= 72MHz input ?)
pwmtimer3.setOverflow(100-1);            // PWM Period width for 720kHz ?
pwmtimer3.setCompare(TIMER_CH3, 50);  // PWM High Pulse width is 50% duty (1:1)
pwmtimer3.refresh();
pwmtimer3.resume();
// initialize digital pin PB1 as an output.
pinMode(PB1, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(PB1, HIGH);   // turn the LED on (HIGH is the voltage level)
delay(1000);              // wait for a second
digitalWrite(PB1, LOW);    // turn the LED off by making the voltage LOW
delay(1000);              // wait for a second
if(PB1=HIGH)  //error from this line
{
digitalWrite(PB0,PWM);
//LED = 1;
}
else
{
digitalWrite(PB0, LOW);
// LED = 0;
}
}

Replies15

Thu Aug 09, 2018 9:29 am

if (PB1 == HIGH)

Thu Aug 09, 2018 9:56 am

Thanks
No errors, but ON/OFF is not working

Thu Aug 09, 2018 1:10 pm

I am not sure because I don’t have the hardware around but maybe
void loop() {
digitalWrite(PB1, HIGH);   // turn the LED on (HIGH is the voltage level)
pinMode(PB0, PWM);
delay(1000);              // wait for a second
digitalWrite(PB1, LOW);    // turn the LED off by making the voltage LOW
digitalWrite(PB0, LOW);
delay(1000);              // wait for a second
}

Thu Aug 09, 2018 4:52 pm

ON/OFF is not working

Thu Aug 09, 2018 8:21 pm

ON/OFF is not working

that sentence has tremendous amount of information to help others help you.

Fri Aug 10, 2018 12:10 am

= proposed above solution is not working = does not turn PB1 ON and does not turn PB1 OFF

Fri Aug 10, 2018 12:50 am

… what do you think this is going to do?

digitalWrite(PB0,PWM);

Fri Aug 10, 2018 1:10 am

That was my guessing, I don’t know a proper command for that., should send PWM to that pin, however this is done in line #4 so I have PWM continuously on PB0.
What I looking for is switching function for pin.

Fri Aug 10, 2018 1:17 am

There is a Maple MiniMaple MiniDevelopment and computing boards…8MHz petertumler Thu Sep 24, 2015 10:58 am Hi, I’m trying to underclock a STM32F103 ( Maple Mini ) to 48MHz (for energy efficiency reasons). According to the datasheet .. The full…View in hardware catalog specific example that shows how to use PWM and pwmwrite() , It is called FadingOnBoard

There are posts you can find here that talk about it:
https://www.stm32duino.com/viewtopic.php?t=634

There is the MapleMapleDevelopment and computing boards…expensive short of buying in bulk. many resort to usb-serial converters. today comes leaflabs maple , stm32duino it is ‘disruptive’ in a similar light, user customizable usb is no…View in hardware catalog leaflabs docs:
http://docs.leaflabs.com/static.leaflab … write.html

Fri Aug 10, 2018 2:38 am

Thanks Rick
This is about frequency and duty cycle not about turn on/off the pin output.

Fri Aug 10, 2018 3:16 am

// PB0 - using pwm + timer
// PC13 - so i can see it toggling
HardwareTimer pwmtimer3(3);

void setup() {
pinMode(PB0, PWM);
pwmtimer3.pause();
pwmtimer3.setPrescaleFactor(72);       // Timer input clock Prescaler = 1MHz 72MHz
pwmtimer3.setOverflow(1000 - 1);       // PWM Period width for 1kHz
pwmtimer3.setCompare(TIMER_CH3, 500);  // PWM High Pulse width is 50% duty (1:1)
pwmtimer3.refresh();
pwmtimer3.resume();

// initialize led as output
pinMode(PC13, OUTPUT);
}

int state=0;

void loop() {
state ^= 1; // toggle state

digitalWrite(PC13, (state ? HIGH : LOW)); // toggle bluepill led

if ( state ) {
pwmtimer3.resume();
}
else {
pwmtimer3.pause();
}

delay(100); // let the pwm run or stop for 100 ms
}

Fri Aug 10, 2018 3:26 am

Or you could use pwmWrite. This example simulates an IR 36kHz signal being turned on and off
// PB0 - using pwm + timer
// PC13 - so i can see it toggling
HardwareTimer pwmtimer3(3);

void setup() {
pinMode(PB0, PWM);
pwmtimer3.pause();
pwmtimer3.setPrescaleFactor(1000);  // Timer input clock Prescaler = 72kHz
pwmtimer3.setOverflow(2 - 1);       // PWM Period width for /2 = 36kHz
pwmtimer3.setCompare(TIMER_CH3,1);  // PWM High Pulse width is 50% duty (0->1)
pwmtimer3.refresh();
pwmtimer3.resume();

// initialize led PC13 as an output.
pinMode(PC13, OUTPUT);
}

int state=0;

void loop() {
state ^= 1; // toggle state

digitalWrite(PC13, (state ? HIGH : LOW)); // toggle bluepill led

if ( state ) {
pwmWrite(PB0,1);
}
else {
pwmWrite(PB0,0);
}

delay(100); // let the pwm run or stop for 100 ms
}

Fri Aug 10, 2018 3:39 am

Thanks Rick
It is working, I used blinking LED to demonstrate the problem in simply way, but my application the LED is on when the button attached to another pin (PC13 ) is pressed.
Sorry , I should start from this.

Fri Aug 10, 2018 3:51 am

I am trying to use
digitalRead(PC13 ); // toggle bluepill

Fri Aug 10, 2018 4:54 am

Thanks Rick
THE PROBLEM SOLVED
// PB0 - using pwm + timer
// PC13 - so i can see it toggling
const int pushBtn=PC13;
HardwareTimer pwmtimer3(3);
void setup() {
pinMode(PB0, PWM);
pwmtimer3.pause();
pwmtimer3.setPrescaleFactor(72);       // Timer input clock Prescaler = 1MHz 72MHz
pwmtimer3.setOverflow(1000 - 1);       // PWM Period width for 1kHz
pwmtimer3.setCompare(TIMER_CH3, 500);  // PWM High Pulse width is 50% duty (1:1)
pwmtimer3.refresh();
pwmtimer3.resume();
// initialize led as output
//pinMode(PC13, OUTPUT);
// pinMode(PC13,INPUT);
pinMode(pushBtn,INPUT);
}
int state=0;
void loop() {
// state ^= 1; // toggle state
//digitalWrite(PC13, (state ? HIGH : LOW)); // toggle bluepill led
//digitalRead(PC13, (state ? HIGH : LOW));
// digitalRead(PC13, state );
digitalRead(PC13);
//if ( state )
//if(PC13==HIGH)
///////////////////////////////
int btnValue=digitalRead(pushBtn); // Read the Button State Pressed or not
if(btnValue==1)
////////////////////////////////
{
pwmtimer3.resume();
}
else {
pwmtimer3.pause();
}
delay(100); // let the pwm run or stop for 100 ms
}

Post a reply

Optional · up to 10 MiB