Ionizationx: a clean environment is a human right!

Projects by members => Projects by members => Sebosfato => Topic started by: sebosfato on June 30, 2018, 04:20:18 am

Title: Arduino Analog Instruments
Post by: Login to see usernames on June 30, 2018, 04:20:18 am
I need your help to develop some better arduino sketchs for high sampling rate for measuring the signals we need

i´m not a programer and i dont know yet how to work with the registers of arduino

i found a great source that explain how to build a oscilloscope with an arduino uno that may be a source of inspiration

www.instructables.com/id/Girino-Fast-Arduino-Oscilloscope

We need to develop the following

Pulse frequency meter

Gate frequency meter

Pulse time and width meter

Gate time and width meter

Voltage input meter (simplest one assuming it were flat dc but more complicated in case is not filtered)

Current input meter

watts input meter

electron extraction amp meter

and some thermometers around to monitor the parts!

with my system of labview i already have all this but the current is not peak... i only have average because the module for isolated low voltage readings works only on average

but i would like to have some more channels oscilloscope to be able to see all that in real time right now is likely impossible so i will have to work with what i have and think that stan didnt have all this stuff we have today... but likely had the knowledge of using simple needle amp meters and voltmeters =D

Anyone ?
Title: Re: Arduino Analog Instruments
Post by: Login to see usernames on July 28, 2018, 18:33:45 pm
Title: Re: Arduino Analog Instruments
Post by: Login to see usernames on July 28, 2018, 18:39:31 pm
Hello fabio.

Nice project to start with.
I already built the Girino oscilloscope.
There are some changes in the code you can do, they are on the comment section of this instructable, but unfortunately if you want it to work like an oscilloscope, it is not well suitable for frequencies higher than 20khz due to the speed of CPU and the operation time need by the instructions needed to aquire the signal data.
Title: Re: Arduino Analog Instruments
Post by: Login to see usernames on July 28, 2018, 18:42:51 pm
And by the way, if you want to add more channels you can opt to buy a cheaper 2nd hand scope and connect the external trigger to the other oscilloscope that you aready have.

Title: Re: Arduino Analog Instruments
Post by: Login to see usernames on July 29, 2018, 03:25:25 am
I see there is this big limitation in sample rate

my intention is to simply get the peak amp reading and is possible to have a 2 seconds for the sampling time to get the highest peak for example and display... this way i assume is possible to have a better resolution..

does any diference using this method?
 
Title: Re: Arduino Analog Instruments
Post by: Login to see usernames on July 30, 2018, 20:17:38 pm
the cdaq from labview also have this sample rate limitation...

for example for the frequency meters i had to use the frequency dividers to be able to get a lower frequency for it to be able to measure...

i guess the amps present correctly because there i use the modules that come with it and as it is rms it may only drop the rms value

i want a peak ampere meter such i can know exactly what the peak was... this way i will be able to know many of the parameters in the circuit like the peak voltages and the current peak at the secondary can be directly calculated..

i have a multimeter that can take max and min values but it only measure 10 amps... so i guess the best way is to get this 30 amp module from arduino to do the job

it gives 66mv per amp and at 0 amps it gives a 2.5v output so it goes down to 0.5 volts with negative 30 amps and 4.5v with same current in the other direction... works like a resistor but has this 2.5v bias and has the feature of being isolated!

i can connect it to the oscilloscope however the values will need to be calculated manually because of the bias and this 66mv per amps division...

there is one module that is 20 amps and the mv/amp division is a round number 100mv/amp.. this should be easier to use with my oscilloscope... but it will still have the 2.5v bias to get around..

so my friend X-Blade let me try to put the question in a different manner

do you think that using the registers instead of arduino ide is better to achieve this speed? was it required for your girino oscope?





Title: Re: Arduino Analog Instruments
Post by: Login to see usernames on August 02, 2018, 01:25:32 am
i think i found out how to get the peak amps measuring

