Notifications
Clear all

Help with project code for ATTINY13

14 Posts
6 Users
4 Likes
123 Views
Hacker
(@hacker)
Active Member
Joined: 2 years ago
Posts: 8
Topic starter  

What I’m trying to do is, compare the resistance between two resistors using a voltage divider and when one resistance goes beyond say 20% a another pin will activate a light.
I feel I don’t have a clue as to what I am doing.
Two resistors are setup between pin 5 and pin 4 with pin 3 connected in the middle on the two. Pin 3 will make sure that the two resistor values are about the same. But if one's resistance goes too low or too high by 20% pin 0 will go high and signal a LED.
I am using Arduino IDE 1.8.16 programming an ATTINY13a

Here is the code I came up with...

const int sensorPin = A3; // Analog input pin that senses Vout
int sensorValue = 0; // sensorPin default value
float Vin = 4.5; // Input voltage
float Vout = 0; // Vout default value
float Rref = 999000; // Reference resistor's value in ohms (you can give this value in kiloohms or megaohms - the resistance of the tested resistor will be given in the same units)
float R = 0; // Tested resistors default value

void setup ()
{
pinMode(5, OUTPUT);
pinMode(3, INPUT);
pinMode(4, OUTPUT);
pinMode(0, OUTPUT);
digitalWrite(5, HIGH);
digitalWrite(4, LOW);
digitalWrite(0, LOW);
analogRead(3);
}

void loop ()
{
sensorValue = analogRead(sensorPin); // Read Vout on analog input pin A0 (Arduino can sense from 0-1023, 1023 is 5V)
Vout = (Vin * sensorValue) / 1023; // Convert Vout to volts
R = Rref * (1 / ((Vin / Vout) - 1)); // Formula to calculate tested resistor's value
if R > (Rref * .20) || R < Rref * .20)
then digitalWrite(0, HIGH);
delay(25000); // Delay in milliseconds between reeds

Am I on the right track?


   
Inst-Tech reacted
Quote
Topic Tags
Ron
 Ron
(@zander)
Famed Member
Joined: 2 years ago
Posts: 3432
 

@hacker What is the purpose of your project?

"Don't tell people how to do things. Tell them what to do and let them surprise you with their results.” - G.S. Patton, Gen. USA
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon


   
ReplyQuote
Hacker
(@hacker)
Active Member
Joined: 2 years ago
Posts: 8
Topic starter  

@zander basically it's an alert system to check if one resistor has been cut or bypassed. But am I on the right track?

 

I don't know what I'm doing wrong, but I can't find anything on YouTube to help me with this problem.


   
ReplyQuote
Ron
 Ron
(@zander)
Famed Member
Joined: 2 years ago
Posts: 3432
 

@hacker I just searched YouTube for 'voltage divider' and got a lot of hits. What the heck did you search for?

"Don't tell people how to do things. Tell them what to do and let them surprise you with their results.” - G.S. Patton, Gen. USA
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon


   
ReplyQuote
Inst-Tech
(@inst-tech)
Reputable Member
Joined: 1 year ago
Posts: 335
 

Posted by: @hacker

What I’m trying to do is, compare the resistance between two resistors using a voltage divider and when one resistance goes beyond say 20% a another pin will activate a light.
I feel I don’t have a clue as to what I am doing.
Two resistors are setup between pin 5 and pin 4 with pin 3 connected in the middle on the two. Pin 3 will make sure that the two resistor values are about the same. But if one's resistance goes too low or too high by 20% pin 0 will go high and signal a LED.
I am using Arduino IDE 1.8.16 programming an ATTINY13a

Here is the code I came up with...

const int sensorPin = A3; // Analog input pin that senses Vout
int sensorValue = 0; // sensorPin default value
float Vin = 4.5; // Input voltage
float Vout = 0; // Vout default value
float Rref = 999000; // Reference resistor's value in ohms (you can give this value in kiloohms or megaohms - the resistance of the tested resistor will be given in the same units)
float R = 0; // Tested resistors default value

void setup ()
{
pinMode(5, OUTPUT);
pinMode(3, INPUT);
pinMode(4, OUTPUT);
pinMode(0, OUTPUT);
digitalWrite(5, HIGH);
digitalWrite(4, LOW);
digitalWrite(0, LOW);
analogRead(3);
}

void loop ()
{
sensorValue = analogRead(sensorPin); // Read Vout on analog input pin A0 (Arduino can sense from 0-1023, 1023 is 5V)
Vout = (Vin * sensorValue) / 1023; // Convert Vout to volts
R = Rref * (1 / ((Vin / Vout) - 1)); // Formula to calculate tested resistor's value
if R > (Rref * .20) || R < Rref * .20)
then digitalWrite(0, HIGH);
delay(25000); // Delay in milliseconds between reeds

Am I on the right track?

Hi @hacker, Yes you are on the right track..but to find the tolerance part of your scheme, you must first add the (Rref* .20) to the Rref, as another variable like Tohigh= (Rref+(Rref*.20)), and then do likewise to Tolow=(Rref-(Rref*.20)), then compare R to those values...You also need the use the { and } brackets in your if statement ..eg:

Tohigh= (Rref+(Rref*.20));

