Why this code doesn't work at all? Are there any major errors?

In the last 3 days, I wrote this code to run on an ATTiny13A. The following is the code!

#define out 0
#define charge 1

float batv = 0.0;
float in1v = 0.0;
float in3v = 0.0;
float temp1 = 0.0;
float temp2 = 0.0;
float temp3 = 0.0;
int r1 = 110439; 
int r2 = 21866;
int r3 = 110439;
int r4 = 21866;
int PWM = 0;

void setup() {
  pinMode(out, OUTPUT);
  pinMode(charge, OUTPUT);
  digitalWrite(out, LOW);
  digitalWrite(charge, LOW);
}
  
void loop() {
  read_bat_volt();
  read_in1_volt();
  //read_in3_volt();
  bat_volt_protect();
  bat_charge();
  delay(10);
}

void read_bat_volt(void){
  int analogvalue1 = analogRead(A1);
    temp1 = (analogvalue1 * 5.0) / 1024.0;
    batv = temp1 / (r2/(r1+r2));
}
void read_in1_volt(void){
  int analogvalue2 = analogRead(A2);
    temp2 = (analogvalue2 * 5.0) / 1024.0;
    in1v = temp2 / (r4/(r3+r4));
}
void turn_on_output(void){
  if(batv > in1v)
  digitalWrite(out,HIGH);
  else
  digitalWrite(out, LOW);
}
void bat_volt_protect(void){
  if(batv >= 13)
     digitalWrite(out, LOW);
  else if (batv <= 11) 
     digitalWrite(out, LOW);
   else
     digitalWrite(out, HIGH);
}
void bat_charge(void){
  if(batv <= 11.1){
    PWM++;
    PWM = constrain(PWM,0,254);
  }
  else if(batv >=13) {
    PWM = 0;
    PWM = constrain(PWM,0,254);
  }
  else{
    PWM--;
    PWM = constrain(PWM,0,254);
  }
  analogWrite(charge, PWM);
}

This is a circuit that I made to automatically switch a CCTV camera from main supply to battery power after the main power goes off; And to charge the battery up to 13V if the battery goes below 11v! (The battery is an old car battery from a Chevy Cruze!)

Schematic for the circuit;
Schematic_CCTV Control Circuit Using Attiny13A_2023-01-21.pdf (69.0 KB)
(Note- Maybe the R2 and R4 resistors are changed from 10k to 100k; Does it makes any sense?)

There are no errors seems to pop up in the IDE; But, It doesn't do anything other than slowly turning on the light in pin1 (light), Which seems to be the PWM code works!

I'm using the MicroCore library by MCUDude. And I used the following code to find the resistor values!

int analogPin = 0;
int raw = 0;
int Vin = 5.15;
float Vout = 0;
float R1 = 1753;
float R2 = 0;
float buffer = 0;

void setup(){
Serial.begin(9600);
}

void loop(){
  raw = analogRead(analogPin);
  if(raw){
    buffer = raw * Vin;
    Vout = (buffer)/1024.0;
    buffer = (Vin/Vout) - 1;
    R2= R1 * buffer;
    Serial.print("Vout: ");
    Serial.println(Vout);
    Serial.print("R2: ");
    Serial.println(R2);
    delay(1000);
  }
}

Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE

The problem may not be in the code. The circuit can create problems also.

On an ATTiny, the largest possible positive int is 32767. Declare as long instead.

There may be other problems. Turn on compiler warnings ("verbose", I think, in Preferences).

Well first of all your sketch doesn't actually fit on an ATtiny13 ! just looking at it i was already thinking this with all the floating point calculation sucking up a lot of the available memory, and then just as i read

i thought, let's have a look.
So yes the compiler spits out a warning and then calculates 1176 bytes of flash, which is at 114% then.
So i figure you never successfully uploaded the sketch to the ATtiny, and that is why it doesn't work.

If you want to use an Attiny13 you should avoid floating point calculus (and even float variables i general)
You also don't really need floats. You only need an accuracy of 1 or 2 numbers behind the dot (excused my Dutchism) you can easily create those values in 16-bit integers. (eg 11.1 = 111 or even 1110) Even the Resistor value does not need that accuracy, since the the analog read only has 4 numbers (10-bit)
Try and do multiplications first before division but keep observing the maximum value the variable can hold.
10-bit x 10-bit x 10-bit still fits into a 32-bit variable at any time.

Now you are doing a comparison on a voltage that has a value that makes complete sense to you, but there is no real need for that.
You may not even need to go that far, but i the easiest is just to get rid of the floats, because that just doesn't easily fit onto an ATtiny13's 1KB

This uses 918 bytes of Flash, 5 bytes of SRAM.

#define out 0
#define charge 1

float batv = 0.0;
float in1v = 0.0;
float in3v = 0.0;
float temp1 = 0.0;
float temp2 = 0.0;
float temp3 = 0.0;
const float r1 = 110439;
const float r2 = 21866;
const float r3 = 110439;
const float r4 = 21866;
uint8_t PWM = 0;

void setup() {
  pinMode(out, OUTPUT);
  pinMode(charge, OUTPUT);
  digitalWrite(out, LOW);
  digitalWrite(charge, LOW);
}

void loop() {
  read_bat_volt();
  read_in1_volt();
  //read_in3_volt();
  bat_volt_protect();
  bat_charge();
  delay(10);
}