i will have to use a low foward voltage diode followed by a capacitor at the output of the current sensor than a switch to zero it every certain number of readings...

remembering zero is 2,5 v

i also thought of something simple for using it with the oscilloscope.. i will simply use two voltage dividers

one to create a 2,5v reference point... maybe 2x 470 resistors in series so at the center i will have exactly 2,5volts to use as reference for the reading on the oscilloscope.. and i will do another divider to get intead of 66mv per amp get 50mv per amp.. for that i will probably use a higher resistor value because i dont want this load to change the reading..... probably something like a 20kohm 20turns trim pot

this way it will be able to get the capacitor charged to the highest peak it measures and the arduino will be able to catch the reading! also will be able to get the real time signal on the oscilloscope..

i think in this manner is possible to use the arduino also as the frequency meter and do at least some of all the other functions i was doing on the labview ...


 
Title: fast port manipulation
Post by: Login to see usernames on August 15, 2018, 15:50:22 pm
https://www.instructables.com/id/Fast-digitalRead-digitalWrite-for-Arduino/
Title: pressure sensors i found here
Post by: Login to see usernames on August 15, 2018, 15:57:40 pm
0-7,2psi
https://produto.mercadolivre.com.br/MLB-715523151-sensor-de-presso-mpx5050dp-para-arduino-pic-e-etc-_JM

0-15psi
https://produto.mercadolivre.com.br/MLB-990133178-sensor-mpx5100dp-de-presso-arduino-freescale-_JM

https://produto.mercadolivre.com.br/MLB-1024322697-sensor-de-presso-mpx2100ap-mpx2100-arduino-freescale-_JM

0-7bar
https://produto.mercadolivre.com.br/MLB-708718648-sensor-de-presso-mpx5700-para-arduino-pic-e-etc-_JM


0-10bar
https://produto.mercadolivre.com.br/MLB-992752056-sensor-de-presso-pieso-0-10-bar-05-a-45v--_JM


0-5,8psi
https://produto.mercadolivre.com.br/MLB-933991308-sensor-presso-xgzp040db1r-dip6-40kpa-58-psi-arduino-pic-_JM

0-15psi
https://produto.mercadolivre.com.br/MLB-934015390-sensor-presso-xgzp101db1r-100kpa-15psi-arduino-pic-dip6-_JM

Title: Re: Arduino gms
Post by: Login to see usernames on August 15, 2018, 16:50:06 pm
the arduino will be very useful on the gas management system because for resume it will make the same job much cheaper and easier to develop a full system and control the parameters to tune.. it only need one digital potentiometer to change the parameters and or a touch screen or connection with the computer or cell phone via wireless to show the data  being changed i will use a nextion touchscreen so i dont even need the digital potentiometer 

the main variables we have are

1 the injection timing that is stabilized at acceleration 0 using the pressure signal and frequency signal

2 the frequency compensation variable that is added or subtracted from the injection time to stabilize the rpm of idle at acceleration0

3 rpm - acceleration correlation   set the pedal as the range for the rpm for example at 0 its idle and at middle is 3000 rpm etc so the timing will need to be adjusted regardless of the pressure to reach that rpm when the pedal is pressed

so the accelerator sets directly the comparison rpm seems a good idea... because the program will know how where to go ...

ex at idle you have 1000rpm digital accelerator is set to 0v, than at 1v it is set to 2000rpm it can be set numerically on the program.

so until the car reach that rpm the sensor will keep increasing the timing of the injector to get there every time it reads and see that there is this difference

also we could create a system that instead link the gas production to this instead of only the timing of the injector

so basically until it reach that rpm number of counting it can control the gate time! or simply the gate time should have a direct relatioship with the pedal of course with the range adjustable for the engine

i believe that to have a good acceleration the idle should be at 15% gate or something like that..

than as we press the pedal the system will get crazy to produce the gas needed to reach the rpm set by the pedal



