Notifications
Clear all

Help with project code for ATTINY13

14 Posts
6 Users
4 Reactions
903 Views
Hacker
(@hacker)
Member
Joined: 4 years ago
Posts: 7
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
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7695
 

@hacker What is the purpose of your project?

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
Hacker
(@hacker)
Member
Joined: 4 years ago
Posts: 7
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)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7695
 

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

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
Inst-Tech
(@inst-tech)
Member
Joined: 3 years ago
Posts: 554
 

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)
Member
Joined: 5 years ago
Posts: 2227
 

   
Inst-Tech reacted
ReplyQuote
Hacker
(@hacker)
Member
Joined: 4 years ago
Posts: 7
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)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7695
 

@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.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2585
 

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.

 

Anything seems possible when you don't know what you're talking about.


   
Ron reacted
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7695
 

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

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7695
 

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

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
Hacker
(@hacker)
Member
Joined: 4 years ago
Posts: 7
Topic starter  

@zander Thank you


   
ReplyQuote
Hacker
(@hacker)
Member
Joined: 4 years ago
Posts: 7
Topic starter  

thank you all


   
ReplyQuote
Dazza
(@dazza)
Member
Joined: 2 years ago
Posts: 49
 

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