Tolow=(Rref-(Rref*.20));

If (R>Tohigh || R<Tolow) {

        digitalWrite(0,HIGH);

}

delay(25000):

Hope this helps..

Regards,

LouisR

 

LouisR


   
Hacker reacted
ReplyQuote
robotBuilder
(@robotbuilder)
Noble Member
Joined: 3 years ago
Posts: 1556
 

@hacker 

Unsure what you are doing or why a resistor would be cut or bypassed

Modified with the suggestions by @inst-tech but haven't really figured out what you are doing as no complete circuit design has been shown.

 

const int sensorPin = A3; // Analog input pin that senses Vout
int sensorValue = 0;      // sensorPin default value
float Vin = 4.5;          // Input voltage
float Vout = 0;           // Vout default value
// Reference resistor's value in ohms (you can give this value in kiloohms or megaohms
// - the resistance of the tested resistor will be given in the same units)
float Rref = 999000;  
float R = 0; // Tested resistors default value

float toHigh = 0;
float toLow = 0;

void setup ()
{
  pinMode(5, OUTPUT);
  pinMode(3, INPUT);
  pinMode(4, OUTPUT);
  pinMode(0, OUTPUT);
  digitalWrite(5, HIGH);
  digitalWrite(4, LOW);
  digitalWrite(0, LOW);
  analogRead(3);
}

void loop ()
{  
  sensorValue = analogRead(sensorPin); // Read Vout on analog input pin A0 (Arduino can sense from 0-1023, 1023 is 5V)
  Vout = (Vin * sensorValue) / 1023;   // Convert Vout to volts
  R = Rref * (1 / ((Vin / Vout) - 1)); // Formula to calculate tested resistor's value

  toHigh = (Rref+(Rref*.20));

  toLow = (Rref-(Rref*.20));

  if (R>toHigh || R<toLow) {
        digitalWrite(0,HIGH);
  }
  
  
  delay(25000);                    // Delay in milliseconds between reeds
}



 

 

 


   
Inst-Tech reacted
ReplyQuote
Hacker
(@hacker)
Active Member
Joined: 2 years ago
Posts: 8
Topic starter  

@zander What I'm looking for is... Am I declaring the pens and setting them correctly. It's setting up the script that I want to make sure I'm doing right.


   
ReplyQuote
Ron
 Ron
(@zander)
Famed Member
Joined: 2 years ago
Posts: 3432
 

@hacker Sorry, several folks are trying to help but still missing some information. Also, in your latest to me, what is 'pen', and what do you mean by 'script'? I have been involved with computers since 1959 and a script to me is something like bash for 'command language interpreter'.

Use the IDE Tools / Auto Format and get the code readable. Then read the help here to learn how to post the code like @robotbuilder did above. Also you need to show us a wiring diagram.

"Don't tell people how to do things. Tell them what to do and let them surprise you with their results.” - G.S. Patton, Gen. USA
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon


   
ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 1 year ago
Posts: 2124
 

Posted by: @hacker

What I'm looking for is... Am I declaring the pens and setting them correctly. It's setting up the script that I want to make sure I'm doing right.

From that perspectve,I would suggest the following ...

1) your sensorPin value is set to A3 but the comment where it's read says that the reading is taken on pin A0. Pick a pin and stick with it 🙂

2) You only declare the sensorPin but then you set up pins 5, 3, 4 and zero (whatever that is). You should add const int <something> = 5 (etc) definitions for the other pins. Giving them names may also help you remember what they're for later.

3) there's no point in reading pin 3 inside the setup() section (is there ?)

4) Since all you want is comments on the overall layout, I won't comment on the body of code except to say it needs work too.

Hope this helps.

 

Experience is what you get when you don't get what you want.


   
Ron reacted
ReplyQuote
Ron
 Ron
(@zander)
Famed Member
Joined: 2 years ago
Posts: 3432
 

@will Excellent work as always, let's just hope the OP can follow directions better than some newbies.

"Don't tell people how to do things. Tell them what to do and let them surprise you with their results.” - G.S. Patton, Gen. USA
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon


   
ReplyQuote
Ron
 Ron
(@zander)
Famed Member
Joined: 2 years ago
Posts: 3432
 

@hacker Can you tell us why you want to build this device? Is it a work or school related thing?

"Don't tell people how to do things. Tell them what to do and let them surprise you with their results.” - G.S. Patton, Gen. USA
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon


   
ReplyQuote
Hacker
(@hacker)
Active Member
Joined: 2 years ago
Posts: 8
Topic starter  

@zander Thank you


   
ReplyQuote
Hacker
(@hacker)
Active Member
Joined: 2 years ago
Posts: 8
Topic starter  

thank you all


   
ReplyQuote
Dazza
(@dazza)
Trusted Member
Joined: 4 weeks ago
Posts: 40
 

I'm not familiar with this chip set, however If you want to monitor a change in behaviour, why not compare the current value of Pin3 (the Analogue Read) with its current state?  

I'm a little confused on why you want the exact values of the resisters reflected in the code, if a line/wire is cut there will be a change in state regardless of direction.

This One, a long time I have I watched. All this life has he looked away to future, to the horizon. Never his Mind on where he was! what he was doing!
Yoda


   
ReplyQuote