Hope all this reading is useful for you

soon i will post some arduino code here

but i have to buy some sensors and get a 4 stroke engine to play with... i have one here but is broken and i have no money to do either..

so i ask you gently if you believe please donate me... for as small as you can donate will sure help a lot! Every help will be thankful accepted.

i have a link for donations ..

 but if you want you can send me components or test equipments that are useful please send me an inbox 

 if you have any old stuff you dont use in your house do yourself a favor, sell it and do myself a favor donate 10% of what you sold for me or this website and you will fell much better =D



Title: Re: Arduino Analog Instruments
Post by: Login to see usernames on August 16, 2018, 04:19:19 am
i was looking into interrupts that are a way to have a real time port on an arduino only at pin 2 or 3 can do this...

so basically the clock signal from the engine should triger an interrupt that will start a signal to the injectors with the lenght it calculates...
Title: the very first start of the microcontroler Gas Management and injection System
Post by: Login to see usernames on August 18, 2018, 22:11:52 pm
// this is arduino code
// there is 3 inputs one is the pressure signal and one is the clock signal and one is the acceleration pedal
//it will sense the acceleration pedal voltage 0-5v and convert to a set rpm from 0 to 6000 than it will compare the rpm reading with the set and determine the state if is accelerating or deaccelerating. if accelerating it will increase the injector timing until it reach the desired rpm or decrease if its deaccelerating by an increment decrement value
//now i need to stipulate the limits of this functions and a screen and a button to control it 


int clocksignal = 3; //
volatile float miliseconds = 5; // injection timming
int injector = 13;  // injector output
int rpmread = 0;
int accel = A0;   // analog/digital accelerator signal
int pressure = A1;
int rpmset = 0; // rpm determined by analog/digital accelerator
String state;
float timeincrement = 0.01;
float timedecrement = 0.01;
int mininjectiontime = 0;
int maxinjectiontime=10;

void setup() {
  Serial.begin(9600);
  pinMode(clocksignal, INPUT_PULLUP);
  pinMode(injector, OUTPUT);
  attachInterrupt(1, INJECTION, FALLING);

 

;}

void loop() {

 rpmset=(analogRead(accel)*5);   // rpm determined by analog/digital accelerator

 if (rpmset > rpmread ) {
  state="Accelerating" ;
  miliseconds=(miliseconds+timeincrement);
 
}
else {
  state="Deaccelerating";
  miliseconds=miliseconds-timedecrement;



}

  //Serial.println(digitalRead(timming));
  delay(500);
  Serial.print("Accelerator  ");
Serial.print(rpmset);
Serial.println("RPM SET");
Serial.print("Injection Time ");
Serial.print(miliseconds);
Serial.println(" miliseconds");
Serial.print(rpmread);
Serial.println(" RPM Measured ");
Serial.println(state);
 
}

void INJECTION() {
   digitalWrite(injector, HIGH);
   delayMicroseconds(miliseconds*1000);
   digitalWrite(injector, LOW);
   

   
   Serial.print("INJECTED");Serial.print(miliseconds);Serial.print("miliseconds");
   


Serial.println("");Serial.println("");Serial.println("");Serial.println("");
   
}

Title: Re: Arduino Analog Instruments
Post by: Login to see usernames on August 19, 2018, 07:44:25 am
i added a taskscheduler to have multiple loops for different functions other than the interrupt... one is just to print on the serial monitor every 3 seconds... what i noticed is that to print the info causes instability of the signal but also without printing there are other factors to get hid off

i used one of the outputs of the arduino as a clock generator to test the rpm meter and worked but there is still some instability

i added the limits of the rpm and injector timing respectively 0-7000rpm 0-5v ,  1ms and 10ms.

so the accelerator set the rpm and the arduino set the injectors parameters until it matches the rpm desired

anyone who wants to help finding problems and solutions for them i appreciate




