arduino analog returns offset value after accident - arduino

I am painfully new to arduino so keep that in mind,
while experimenting with a volt meter sketch with a resistor divider (100k and 10k resistor) then I tried to connect a 5v power supply (regulated off board) I doubt it was the voltage due to the divider but I do not know about the current. what i didn't realize was I had not powered it on and got the infamous smell I the quickly disconnected it. after that i tried testing it again (this mainly took place on the analog 0 pin) the board seems to function fine except for the analog pins they will now read ~200 and will not work correctly in my other programs like a ohm meter this could be normal i don't know but i do know that it is not working anymore so any explanation is appreciated
when i say its not functioning i mean my sketches aren't working but it might help you to know my (not exactly mine) sketch
int analogPin= 0;
int raw= 0;
int Vin= 5;
float Vout= 0;
float R1= 1000;
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);
}
}
so when i test it with the intended circuit from here
by default (no resistor) it returns around 3900 and when i attach a 330 resistor i get 3500
so overall can applying that is in the firts part damage the analog pins functionality and if not what couldve happend and if i can fix it
i hope that covers everything if i am forgetting something let me know
thank you
ps i am aware of other questions with returning wrong values but they do not answer any of my questions so please be accommodating

Related

Arduino timer not counting accuratly

I am working on a school project for which I rotate a servo after a cable disconnects and a specific delay is over. This is my current code. We are using an Arduino Uno powered from the USB port
#include <Servo.h>
int reader=4;
int servo1Pin=8;
Servo servo1;
int value;
int pos=10;
int wacht=5000;
void setup() {
pinMode(reader, INPUT);
servo1.attach(servo1Pin);
}
void loop() {
value = digitalRead(reader);
servo1.write(pos);
if (value == LOW) {
delay(wacht);
pos=180;
}
else {
pos=10;
}
}
wacht is the specific delay. When we disconnected pin 4 to break that circuit we don't have a consistent time between the interruption of the power flow and the opening of the servo. It seems to vary from anywhere between 5 to 40 seconds of delay after triggering. Does anyone have any ideas to solve this issue?
try change
pinMode(reader, INPUT);
to
pinMode(reader, INPUT_PULLUP);
And for clarification delay don't use timer.
The order of your instructions seem strange. the usual order is:
Read
Decide
Act
Also, you may want to avoid sending useless instructions to the servo. For example, when you know the servo is already in the correct position. You should not rely on eternal code to do the right thing. In other terms, you have no idea how long servo1.write() takes to execute.
Which would give for your loop()
void loop()
{
if (digitalRead(reader) == LOW)
{
if (pos != 180)
{
delay(wacht); // delay() is only called once, when circuit breaks.
// this guarantees immediate response when circuit
∕/ closes again.
pos = 180;
servo1.write(pos);
}
}
else if (pos != 10)
{
pos = 10;
servo1.write(pos);
}
}
Also, have a look and implement Peter Plesník's answer. This will probably solve some of your problems. You input will definitely still have a random lag of up to 5 seconds when closing the circuit, though.

my serial port did not show my sensor data properly

Hey i got a bit problem with my Arduino and sensor
Here is what i tried ;
#define USE_ARDUINO_INTERRUPTS true // Set-up low-level interrupts for most acurate BPM math.
#include <PulseSensorPlayground.h> // Includes the PulseSensorPlayground Library.
#include <SoftwareSerial.h>
SoftwareSerial blue(0,1);
const int PulseWire = 0; // PulseSensor PURPLE WIRE connected to ANALOG PIN 0
const int LED13 = 13; // The on-board Arduino LED, close to PIN 13.
int Threshold = 550;
PulseSensorPlayground pulseSensor;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
blue.begin(9600);
pulseSensor.analogInput(PulseWire);
pulseSensor.blinkOnPulse(LED13); //auto-magically blink Arduino's LED with heartbeat.
pulseSensor.setThreshold(Threshold);
pulseSensor.begin();
}
void loop() {
// put your main code here, to run repeatedly:
int myBPM = pulseSensor.getBeatsPerMinute();
if(myBPM>200){
myBPM-100;
}
if (pulseSensor.sawStartOfBeat()) {
Serial.println(myBPM);
blue.println(myBPM);
}
delay(10);
}
this code I got from the example library and modified it.
so i want to send data to my android using Bluetooth but this sensor kinda ticked me off because whenever i use it with my HC-06 Bluetooth module it suddenly got a hearth beat without i even touching it and it just sends so much data ignoring the delay I set.
I just need to slowly sending data just like a second but the data didn't show up
so anyone can help?
I read your code and I noticed this piece of code
if(myBPM > 200){ myBPM - 100; }
that is poorly written if (I understand correctly) you want to check the size of myBPM and if it is larger than 200 then it should be subtracted 100.
it should be:
myBPM = myBPM - 100; not myBPM - 100;
I hope my answer will help you. Have a nice day!

How to remove noise from PWM read from a radio receiver?

