Clashing concurrent Arduino devices: camera and telescope drives - concurrency

I have an Arduino driven telescope mount based on Kevin Ferrare's device here:
https://hackaday.io/project/4386-arduino-st4-telescope-control
Which seems to work nicely.
I have added a DSLR remote shutter control using a similar optocoupler design, which also works nicely on its own.
However, when I run them together, any movement of the mount motor while the shutter is open closes the shutter.
I have completely stripped down the code to this:
// This file is part of Arduino ST4.
//
//
void setup()
{
// Flash pins on startup
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(7, OUTPUT);
digitalWrite(2, HIGH); // Red
digitalWrite(3, HIGH); // Blue
digitalWrite(4, HIGH); // Yellow
digitalWrite(5, HIGH); //Green
digitalWrite(7, HIGH);
}
void loop()
{
}
With the result that only three pins illuminate 2, 4 and 5. Otherwise it's fine.
Is there something about concurrent pins that I need to know, or do I have a wiring problem?
Thanks for any input.
Steve

Related

Arduino Traffic Lights and LCD

I have created a simple traffic lights situation with an LCD screen. The countdown time is set as 60 seconds in "Adruino for LCD", and I wish the traffic light runs normally at 59 to 11 seconds when it is counting down. Then, I hope all traffic lights turn to red from green when it shows the countdown at 10 to 0 second.
Here is the screenshot of my Arduino circuit.
Original code for Traffic Lights:
void loop() {
// road - 1
// green signal for the first road
digitalWrite(g1, HIGH);
digitalWrite(r2, HIGH);
digitalWrite(r3, HIGH);
digitalWrite(r4, HIGH);
// delay time for red signal - 9 seconds
delay(10000);
// cosing first road
digitalWrite(g1, LOW);
// yello signal for first and scond road (3 seconds)
digitalWrite(r2, LOW);
digitalWrite(y1, HIGH);
digitalWrite(y2, HIGH);
delay(3000);
// green signal for second road and disable yellow signals
digitalWrite(y1, LOW);
digitalWrite(y2, LOW);
// road - 2
...
// road - 3
...
// road - 4
...
}
I tried to add for loop for traffic lights.
for (int seconds = 60; seconds >= 11; seconds--)
{
//road 1
digitalWrite(g1, HIGH);
:
:
//road 4
if(seconds==11){
for (int j = 10; j >= 0; j--)
{
//road 1
digitalWrite(r1, HIGH);
:
:
//road 4
}
}
}
but it doesn't for the second condition, as I want all the traffic lights should turn to red when it counts from 10 to 0.

How do I use rotary encoders to control an HID device?

My goal is to create a small switch panel for my Windows 10 PC. I'm using an Arduino Micro to run my program. I want to use a digital rotary encoder to control Joystick inputs. I want to control volume by pressing volume up when I turn the encoder clockwise, and volume down when I turn it counterclockwise
#include <Joystick.h>
#define outputA 2
#define outputB 3
int counter = 0;
int aState;
int aLastState;
void setup() {
Joystick.begin();
pinMode(2, INPUT);
pinMode(3, INPUT);
}
void loop() {
aState = digitalRead(outputA);
if (aState != aLastState) {
if (digitalRead(outputB) != aState) { // Clockwise
Joystick.pressButton(1);
delay(10);
Joystick.releaseButton(1);
counter ++;
} else { // Counterclockwise
Joystick.pressButton(2);
delay(10);
Joystick.releaseButton(2);
counter --;
}
Serial.print("Counter: ");
Serial.println(counter);
}
aLastState = aState;
}
This should press a button (button 1) every time the pins are pushed clockwise, and press button 2 when they are moving counterclockwise. I have tried to make the counter value print out to the serial monitor to see what it was doing, but the monitor changes at random moments and doesn't recognize the rotary encoder. I can clarify any points of this, but I was wondering what I could fix.
It turns out that this was just an interference issue. Using pullup resistors fixed the problem.

Arduino Uno + servo motor (SG-90) + ultrasonic sensor(HC-S04) detects an obstacle