unsigned long newtime;
unsigned long lasttime;

#include <TaskScheduler.h>

int clocksignal = 3; // //input interrupt 1 / pin 3
float injectiontime = 5; //ms  injection timming
int injector = 13;  // injector output
int rpmread = 0;
int accel = A0;   // analog/digital accelerator signal
int pressure = A1;
int rpmset = 0; // rpm determined by analog/digital accelerator
String state;
float timeincrement = 0.1;
float timedecrement = 0.1;
int mininjectiontime = 1;
float maxinjectiontime=10;
int maxrpmset=7000;
int clockcount=0;
int lastclockcount;
int fakerpm=20; // 10ms=6000rpm 20ms=3000 30ms= 2000 60= 1020rpm 75 =780 //fakesignal repetition
int fakeclock = 5; //pin5 output fakeclock   --- connect to pin 3 for simulating engines on .. controle its rpm at task t4


void t1Callback();
void t2Callback();
void t3Callback();
void t4Callback();
//Tasks
Task t1(300, TASK_FOREVER, &t1Callback);    //
Task t2(3000, TASK_FOREVER, &t2Callback);     //
Task t3(500, TASK_FOREVER, &t3Callback); //
Task t4(fakerpm, TASK_FOREVER, &t4Callback);     // 10ms=6000rpm 20ms=3000 30ms= 2000 60= 1020rpm 75 =780 //fakesignal repetition
Scheduler runner;


void INJECTION() {  //interrupt routine
 
 
   digitalWrite(injector, HIGH);
   delayMicroseconds(injectiontime*1000);
   digitalWrite(injector, LOW);
   clockcount++;
 // Serial.print("INJECTED FOR: ");Serial.print(injectiontime);Serial.println("injectiontime");
 

   //Serial.println(clockcount);Serial.println("");Serial.println("");
   //Serial.print(rpmread);
//Serial.println(" RPM Measured ");
}


void t1Callback() {
   
 
 rpmset=constrain((analogRead(accel)*6),0,maxrpmset);   // rpm determined by analog/digital accelerator

 if (rpmset > rpmread ) {
  state="Accelerating" ;  // injection time increase
  injectiontime=constrain((injectiontime+timeincrement),1,10);
  }
else {
  state="Deaccelerating"; // injection time reduce
  injectiontime=constrain((injectiontime-timedecrement),mininjectiontime,maxinjectiontime); 

}
 }

void t2Callback() {

Serial.print("Accelerator  ");
Serial.print(rpmset);
Serial.println(" RPM SET");
Serial.print("Injection Time ");
Serial.print(injectiontime);
//Serial.print(injectiontime);
Serial.println(" miliseconds");
Serial.print(rpmread);
Serial.println(" RPM Measured ");
Serial.println(state);
Serial.println(lastclockcount);

}
//____________________________________________________________________________________________________task3
void t3Callback() {
rpmread=(clockcount-lastclockcount)*60*2;
   lastclockcount=clockcount;
 }

//____________________________________________________________________________________________________task4

void t4Callback() {// fake signal

digitalWrite(fakeclock, HIGH);
delayMicroseconds(100);
digitalWrite(fakeclock, LOW); 
  }



void setup() {
  Serial.begin(9600);
  pinMode(clocksignal, INPUT_PULLUP);
  pinMode(injector, OUTPUT);
  pinMode(fakeclock, OUTPUT);

  attachInterrupt(1, INJECTION, FALLING);

 
  runner.init(); //"Initialized scheduler"
  runner.addTask(t1);
  runner.addTask(t2);
 runner.addTask(t3);
  runner.addTask(t4);
 t1.enable();   
 //t2.enable(); 
 t3.enable(); 
 t4.enable();

;}

void loop() {
runner.execute();
 
}