I am using a Remote Control from FlySky. For my robotics project, I want to read PWM from the receiver on an Arduino. I came across 2 options:
pulseIn() arduino function
ISR(PCINTx_vect) (interrupt)
I cant use the first option of pulseIn() because I want my robot to continue with the operation if receiver signal are not coming (Tx not available etc.) So I used ISR.
Most reliable source : Mr. Brookings channel on YouTube.
Here is what I did (Only the required part for 1 axis):
// [R] where R is defined as 0 => [R] == [0]
volatile long CH[4]; //4 pwms to read so array of 4
float IN[3]={0,0,0}; // throttle is directly written
unsigned long timer[4],curr_time;
byte last[4];
void setup(){
PCICR |= (1 << PCIE0);
PCMSK0 |= (1 << PCINT0);
PCMSK0 |= (1 << PCINT1);
PCMSK0 |= (1 << PCINT2);
PCMSK0 |= (1 << PCINT3);
/* There is some more code here */
Serial.begin(115200);
}
void loop(){
/* There is some more code here */
IN[R] = ((CH[ROLL] - (1500 + R_TRIM))/11.0); // eg.: (1200 - (1500 + 8))/11.0 = -28 (interpreted as setpoint of -28° by the robot)
Serial.println(IN[R]);
}
ISR(PCINT0_vect){
curr_time = micros();
//channel 1 roll
if(PINB & B00000001){
if(last[ROLL] == 0){
last[ROLL] = 1;
timer[ROLL] = curr_time;
}
}
else if(last[ROLL] == 1){
last[ROLL] = 0;
CH[ROLL] = ((curr_time - timer[ROLL]));
}
}
I can read the PWM actually, but the robot keeps showing random twitches in its control at a given set point. I managed to trace the reason and found out that the PWM is insanely ridden by noise. Its not stable like it should be - steady. I have a MATLAB plot I used for analysis:
Signal (IN[R]):
Close up (when Tx stick was in the middle w/o movement) :
There are such spikes coming which is adding up to the control signal eventually making my robot to twitch. I tried some filtering techniques like 'moving average' and '1st and 2nd order exponential filters'. Also checked if it was due to power supplied to it - tried putting a capacitor or an iron core to the power lines but in vain. I can figure out how to remove them as their some constrains :
platform is Arduino Uno (slower in heavy computation)
Control loop shall not go below 100Hz (Currently its at 108Hz exponential filters on 4 axes took it to
~85Hz)
I would appreciate some guidance!
There's no way of telling from this if the input is noisy, or if your code is reading the PWM wrong, of if something else is going on, like external noise on the line, the Arduino's clock jitter, or other interrupts taking time. Also note that micros() on an Arduino Uno only has a resolution of 4µs, not 1µs.
You should check the input for jitter and noise, and try fast code that isn't influenced by other interrupts.
A fairly simple and fast way of getting the PWM pulse width is something like this, preferably without using anything else that uses interrupts:
volatile int pwmPulseWidth = 0;
volatile unsigned long int previousTime = 0;
void setup() {
attachInterrupt(0, rising, RISING);
}
void loop() {
// pwmPulseWidth is available here.
}
void rising() {
attachInterrupt(0, falling, FALLING);
previousTime = micros();
}
void falling() {
attachInterrupt(0, rising, RISING);
pwmPulseWidth = micros() - previousTime;
}
Untested, but it should give you an idea. This will return the width of the PWM pulse.
There are other ways of doing this, of course, like using a timer in capture mode.
Knowing the PWM frequency and the width of the PWM pulse is enough to reconstruct the PWM signal, should you want to.

Simple Analog Read Arduino

