im using the analog input to read 5 buttons & i want to read the Ac mains through the transformer i can supply 12vdc throught a voltage devider to measure high & low mains voltageHi,
I guess it´s O.K.
Guess, because we don´t know your requirements about sampling rate and accuracy, precision, resolution.
Guess, because I don´t have the complete datasheet in mind. I recommend you read it first and tell us what´s unclear.
1) usually I don´t need an analog input for reading push buttons. Why do you?
2) requirements?
3) mains is AC. What information of this "AC" do you want to measure? Requirements?
Klaus
thanks Dana you already have guided me to use this method of reading buttons & it works for meRead 5 buttons on 1 pin -
Using analog in to read a push button
I'm working on a project in which I use a lot of the pins on the Arduino; I have run out of most of the pins and and I need to read the value of a push button. Is it possible to read the value of...arduino.stackexchange.com
View attachment 179000
OR this approach :
View attachment 179001
You have to work out the tolerances to set the code V range for each button.
Regards, Dana.
Your question read all the inputs, yes, notwithstanding signal conditioning to use
A/D onboard to its full dynamic range where needed. This of course depends on
what you want from sensors, precision or just gross values.
Regards, Dana.
--- Updated ---
One way of doing the reads and tests :
View attachment 179007
This is mBlock, it converts your code blocks to Arduino code.
Regards, Dana.
hi DanaMy error, the initialization of variables should occur as shown, not the
way I had it prior. Keep in mind the button test values I show are for
reference only, your design for the button and resistor values will
determine what you should use to generate the A/D values that you
will read and test against.
Same is true for the NCT1 & NCT2 readings, I show no actions
on therm once read. Thats where you do additional block code
or call a block subroutine to handle.
View attachment 179066
Regards, Dana.
--- Updated ---
This is how you would do a subroutine :
View attachment 179067
Regards, Dana.
--- Updated ---
Error in above, cannot edit because of auto merge, so here is correct config :
View attachment 179068
Regards, Dana.
void ButtonDelay() {
ButtonsDelay ++;
if ((ButtonsDelay) == 1500) {
ButtonsBusy = 0;
ButtonsDelay = 0;
}
}
void loop() {
int sensorValue = analogRead(A5);
///////////////////////////////////////////////////////////////
if ((sensorValue) > 1000) { // && (ButtonsBusy) == 0) { //1023
ButtonsBusy = 1;
ButtonDelay();
if ((sensorValue) > 1000 && (ButtonsBusy) == 0) { //1023
NoButton = 0;
PowerButton = 1;
LedsDis = PowerLed;
}
}
if ((sensorValue) > 930 && (sensorValue) < 940 ) {//848
ButtonsBusy = 1;
ButtonDelay();
if ((sensorValue) > 930 && (sensorValue) < 940 && (ButtonsBusy) == 0) {//848
NoButton = 0;
SfButton = 1;
LedsDis = SfLed;
}
}
if ((sensorValue) > 730 && (sensorValue) < 745 ) {//649
ButtonsBusy = 1;
ButtonDelay();
if ((sensorValue) > 730 && (sensorValue) < 745 && (ButtonsBusy) == 0) {//649
NoButton = 0;
AlarmButton = 1;
LedsDis = AlarmLed;
}
}
if ((sensorValue) > 785 && (sensorValue) < 799 ) {//689
ButtonsBusy = 1;
ButtonDelay();
if ((sensorValue) > 785 && (sensorValue) < 799 && (ButtonsBusy) == 0) {//689
NoButton = 0;
DownButton = 1;
LedsDis = DownLed;
}
}
if ((sensorValue) > 855 && (sensorValue) < 865 ) {//749
ButtonsBusy = 1;
ButtonDelay();
if ((sensorValue) > 855 && (sensorValue) < 865 && (ButtonsBusy) == 0) {//749
NoButton = 0;
UpButton = 1;
LedsDis = UpLed;
}
}
if ((sensorValue) < 460) {
ButtonsDelay = 0;
if ((sensorValue) < 460) {
NoButton = 1;
LedsDis = B00000000;
InitializeButton();
}
}
///////////////////////////////////////////////////////////////
im working on the hardware & thanks for mentioning about noise cancelling techniquesPost your schematic and your code.
Normally one bounces the button in, then bounces it out, then acts
on the fact it was pressed. That should be the logical flow in your code.
Some designs that need a constant press of a button to effect an operation
would bounce in, do operation, then bounce out.
Bounce test times most folks do it around 50 - 100 mS. I have had switches
need > 500 mS they were so bad. Bounce caused by switch inertia and beam
flex and springs and.....
There is, on most embedded A/Ds, a sample and hold at the input of the ADC.
Essentially a cap that is charged by pin V and then "effectively" disconnected from
pin while conversion is in process. But it is relatively small C, pF range, so that
should not be an issue in your application.
All that being said what are the values of the R's in your button R ladder ? If they
are too high you could be experiencing crosstalk/noise pickup. Something in the
range of 1 - 10K appropriate.
ADC precautions in datasheet :
View attachment 179080
Note the caution of other processes inside ATMEGA generating noise. Effective technique
is to temporarily turn off those, such as PWM, driving leds from ports and other loads,.......
timers......
The ADC is 10 bits, depending on what you use for Vref each LSB is ~ a few mV. Look at
your supply rail using a DSO if you have one, on infinite persistence, bet you will see a
few 100's of mV of noise. So bypassing crucial. The following shows effective esr of various
C technologies :
View attachment 179081
OSCON in this chart are polymer tantalums, best bulk caps for bypassing after MLC's.
Regards, Dana.
if ((sensorValue) > 930 && (sensorValue) < 940 ) {//848
ButtonsBusy = 1;
ButtonDelay();
if ((sensorValue) > 930 && (sensorValue) < 940 && (ButtonsBusy) == 0) {//848
NoButton = 0;
SfButton = 1;
LedsDis = SfLed;
}
}
SfButton = ChkKey(930, 940);
void ButtonDelay() {
ButtonsDelay ++;
if ((ButtonsDelay) == 1500) {
ButtonsBusy = 0;
ButtonsDelay = 0;
}
}
change the 1500 test to (especially if using interrupts)
void ButtonDelay() {
ButtonsDelay ++;
if ((ButtonsDelay) > 1500) {
ButtonsBusy = 0;
ButtonsDelay = 0;
}
}
with a call like
Code:
ChkKey(930, 940);
if you use a static counter variable (for keys 0..5) inside the ChkKey() function you even don´t need to pass a parameter.const uint16_t chkKeyLow[] = {1000, 930, 785, 745 and so on};
I like to go more into detail with this.Note if you do use interrupts
dont forget to declare variables used in interrupt global and volatile.
Actually i cant work more than 3 hours at morning then my brain stops respondingi´m far from being a good programmer, but I try to improve.
But more and more I use constants defined at the beginning instead of using fix numbers in code.
So one could decalare:
setup(){
Timer1.initialize(125000);
Timer1.attachInterrupt(callback);
Timer1.stop();
Serial.begin(9600);
}
void callback() {
interrupter ++;
halfSeconds = (interrupter / 4);
seconds = (interrupter / 8);
minutes = (seconds / 60);
}
void loop() {
int sensorValue = analogRead(A5);
Serial.println(seconds);
///////////////////////////////////////////////////////////////
if ((sensorValue) > 1000) { // && (ButtonsBusy) == 0) { //1023
//ButtonsBusy = 1;
Timer1.start();
//ButtonDelay();
if ((seconds) >= 15) {
if ((sensorValue) > 1000 && (ButtonsBusy) == 0) { //1023
NoButton = 0;
PowerButton = 1;
LedsDis = PowerLed;
Timer1.stop();
seconds = 0;
}
}
}
here i ue TimerOne library for arduinoActually i cant work more than 3 hours at morning then my brain stops responding
& now its break time having fun with both of you Guys
you could help me with this
i use TimerOne library for Arduino sometimes
would you check this code for me ?
when timer starts it count seconds which i can see on serial monitor But when button is pressed the seconds counting goes X5 or more faster
hi KlauscodeCode:[/QUOTE] [QUOTE="johnny78, post: 1742794, member: 596109"] setup(){ Timer1.initialize(125000); // 1000000 = 1 second Timer1.attachInterrupt(callback); Timer1.stop(); //stops TimerOne counting Serial.begin(9600); } [/QUOTE] [QUOTE="johnny78, post: 1742794, member: 596109"] void callback() {// the isr of TimerOne library interrupter ++; //every 0.1.25 second halfSeconds = (interrupter / 4); seconds = (interrupter / 8); minutes = (seconds / 60); } void loop() { int sensorValue = analogRead(A5); Serial.println(seconds); /////////////////////////////////////////////////////////////// if ((sensorValue) > 1000) { //1023 the power button Timer1.start(); // start run timerOne if ((seconds) >= 15) ; if ((sensorValue) > 1000) { //1023 PowerButton = 1; LedsDis = PowerLed; // display result on led declared at the start (int PowerLed = B00010000); Timer1.stop(); stop count the timerOne seconds = 0; //Reset the seconds to 0 for new count } } }
& would you please check the hardware analog inputs setup in post13
thanks
Johnny
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?