Title: Re: Arduino Analog Instruments
Post by: Login to see usernames on August 19, 2018, 09:26:28 am
with some the hint of the guy in the video on the first page for turning off the timer that creates those strange behavior and by manipulating the ports directly instead of the digitalwrite  i was able to generate a very extremely stable frequency nicely almost 50% square 500,230khz with arduino nano using 2useconds on and off (this delay should actually give 250khz but strangely gives 500) using 1usec on and 1 off gives 2Mhz but is not square anymore

this may be the scales it have of division inside the processor..

i imagine the processor has a 16Mhz cristal as a clock source so 2Mhz is 16/8 and 500 is 16/32

but i still didnt understood how does it relate with the time and frequency discrepancy

at lower frequency it seems to have lower discrepancy but although it generate many very precise frequencies it does not generate a continum analog like of course


here is the code to start playing with it :
ps i left the digitalwrite there for you to compare..



//int fakeclock = 13;

void setup() {
//
//pinMode(fakeclock, OUTPUT);
DDRB |= 0B00100000;
cli();
TIMSK0 &= ~(1 << TOIE0);
sei();
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
//digitalWrite(fakeclock, HIGH);
PORTB |= 0B00100000;
delayMicroseconds(2);
PORTB &= 0B11011111;
//digitalWrite(fakeclock, LOW); 
delayMicroseconds(2);
}



Title: Re: Arduino Analog Instruments
Post by: Login to see usernames on August 19, 2018, 09:48:51 am
the possible outputs with different smallest  configurations of delaymicroseconds are :

2Mhz 1:1
800,360khz   1:2
500Khz 2:2
333khz  3:2
250khz  3:3
200khz  4:3
166,744khz  4:4
142,922khz 5:4
125,058      5:5
111,162   5:6

at lower frequencies the precision distance between frequency decreases

the order of the factor dont change the time per se meaning that the on time and off time are exactly proportional

it seems the microseconds is the lowest division possible
Title: Re: Arduino Analog Instruments
Post by: Login to see usernames on August 19, 2018, 16:43:12 pm
http://playground.arduino.cc/Code/BitMath
Title: Re: Arduino freq generator
Post by: Login to see usernames on August 20, 2018, 22:23:01 pm
i was trying to make a frequency generator with arduino and a rotary encoder... was harder than i imagined...

but i´m using the encoder to count up or down the delay time the encoder got unresponsive at low frequencies so i had to take other way

i used a interrupt based library for the encoder so it connects to A2 and A3 analog inputs..

if it is going up it sums with the delay time according to a range determined by the encoder switch connected to D4 so if i press it it can sum or subtract 1 , 10 or 100 or  for each encoder count.. the value of the delay range within is btw 1us and 16300 (thats the upper limit lowest freq)

because of the limited range the lowest freq that can be generated with delayMicrosecond function is 30Hz but using normal delay it can be much lower perhaps 0,03hz

the highest is 254khz

freq adjust

at 100hz  the smallest increase step is 0,02hz    //  dutycycle49,9%
at 1khz   the smallest increase step is 2<>2hz   //  dutycycle  49,8%
at 10khz the smallest increase step is 185<>200hz   //  dutycycle   48,6%
at 100khz the smallest increase step is  17kz<>26kz    //  dutycycle 36,5%
at 254khz the smallest decrease step is  86kz<     //  dutycycle 15,8%

the frequency is stable with cristal precision! <1%

there is a lower frequency noise on the voltage level of the output pulses but it can be reduced adding more capacitors to the arduino voltage line i guess it could increase the frequency stability even further


the code:

// -----
// InterruptRotator.ino - Example for the RotaryEncoder library.
// This class is implemented for use with the Arduino environment.

// -----because of the limited range the lowest freq that can be generated with delayMicrosecond function is 30Hz but using normal delay it can be much lower perhaps 0,03hz

//the highest is 254khz

//freq adjust