void read_bat_volt(void) {
  int analogvalue1 = analogRead(A1);
  temp1 = analogvalue1 * (5.0 / 1024.0);
  batv = temp1 * ((r1 + r2) / r2);
}
void read_in1_volt(void) {
  int analogvalue2 = analogRead(A2);
  temp2 = analogvalue2 * (5.0 / 1024.0);
  in1v = temp2 * ((r3 + r4) / r4);
}
void turn_on_output(void) {
  if (batv > in1v)
    digitalWrite(out, HIGH);
  else
    digitalWrite(out, LOW);
}
void bat_volt_protect(void) {
  if (batv >= 13)
    digitalWrite(out, LOW);
  else if (batv <= 11)
    digitalWrite(out, LOW);
  else
    digitalWrite(out, HIGH);
}
void bat_charge(void) {
  if (batv <= 11.1) {
    if (PWM < 254) {
      PWM++;
    }
  }
  else if (batv >= 13) {
    PWM = 0;
  }
  else {
    if (PWM > 0) {
      PWM--;
    }
  }
  analogWrite(charge, PWM);
}

and does it work ?

Thanks for the editing; but it seems not working! Don't know why, but it's what it is!

So I thought to run a good code on a Nano and port it into ATTiny13A. let's see

Hello people;
I made this code (which seems to be working), and it still uses 18 bytes more than 1024 (1042 bytes).

#define out 2

float volt, voltage, temp, temp1, in, input;
int r1 = 104280;
int r2 = 10030;

void setup()
{
  pinMode(out, OUTPUT);
  digitalWrite(out, LOW);
  //Serial.begin(9600);
}

void loop() 
{
  read_volt();
  read_input();
  ext_volt();
  set_output();
}

void read_volt(void){
  volt = analogRead(A0);
  temp = volt * (4.9 / 1023);
  voltage = temp / (r2/(r1+r2));
  // Serial.print("Input Voltage");
  //Serial.println(temp);
  //Serial.print("Actual Voltage = ");
  //Serial.print(voltage);
  //Serial.println("V");
  //delay(2000);
}

void read_input(void){
  in = analogRead(A1);
  temp1 = in * (4.9 / 1023);
  input = temp1 / (r2/(r1+r2));
}

void ext_volt(void){
  if(voltage>=input){
    digitalWrite(out, HIGH);
  }
  else{
    digitalWrite(out, LOW);
  }
}

void set_output(void){
  if(voltage>=10.9 && voltage<=13.1){
    digitalWrite(out, HIGH);
  }
  else{
    digitalWrite(out, LOW);
  }
}

So how can I use int and divide the value (by 100) to get a value with 2 decimals?

Thanks!

First, I couldn't get your code to work (the calculations) without making r2 float

 float r2 = 10030.0;

which is why I was even tryting the calculations, it looked like it might be wrong. So maybe I am only confirming my own bias.

Anyway making it all integer is easy. This code uses 4 byte integers, so be careful. I just bluffed my way through this. Note that intAnswer does not use the function or any floating point maths.

# include <stdio.h>

float volt, voltage, temp, temp1, in, input;
int r1 = 104280;
float r2 = 10030.0;

float read_volt(int reading)
{
  volt = reading;
  temp = volt * (4.9 / 1023);
  voltage = temp / (r2/(r1+r2));
  
  return voltage;
}

int main()
{
    printf("Jello Whirled!\n");

    for (int aIn = 0; aIn <= 1024; aIn += 128) {
        
        int intAnswer = (aIn << 6) / 293 + 2;
        intAnswer >>= 2;

        printf("%d\t%f\t%d\n", aIn, read_volt(aIn),   intAnswer);
        
    }
}

You should probably let the compiler figure out the constants. I had a calculator.

Jello Whirled!
0       0.000000        0
128     6.987370        7
256     13.974739       14
384     20.962109       21
512     27.949478       28
640     34.936848       35
768     41.924217       42
896     48.911587       49
1024    55.898956       56

HTH

a7

first of all as i explained, you don't need a value with 2 decimals.

Also these are constants and the calculation you do with them is the same every time, so you can do that up front.
The result of

(r2/(r1+r2)) = 0,0877438544309334

which is no good to us, we want an integer, and we will have

(4.9 / 1023) =  0,0047898338220919

which is also a constant.
In fact that is all part of the same calculation

0,0047898338220919 /   0,0877438544309334 = 0,0545888239485077

now if we use the 4 significant digits 5459 (rounded) we need to move the dot 5 digits.
, so the result of

in * 5459

will result in a value where 10 volts = 10 * 10,000 = 100,000
and it will be larger than 16 bit so you need to make sure that the calculation is done 32-bit, and we will divide by 100 to get 4 digits significant values. (edit Or divide by 1000 ? someone please check it's 5 a.m here )

uint32_t in = analogRead(A0);
uint16_t voltage = in * 5459 / 100;

now i've declared these types locally but with your code they should be global (or not accessible in other functions, but the result can be compared like this

voltage>=1090 (10.9 )

Lookin' good. I added a column for

        int D_R  = aIn * 5459 / 100;  // 4 byte int
Jello Whirled!
0       0.000000        0       0
128     6.987370        7       6987
256     13.974739       14      13975
384     20.962109       21      20962
512     27.949478       28      27950
640     34.936848       35      34937
768     41.924217       42      41925
896     48.911587       49      48912
1024    55.898956       56      55900

Wrong time to continue here also. CU.

a7

If you (or the preprocessor) figured out the equivalent direct readings those values must come from, you could use those numbers directly, no floating point, no multiplication or division.

  int reading = analogRead(A0);

  if (reading >= 199 && reading <= 240) {

a7

Can't say if it'll work for you but, on an UNO, using this library your code compiles to 1514 bytes flash. Without the library it compiles to 1776 bytes flash.

Thanks, it seems to take only 184 bytes of flash! I will try and share if it works.