I am trying to program some software for my Arduino to read data from an IR fire detector module, I know the module works but I just cannot get the Arduino to read from it and carry out a function correctly. I want it to work like this...
const int IRDetector1Input = A2;
const int IRDetector1Output = A1;
const int LEDButton = 3;
void setup(){
pinMode(IRDetector1Input, INPUT);
pinMode(IRDetector1Output, OUTPUT);
pinMode(LEDButton, OUTPUT);
void loop(){ //Problem Point
if analogRead(IRDetectorInput, HIGH); //This is the problem, the code doesn't match with digital or analog write.
digitalWrite(LEDButton, HIGH);
There are a couple of issues that I see.
One, is that you are using the analogRead() function incorrectly. When you perform an analogRead() the function reads a digital representation of the voltage as seen at the requested pin and returns an integer value (0 to 1023). Also, this function only takes a single parameter, the pin number. You are sending the pin number and HIGH. Here is an example of what to do if you wanted to know what the digital representation of the voltage was at that analog pin:
int analogValue;
analogValue = analogRead(IRDetector1Input);
The second issue is with your if() statement. I believe that you are trying to see if the value received at the pin (where your IR detector is connected) is actually HIGH. First, your if() statement would be more correct (but still wrong), like this:
if (analogRead(IRDetector1Input) == HIGH)
digitalWrite(LEDButton, HIGH);
The reason that it is still wrong is that HIGH is a value that is defined as being the integer number 1, whereas LOW is defined as 0. So, you would be comparing the return value of analogRead() which could be anywhere from 0 to 1023, to the number 1.
So... how can you fix it? Well, it's tough to say without knowing how your IR detector module works. If the detector sends a digital value (a high voltage or a low voltage and not something in between) to your Arduino, use digital functions and a digital pin (i.e. digitalRead()). However, if your IR is actually sending an undetermined voltage, then set an analog threshold and check for it. You will need to run tests to determine where this threshold should be. For example:
#define ANALOG_IR_THRESHOLD 750 /* Arbitrarily set */
if (analogRead(IRDetector1Input) >= ANALOG_IR_THRESHOLD)
{
digitalWrite(LEDButton, HIGH);
}

How to control speed of a Servo motor using arduino Mega

I am working on a project in which I need to change the speed of servo motors. The hardware I am using is an Arduino Mega 2560 board and I am using Servo.h library to control servos. Servo rotates from o to 180 degree. I am using 12 servo motors in the project and have to control them simultaneously. Is there a way?
You can use delay() function in a while or for loop
Example:
Servo s;
s.attach(9);
for(int i=0 ; i<180 ; i++)
{
s.write(i);
delay(10); //10 milisecond
}
If they all are the same degree, try this code below.
At the very top (Not between any "{}"s):
#include <Servo.h>
Servo S1;
Servo S2;
Servo S3;
Servo S4;
Servo S5;
Servo S6;
Servo S7;
Servo S8;
Servo S9;
Servo S10;
Servo S11;
Servo S12;
Put this in Setup:
S1.attach(1);
S2.attach(2);
S3.attach(3);
S4.attach(4);
S5.attach(5);
S6.attach(6);
S7.attach(7);
S8.attach(8);
S9.attach(9);
S10.attach(10);
S11.attach(11);
S12.attach(12);
You need to change the pin numbers.
Just put this anywhere (Not between any "{}"s):
void TurnServos(int toDegree){
servosAt = S1.read;
if(servosAt == toDegree){
}
if(servosAt > toDegree){
while(S1.read > toDegree){
int currentToDegree = S1.read - 1;
S1.write(currentToDegree);
S2.write(currentToDegree);
S3.write(currentToDegree);
S4.write(currentToDegree);
S5.write(currentToDegree);
S6.write(currentToDegree);
S7.write(currentToDegree);
S8.write(currentToDegree);
S9.write(currentToDegree);
S10.write(currentToDegree);
S11.write(currentToDegree);
S12.write(currentToDegree);
delay(10); //Adjust this to make it faster or slower.
}
}
if(servosAt < toDegree){
while(S1.read < toDegree){
int currentToDegree = S1.read + 1;
S1.write(currentToDegree);
S2.write(currentToDegree);
S3.write(currentToDegree);
S4.write(currentToDegree);
S5.write(currentToDegree);
S6.write(currentToDegree);
S7.write(currentToDegree);
S8.write(currentToDegree);
S9.write(currentToDegree);
S10.write(currentToDegree);
S11.write(currentToDegree);
S12.write(currentToDegree);
delay(10); //Adjust this to make it faster or slower.
}
}
}
void ClearServos(){
int startDegree = 90; //Change this number to anything you want.
S1.write(startDegree);
S2.write(startDegree);
S3.write(startDegree);
S4.write(startDegree);
S5.write(startDegree);
S6.write(startDegree);
S7.write(startDegree);
S8.write(startDegree);
S9.write(startDegree);
S10.write(startDegree);
S11.write(startDegree);
S12.write(startDegree);
}
How to use this:
In setup before you do anything with servos but after the part I told you to put in setup, use ClearServos(); to prepare the servos to be used. (This probably isn't necessarily, but I don't know what happens when you use S1.read without changing it and if the servos are at different positions, it will fix problems. It can be avoided if it won't cause problems, but I think you should use it if you can.) All of them will turn to 90 degrees. (90 degrees can be changed with the variable startDegree in void ClearServos.)
To turn them, use TurnServos(90);. 90 is the degree you want it to turn to.
Haven't tested this because I don't have a Mega or 12 servos. Please comment if you notice any errors since this is huge. I spent a lot of time on this so I hope I helped. :)
Maybe you can put some resistors in series to your servo's VCC pin, before your servo motor to decrease the voltage across; thus slowing it. However, this will cause your servo's to be "constant" speed.
An alternative could be to put a transistor in between your servo VCC connection and set PWM on base pin to regulate current (to regulate speed), but that would cost you an extra pin per servo if you're not using a multiplexer in between; and could make your design a little more complicated.
delayMicroseconds(value) closest to 90-null is slowest for 360 servos both pan and carriage on my timelapse rig, shoot move shoot, in time with the mechanical shutter clicker (mini standard servo).
in Servo library WriteMicroseconds(...) function sets servo speed.
for more information please click

Resources