//at 100hz  the smallest increase step is 0,02hz    //  dutycycle49,9%
//at 1khz   the smallest increase step is 2<>2hz   //  dutycycle  49,8%
//at 10khz the smallest increase step is 185<>200hz   //  dutycycle   48,6%
//at 100khz the smallest increase step is  17kz<>26kz    //  dutycycle 36,5%
//at 254khz the smallest decrease step is  86kz<     //  dutycycle 15,8%

//the frequency is stable with cristal precision! <1%

//i used This example that checks the state of the rotary encoder in the loop() function.
// The current position is printed on output when changed.

// Hardware setup:
// Attach a rotary encoder with output pins to A2 and A3.and switch to pin 3 to trigger interrupt1 added a small debounce capacitor from this pin to ground
// The common contact should be attached to ground.

#include <RotaryEncoder.h>

unsigned int range=100;
int swt=3;
unsigned int pos = 0;
unsigned int microdelay;

RotaryEncoder encoder(A2, A3);// Setup a RoraryEncoder for pins A2 and A3:

void setup()
{
DDRB |= 0B00100000; // activate pin13
cli();
TIMSK0 &= ~(1 << TOIE0);  // disable timer (no millis)
sei();

pinMode(swt, INPUT_PULLUP);
attachInterrupt(1,swtch,FALLING);  // pin3
Serial.begin(57600);
Serial.println("Arduino Frequency Generator with RotaryEncoder library.");
// You may have to modify the next 2 lines if using other pins than A2 and A3
PCICR |= (1 << PCIE1);    // This enables Pin Change Interrupt 1 that covers the Analog input pins or Port C.
PCMSK1 |= (1 << PCINT10) | (1 << PCINT11);  // This enables the interrupt for pin 2 and 3 of Port C.
}


// The Interrupt Service Routine for Pin Change Interrupt 1
// This routine will only be called on any signal change on A2 and A3: exactly where we need to check.
ISR(PCINT1_vect) {
  encoder.tick(); // just call tick() to check the state.
}

void loop()
{
  unsigned int newPos = encoder.getPosition();
  if (pos != newPos) {
  // Serial.println(newPos);
   Serial.println(microdelay);// Read the current position of the encoder and print out when changed.

 if ((newPos > pos)&& ((microdelay+range) <= 16300))  {
  microdelay= microdelay + range;
  }else if ((newPos<pos) && ((microdelay-range) >= 1)&& (microdelay-range)<=16300){
    microdelay= microdelay - range;
  }
     pos = newPos;
  }

microdelay= constrain((microdelay),1,16300);
PORTB |= 0B00100000;   //pin 13  on
   delayMicroseconds(microdelay);
PORTB &= 0B11011111;   //pin 13  off
   delayMicroseconds(microdelay);


       
  }
void swtch (){
 
    if (digitalRead(swt)== LOW)range=range*10;

 if (range > 100) range=1;
Serial.println (range);
  }

Title: Re: Arduino Analog Instruments
Post by: Login to see usernames on August 21, 2018, 03:55:30 am
just missing the save function to have the last frequency automatically recall

i dont think it will work on esp chips but probably will work on any arduino board

i´m using arduino nano 328/p processor to upload i have to select on arduino ide the nano board and set it as old boot loader since mine is a china one probably..

Title: Re: Arduino Analog Instruments
Post by: Login to see usernames on August 21, 2018, 05:26:13 am

i turned off serial port of the arduino

i tweeked a little to start at 10hz but this also reduced the max freq to 130khz because i had to use 3 delays to lower from 30 to 10 hz the steps also changed it could be improved using a float perhaps but is already good for what i need now that is just a clock output to mimic the rpm sensor.. this will trigger the other arduino injection system while i dont plug it to the car fox ex

i improved the rotary input pins so i can use a fix pack of wires  in right order

and reversed the action of the switch and added a range  so  it now dont really need the save function because it became very fast to find the desired frequency

the button basically set how fine is the adjust and at the same time as consequence limit the ranges making it easy to work without display

