I am having trouble getting a stepper motor to rotate counterclockwise with code written for an Arduino Uno.
This is a simple setup that uses a push button to move the stepper as you hold the button.
Originally the code was written to move the stepper clockwise and I changed the myStepMotor.step to have a -1 instead of 1. From my understanding this should cause the stepper to go in the other direction, but with both a positive or negative number it still rotates in the clockwise direction.
#include <Stepper.h>
const int myStepsPerRev = 64;
char k_ccw;
int myStepsTook = 0;
// allocate a stepper motor object for pins 8 through 11:
Stepper myStepMotor (myStepsPerRev, 8, 9, 10, 11);
void setup() {
pinMode(3, INPUT);
myStepMotor.setSpeed(60); // set the motor speed for 60rpm
myStepsTook = 0; // initialize steps taken to zero
Serial.begin(9600); // initialize the serial port
}
void loop() {
k_ccw = digitalRead(3);
if (k_ccw == LOW) {
delay(125);
myStepsTook++;
Serial.print("Steps Taken: ");
Serial.println(myStepsTook);
myStepMotor.step(-1);
}
}
The pin sequence you want is a bit unexpected; if you connected it in the "standard" way, the 28BYJ-48 should be initialised in software like so:
Stepper myStepMotor(myStepsPerRev, 8, 10, 9, 11);
It should then work properly in both directions.
See here for more information, should you need it.
Related
So i'm making a "turret" type thing using two servo motors, controlled by a joystick. The code i'm running works, however it's very jerky and doesn't move that well, especially in diagonal lines. My code is as follows:
#include <Servo.h>
#define LASER 11
int x = 0;
Servo servo_1; // create servo object to control a servo
Servo servo_2;
// Arduino pin numbers
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
int butt;
int joy_val;
void setup() {
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
servo_1.attach(9);// attaches the servo on pin 9 to the servo object
servo_2.attach(10);
pinMode(LASER, OUTPUT);
digitalWrite(LASER, HIGH);
Serial.begin(9600);
}
void loop() {
joy_val = analogRead(X_pin); // reads the value of joystick (between 0-1023)
joy_val = map(joy_val, 0, 1023, 0, 180); // servo value between 0-180
servo_1.write(joy_val); // sets the servo position according to the joystick value
delay(150);
joy_val = analogRead(Y_pin); // reads the value of joystick (between 0-1023)
joy_val = map(joy_val, 0, 1023, 0, 180); // servo value between 0-180
servo_2.write(joy_val); // sets the servo position according to the joystick
value
delay(150);
delay(15);
butt = digitalRead(SW_pin);
if (butt == LOW){
x = true;
}
if (x == true){
digitalWrite(LASER, LOW);
Serial.print(x);
}
}
I would really appreciate any advice or help, I'm fairly new to arduino :)
Servos are small and light and attempt to move to the position you tell them as quickly as they can. Joysticks can also change values very quickly, and they can also be glitchy. As a result, your servos are constantly making lots of small, quick movements, which can make the turret seem jerky.
I can think of two options, and you might want to do both:
Smooth out the joystick inputs with some low-pass filtering. This generally just means using a weighted average of the current and previous values. The idea is to eliminate a bad reading or two that might happen because of dirty contacts in the potentiometer.
Smooth out the motion. Instead of instantly trying to move the servos directly to the joysticks' current positions, move them toward the target position. On each iteration of the loop, they'll get closer to the target position instead of trying to jump there almost instantaneously.
For #2, there are a couple approaches I like to use.
One is to simply use a weighted average of the servo's current position and the target position. If you move the joystick a fair distance, the turret will swivel quickly but slow down as it approaches the target position.
The other is to use a physical model. Imagine creating a force vector that points from the servos' current position to the joysticks' target position and is proportional to the distance between them. Apply that force to the current point. Also apply a "friction" force that resists the current point's velocity. Numerically integrate the velocity and the position in the loop. If you move the joystick suddenly to a new position, then the turret will accelerate toward it and then slow down as it approaches it. Adjusting the constants used to compute the forces will let you control how "heavy" the mechanism appears to be.
I put a delay proportional to the speed of the servo. Try this (taken from my tutorial: Arduino Servo Motor Basics and Control):
#include <Servo.h>
#include <math.h>
Servo servo_1; // servo controller (multiple can exist)
int servo_pin = 3; // PWM pin for servo control
int joy_pin_x = A0; // pin for x-dir joystick
int joy_pin_y = A1; // pin for y-dir joystick
int offset_x = 0; // subtracting the initial joystick x-location
int offset_y = 0; // subtracting the initial joystick y-location
int pos = 90; // servo starting position aligned with joystick
int prev_deg = 0; // bound for reducing jitter
int x_prev = 0; // bound for reducing jitter
int y_prev = 0; // reducing jitter
void setup() {
servo_1.attach(servo_pin); // start servo control
Serial.begin(9600);
servo_1.write(pos); // move to center (joystick neutral)
Serial.println("Positioned at 90 Degrees");
offset_x = analogRead(joy_pin_x); // initial joystick x-val
offset_y = analogRead(joy_pin_y); // initial joystick y-val
}
void loop() {
int x_val = analogRead(joy_pin_x)-offset_x; // relative joystick x
int y_val = analogRead(joy_pin_y)-offset_y; // relative joystick y
if (abs(x_prev-x_val)<10 and abs(y_prev-y_val)<10){
// reduce jitter
} else {
x_prev = x_val;
y_prev = y_val;
float deg = 180-(int(atan2(x_val,y_val)*(180.0/PI))+90); // angle calc
if (abs(deg-prev_deg)>2 and deg>0 and deg<180){
servo_1.write(deg); // move servo to joystick location
delay(abs(deg-prev_deg)*(10.0/6.0));
prev_deg = deg;
Serial.println(deg); // print out degree
}
}
}
Notice the delay that is functionally dependent on the angle it's moving to - this will 'smooth' the servo and reduce jitter (though not completely remove it).
I have a set of leds that are setup every other led reversed so when I apply power one way light 1,3,5... light. Change power and 2,4,6... I'm trying to set the brightness using PWM on the digital pins. Here's my code:
unsigned long flashCount = 0;
bool bSwitch = true;
void setup()
{
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
}
void loop()
{
if((flashCount + 1000) < millis())
{
if(bSwitch)
{
analogWrite(6, 0);
analogWrite(7, 1);
bSwitch = false;
}
else
{
analogWrite(7, 0);
analogWrite(6, 1);
bSwitch = true;
}
flashCount = millis();
}
}
If I change analogWrite to 255 instead of 1, it will switch both sets of leds. If I change analogWrite to 127 or less, only one set will light. If I switch the led wires to the pins, the problem switches to the other set of lights.
The leds are like so:
GPIO pin 6 --------.-LED+.---.-LED+.---.-LED+.---.-LED+.---|
GPIO pin 7 ---.+LED-.---.+LED-.---.+LED-.---.+LED-.--------|
Change the connection of the LEDs to pins that both support PWM.
Not all pins support PWM. The analogWrite documentation specifies which pins depending on which board:
On most Arduino boards (those with the ATmega168 or ATmega328P), this function works on pins 3, 5, 6, 9, 10, and 11. On the Arduino Mega, it works on pins 2 - 13 and 44 - 46. Older Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11.
The other factor is that analogWrite(255) and analogWrite(0) will revert to driving the output as a digital output. So writing 255 causes both pins to output (one as a digital output and the other in PWM mode). But writing 1 to 127 only causes the PWM capable pin to change.
From arduino's manpages:
Syntax
analogWrite(pin, value)
Parameters
pin: the pin to write to. Allowed data types: int.
value: the duty cycle: between 0 (always off) and 255 (always on). Allowed data types: int
Using an analogWrite with a value of 1 is essentially near-zero. 255 would be full voltage. You're attempting to use analogWrite() as if it was digitalWrite().
Consider using digital write instead in your code: https://www.arduino.cc/reference/en/language/functions/digital-io/digitalwrite/
As for your LED's behavior, it seems like your circuit needs to be debugged as well: Your circuit will only allow current to flow when pin 7 is on. Diodes (Light Emitting Diodes) only allow current in one direction. If you're intending to have the LED's alternate, they should all be oriented with the positives pointing toward their GPIO pin and where they meet they should be grounded with a pull-down resistor.
I am learning Arduino and I was making one program which will print "Hello" to the lcd display connected to arduino.
I dont know what is wrong but the LCD is not showing desired output, rather shows special characters and when I increase/decrease the contrast of lcd using the Potentiometer, the characters changes everytime.
my code is:
// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello");
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis() / 1000);
}
Please help.
Check what kind of lcd you are using.
Some lcd's come with the pin numbering reversed. IE the location of the 16 pins is on the top left corner, but instead of starting with pin number 1, the lcd starts with pin number 16.
Since the format of the LCD pins is the same both way (GND, VCC, input * 14, vcc, Gnd) it does not cause shorting problems, But the control signals are sent to the data pins, and the data signals are sent to the control pins.
I got this hint from the fact that changing the brightness changes the characters. If I am right, changing the contrast via the potentiometer will do you no good with respect to the actual contrast.
Im doing a home project just for fun and Im pretty new to the Arduino, but I do know the basics.
Im creating a automatic venetian blind where it will open when dark and close when bright.
So the slats of the venetian blinds will rotate to a open position when the light sensor detects darkness and rotate to a closed position when the light sensor detects light.
Im using:
arduino uno r3/
continuous servo motor /
led/
LDR (light sensor)/
10k resistor/
This code works for a standard servo motor. I wanted it to work with a continuous servo motor because you can "control it better" and its the one I have.
I guess I will need a if statement something like this:
if light sensor detects dark then rotate the servomotor a to a certain degree and then stop
and if light sensor detects light then rotate the servomotor to a certain degree and then stop.
#include <Servo.h>
Servo servo1;
int sensorPin = A0; // select the input pin for the ldr
int ledPin = 13;
unsigned int sensorValue = 0;
int servoPin = 9;
int pos = 0;
void setup()
{
//Start Serial port
Serial.begin(9600); // start serial for output - for testing
servo1.attach(9);
pinMode(ledPin, OUTPUT);
}
void loop()
{
// For DEBUGGING - Print out our data, uncomment the lines below
Serial.print("Cell = "); // print the value (0 to 1024)
Serial.println(analogRead(sensorPin)); // print carriage return
pos = analogRead(sensorPin);
pos = constrain (pos, 0, 1023);
int servoPos = map(pos, 0, 1023, 255, 0);
int servoDegree = map(servoPos, 255, 0, 0, 179);
servo1.write(servoDegree);
Serial.print("Servo Degree = ");
Serial.println(servoDegree);
int val = analogRead(sensorPin);
val = constrain (val, 0, 1023);
int ledLevel = map(val, 0, 1023, 255, 0);
analogWrite (ledPin, ledLevel);
delay(50);
}
With continuous servo motors, you give up position information. (See this Polou page for details.) This means you won't know when the blinds have reached their open/closed positions unless you also add limit switches. If you go with the switches, then a continuous servo would work. A better solution might be a small gearhead stepper motor like this one from AdaFruit. They have torque but they are much slower than continuous servos.
Key thing is that you don't want to be energizing the motor continuously (which is how standard servos maintain position). That is wasteful and will burn out the motor in something like a blinds application, day in, day out. You want it to do the task and then in loop() wait until the state (light level in your app) has changed. So you would need to keep track of the last light level, then in loop() check if the current light level is different (and greater than some threshold you will have to determine through testing), then change the state of the blinds and store that last level.
I'm trying to display a potentiometer's value on an Adafruit ST7565 GLCD. My serial monitor is giving me values between 1.62-1.67, while the GLCD ranges from -20,000 to +20,000. I'm not sure whether the arithmetic/data type is wrong or whether I am allocating memory improperly for the "sprintf" conversion.
#include "ST7565.h"
#include "stdlib.h"
char buffer[5];
int ledPin = 13; // LED connected to digital pin 13
char str[8];
// the LCD backlight is connected up to a pin so you can turn it on & off
#define BACKLIGHT_LED 10
// pin 9 - Serial data out (SID)
// pin 8 - Serial clock out (SCLK)
// pin 7 - Data/Command select (RS or A0)
// pin 6 - LCD reset (RST)
// pin 5 - LCD chip select (CS)
ST7565 glcd(9, 8, 7, 6, 5);
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
void setup() {
Serial.begin(9600);
// turn on backlight
pinMode(BACKLIGHT_LED, OUTPUT);
digitalWrite(BACKLIGHT_LED, HIGH);
// initialize and set the contrast to 0x18
glcd.begin(0x18);
glcd.display(); // show splashscreen
delay(3000);
glcd.clear();
Serial.println(" ");
digitalWrite(BACKLIGHT_LED, HIGH);
glcd.drawstring(0,0," ");
glcd.display();
glcd.clear();
}
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage);
digitalWrite(BACKLIGHT_LED, HIGH);
Serial.println(voltage);
sprintf(str,"%d",voltage); // converts to decimal base.
glcd.drawstring(0,0,str);
glcd.display();
delay(500);
glcd.clear();
}
Any insight is appreciated. I don't have much formal programming experience, so linking a tutorial about data types won't be of any use. I need to see a specific example like this worked out to truly understand.
You used %d to print out a float; this is undefined behaviour (in your case, it probably dumped out the integer representation of some part of the float's bit sequence).
Instead of using sprintf (since sprintf(..., "%f", val) is reportedly broken on Arduino), use dtostrf:
dtostrf(voltage, 0, 2, buf);
Also, if you're interested, you can see how Arduino prints floats here.