I'm a newbie to Arduino. The goal is to write a program that can stop a hypothetical conveyor belt (read a servo motor) if too many objects are stacked on a belt, that is detected by HC-S04 sensor. If the the distance exceeds the minimum threshold value (1000), then the motor can freely spin. If not, it should stop. With the code below, I get a servo motor to spin regardless of whether an object is in front of it or not.
Tried taking out and changing the delay. It just was spinning faster or slower, but still ignored obstacle distance. Tried commenting sensorValue = map(sensorValue, 0, 255, 0, 4000); but it didn't affect the result.
#include <Servo.h>
#include <Wire.h>
Servo servo;
const int trigPin= 9;
const int echoPin= 10;
const int threshold = 1000;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin,INPUT);
Serial.begin(9600);
servo.attach(8);
}
void loop() {
int sensorValue = digitalRead(echoPin);
sensorValue = map(sensorValue, 0, 255, 0, 4000);
if (sensorValue >= threshold) {
digitalWrite(8,HIGH);
delay(10);
}
else {
digitalWrite(8,LOW);
delay(10);
}
}
Debug by outputting your sensorValue contents - this will allow you to check which branch is triggered.
(can't post as a comment because my rep is not high enough).
You can use the debugging values to check, configure and calibrate your sensor.

On and Off Button on Arduino

I'm back with another non-homework related question. I'm playing with an arduino with my brother, and we're trying to attach a button so that when its pressed, his sensor stays on and does what it has to do. When its pressed again it'll do nothing and turn off. Right now when the button is held down it stays on, but when its unpressed it stays off. We're trying to make something that'll continuously take pictures when its thrown off a building. It'll stop with the ultra-sonic sensor reads <= 5. I can't throw myself off the roof top while holding the button XD It's a 2 pin button. Here's the code of what we have right now:
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int ledPin = 13;
const int buttonPin = 2;
// defines variables
long duration;
int distance;
int safetyDistance;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
pinMode(echoPin, INPUT);// Sets the echoPin as an Input
pinMode(buttonPin, INPUT);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
myservo.attach(8); // attaches the servo on pin 9 to the servo object
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(buttonPin, HIGH);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
safetyDistance = distance;
if (digitalRead(buttonPin) == HIGH)
{
if (safetyDistance <= 5){
digitalWrite(buzzer, HIGH);
digitalWrite(ledPin, HIGH);
}
else{
digitalWrite(buzzer, LOW);
digitalWrite(ledPin, LOW);
for(pos = 0; pos <= 180; pos += 20) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(10); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=0; pos-=2) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(10); // waits 15ms for the servo to reach the position
}
}
}
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}
Now, what we're having trouble with is, I understand that there has to be states for the button. But as we were scouring the internet for help we came across terms like debouncing and we just don't understand what that means. We're so close to finally finishing our mini project. The servo is moving fine, the ultrasonic sensor works well. We just need help figuring out this button. Any advice and help would be appreciated as we're both scratching our heads on this one. Thank you!!
--Zane
Normal buttons, like they are used in most project, are doing nothing else but pressing two electrical contacts together, when it is pressed. But - because it's a mechanical action with limited velocity - the level on the pin doesn't simply rise from LOW to HIGH (or vice versa). Instead it changes it's level in the moment the button gets pressed a few times, until the final level is reached. This variation is to fast for a human to see (for example with an attached LED), but slow enough for a microcontroller like the arduino taking notice. So often you have to make sure, that it's really only one button press, even if the level changes a few times in a row. Mostly it is sufficient to add a small timeout, where the button presses aren't recognized by your code (for example 50 ms), or checking a second time for the state of the button after this period. You can check the corresponding Arduino page for an official example of debouncing.
In your code you are only checking directly the state of the button, which is why it turns off, when you release the button. I would try something like this:
boolean program_state = false;
unsigned long debounce_time = 50;
unsigned long debounce_time_stamp=0;
void loop(){
// ultasound measuring code
if(digitalRead(buttonPin) && debounce_time_stamp - millis() > debounce_time){
program_state=!program_state;
debounce_time_stamp = millis();
}
if(program_state){
// distance checking and servo code
}
}
This check - when the button is pressed - if enough time has been bygone since the last recognized press (the amount of time can be adjusted with the variable debounce_time). When a valid press is recognized, the program_state variable is toggled to change the state between the two modes (On and Off).
Notice that with this the code is unresponsive for the time the servo needs to finish one sequence. If you want to have code, that is more responsive, you should consider using the button as an external interrupt (for this look at the examples on the correspondig Arduino page).

Use Arduino to control a stepping motor and receive the feedback

I want to use Arduino to control a stepper motor and catch the feedback. After my upper computer catch the feedback there is other work to do after the stepper motor has rotated. The code is as below:
int x;
void setup() {
Serial.begin(9600);
pinMode(5,OUTPUT); // Step
pinMode(4,OUTPUT); // Dir
}
void loop() {
digitalWrite(4,LOW);
for(x = 0; x < 3200; x++) // Loop 3200 times
{
digitalWrite(5,HIGH); // Output high
delayMicroseconds(400);
digitalWrite(5,LOW); // Output low
delayMicroseconds(400);
}
Serial.println("ok");
delay(1000);
}
But before the stepper motor rotate, the upper computer has already received the feedback of "ok". How can I improve my code.