now it goes from

10-555hz
10-1.6khz
10-16khz
10-130kz

hope you enjoy

new code:

// -----
// InterruptRotator.ino - Example for the RotaryEncoder library.

// -----because of the limited range the lowest freq that can be generated with delayMicrosecond function is 30Hz but using normal delay it can be much lower perhaps 0,03hz

//the highest is 254khz

//freq adjust

//at 100hz  the smallest increase step is 0,02hz    //  dutycycle49,9%
//at 1khz   the smallest increase step is 2<>2hz   //  dutycycle  49,8%
//at 10khz the smallest increase step is 185<>200hz   //  dutycycle   48,6%
//at 100khz the smallest increase step is  17kz<>26kz    //  dutycycle 36,5%
//at 254khz the smallest decrease step is  86kz<     //  dutycycle 15,8%

//the frequency is stable with cristal precision! <1%

//i used This example that checks the state of the rotary encoder in the loop() function.
// The current position is printed on output when changed.

// Hardware setup:
// Attach a rotary encoder with output pins to A2 and A3.and switch to pin 3 to trigger interrupt1 added a small debounce capacitor from this pin to ground
// The common contact should be attached to ground.

#include <RotaryEncoder.h>

unsigned int range=1000;
int swt=3;
unsigned int pos = 0;
unsigned int microdelay=16300;

RotaryEncoder encoder(A3, A2);// Setup a RoraryEncoder for pins A2 and A3:

void setup()
{
DDRB |= 0B00100000; // activate pin13
cli();
TIMSK0 &= ~(1 << TOIE0);  // disable timer (no millis)
sei();

pinMode(swt, INPUT_PULLUP);
attachInterrupt(1,swtch,FALLING);  // pin3
//Serial.begin(57600);
//Serial.println("Arduino Frequency Generator with RotaryEncoder library.");
// You may have to modify the next 2 lines if using other pins than A2 and A3
PCICR |= (1 << PCIE1);    // This enables Pin Change Interrupt 1 that covers the Analog input pins or Port C.
PCMSK1 |= (1 << PCINT10) | (1 << PCINT11);  // This enables the interrupt for pin 2 and 3 of Port C.
}


// The Interrupt Service Routine for Pin Change Interrupt 1 but the pin 3 interrupt sill work
// This routine will only be called on any signal change on A2 and A3: exactly where we need to check.
ISR(PCINT1_vect) {
  encoder.tick(); // just call tick() to check the state.
}

void loop()
{
  unsigned int newPos = encoder.getPosition();
  if (pos != newPos) {
  // Serial.println(newPos);
   //Serial.println(microdelay);// Read the current position of the encoder and print out when changed.

 if ((newPos > pos)&& ((microdelay+range) <= 16300))  {
  microdelay= microdelay + range;
  }else if ((newPos<pos) && ((microdelay-range) >= 1)&& (microdelay-range)<=16300){
    microdelay= microdelay - range;
  }
     pos = newPos;
  }

microdelay= constrain((microdelay),1,16300);
PORTB |= 0B00100000;   //pin 13  on
   delayMicroseconds(microdelay);
    delayMicroseconds(microdelay);
     delayMicroseconds(microdelay);
PORTB &= 0B11011111;   //pin 13  off
   delayMicroseconds(microdelay);
    delayMicroseconds(microdelay); delayMicroseconds(microdelay);
    }
void swtch (){
if (digitalRead(swt)== LOW)range=range/10;
if (range <1) range=1000;
//Serial.println (range);
  }

Title: Re: Arduino Analog Instruments
Post by: Login to see usernames on September 07, 2018, 05:16:07 am
https://playground.arduino.cc/Main/ACPhaseControl

ac power control
Title: Re: Arduino Analog Instruments
Post by: Login to see usernames on September 24, 2019, 03:57:54 am
oh luckly i had something here