Hello, this is my first time using the forum for help, sorry if something is unclear or wrong
Specs:
- 8 MHz
- The Library used is ATTinyCore ( Spence Konde - Dr. Azzy)
- Communicating through FTDI cable
I'm trying to program an ATtiny85, which can send data from my PC (Arduino Terminal), and depending on the data output a specific PWM (duty cycle). I can get the serial communication to work nicely without setting a duty cycle, and vice versa. The problem I face now is that whenever I try to implement both, I can get it to work somewhat. I can read a correct data the first time I send. But whenever I try to renew the data, then I get unknown data in my terminal. But by the fifth time, it reads a correct value ( it varies). It seems likely something with the interrupt or something, because whenever it reads unknown data, the duty cycle goes to zero.
#include <SoftwareSerial.h>
const int Rx = 2; // this is physical pin 7
const int Tx = 0; // this is physical pin 5
SoftwareSerial myserial(Rx, Tx); // set the serial connection
char rc = 0;
int flag = 0;
void PWM_config()
{
DDRB = (1<<PB1); // set PB1 as output to enable PWM generation
TCCR0A=0x00; //Normal mode
TCCR0A|=(1<<WGM00)|(1<<COM0B1); // Clear OC0B on compare Match when up-counting. Set OC0B on compare Match when down-counting ( COM0B1 is set to PB1, if i change it to COM0A1, then pb0. )
TCCR0B=0x00;
TCCR0B |= (1<<CS00); //prescaling with 1
TCNT0=0;
}
void setup() { // This executes once
PWM_config();
Serial.begin(9600); // Initialize serial port
myserial.begin(9600);
pinMode(Rx, INPUT);
pinMode(Tx, OUTPUT);
pinMode(1, OUTPUT); // Prepare output pin
}
void loop() { // This loops continuously
if (myserial.available() > 0) { // Checks if anything written to serial
rc = myserial.read(); // Read said data into the variable "rc"
Serial.println(rc);
if (rc == 49) { // char 1 = int 49
OCR0B = 100;
}
else if (rc == 50) { // // char 2 = int 50
OCR0B = 200;
}
}
Picture of the terminal values can be seen below:
I hope if any of you can help and I appreciate you for using your time looking through this:)