Arduino servo not working. No matter what I try, it always turns 90 degrees - arduino-uno

I've been working on a problem for a while that I can't seem to solve. I have four servos connected to an arduino and if I indicate in the code that it should run, it works fine. But the servo doesn't seem to want to rotate 360 degrees. A negative or positive value does seem to affect the position, but it never rotates more than 90 degrees. Does anyone see the problem?
#include <Servo.h>
Servo servo1; // create servo object to control a servo
Servo servo2; // create servo object to control a servo
Servo servo3; // create servo object to control a servo
Servo servo4; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup() {
servo1.attach(11); // attaches the servo on a pin to the servo object
servo2.attach(10); // attaches the servo on a pin to the servo object
servo3.attach(9); // attaches the servo on a pin to the servo object
servo4.attach(6); // attaches the servo on a pin to the servo object
pos = 90;
resetServo();
pos = 270;
resetServo();
}
void loop() {
}
void resetServo(){
delay(3000);
servo1.write(pos);
delay(500);
servo2.write(pos);
delay(500);
servo3.write(pos);
delay(500);
servo4.write(pos);
delay(500);
}
I already tried to change the rotating value but nothing is working...

Related

How do I animate a line from my draw() function?

I'm using Processing to create a learning experience project that allows users to join network components together. I have links using standard lines, but I want to be able to show a signal moving through the line if there is a valid connection. Think of the line as a network cable for example. Is there anyway I can animate this line?
void draw(){
pushStyle();
stroke(0);
line(from.x, from.y, to.x, to.y);
popStyle();
}
} //draw function in the 'link' file
Of course you can, but your question is a little broad. You do have a particular type of animation in mind? Endless possibilities ;)
The basic way to handle something like this in processing is to increase some animation-variables every frame (or use time management - though that is beyond the basics).
Because the animation-variables (for instance position or color) are changed every frame, the animation is different every frame. It's animated.
Below I give an example of a small green line traveling over a black 'connection' line. If you read through the code I think you'll figure it out. This should be incorporated in a nice 'connection' class for ease of use on a larger scale.
//set coordinates for connection-line
int fromX = 100;
int toX = 600;
int fromY = 100;
int toY = 200;
//copy start coordinate for animation
int animx = fromX;
int animy = fromY;
//determine animation stepsize
int stepX = (toX-fromX)/10;
int stepY = (toY-fromY)/10;
void setup() {
size(800, 300);
//set framerate so animation is not to fast
frameRate(5);
//draw thick lines
strokeWeight(10);
}
void draw() {
background(255);
// draw connection in black
stroke(0);
line(fromX, fromY, toX, toY);
//draw animation in green
stroke(0, 255, 0);
line(animx, animy, animx+stepX, animy+stepY);
// step animation for next frame
animx = animx+stepX;
animy = animy+stepY;
// check for reset (if the animation on the next frame is drawn outside the line)
if (animx+stepX > toX)
{
animx = fromX;
}
if (animy+stepY > toY)
{
animy = fromY;
}
}

Space.Self smooth rotating menu around hand for Oculus Touch. How can I control the speed of the rotation with value for speed?

using UnityEngine;
public class Lerp : MonoBehaviour
{
[Range(0, 360)]
public float angle = 120;//Specify Angle For Rotation
float tempAngle;//Temporary Angle Measurement Variable
bool horizontalFlag;//Check For Horizontal Roation
bool isRotating;//Check Whether Currently Object is Rotating Or Not.
int Direction;//Direction Of Rotation
//Called For Initialization
void Start()
{
horizontalFlag = isRotating = false;
}
//Method For Horizontal Input
void CheckForHorizontalInput()
{
if (Input.GetAxis("Horizontal") != 0 && !isRotating)
{
isRotating = true;
Direction = (Input.GetAxis("Horizontal") < 0 ? - 1 : 1);
horizontalFlag = true;
tempAngle = 0;
}
}
//Method For horizontal Rotation
void HorizontalRotation()
{
transform.Rotate(Vector3.back * angle * Time.fixedDeltaTime * Direction, Space.Self);
tempAngle += angle * Time.fixedDeltaTime;
if (tempAngle >= angle)
{
tempAngle = 0;
isRotating = false;
horizontalFlag = false;
}
}
void Update()
{
CheckForHorizontalInput();
if (horizontalFlag)
HorizontalRotation();
}
}
I want to be able to control the speed of the rotation of the object in seconds.
now the menu is 3 pages and it is rotating with 120 degrees smoothly and stops on 120 degrees. Also if the button pressed is hold the rotation continues and stops on the same range of 120 degrees that the menu is in. if I move the joystick left the menu rotates left if I move the joystick to the right the menu rotates to the right.
Can you tell me how to do this?

changing center of rotation for a shape in processing

I am trying to rotate a vector I have created in illustrator using processing. I would like it to rotate this vector from it's center so it appears to be spinning as oppose to moving around an invisible point. Below is my attempt:
PShape WOE;
void setup(){
WOE = loadShape("WOE.svg");
size (500, 500);
smooth();
}
void draw(){
background(20);
shape(WOE, width/2, height/2, WOE.width, WOE.height); //draw shape in "center" of canvas (250, 250)
translate(-WOE.width/2, -WOE.height/2); //move shape so that center of shape is aligned with 250, 250
WOE.rotate(0.01);
}
From a strictly logical point of view this should work, however this results in the vector rotating around the center of the canvas, but approximately 100px away. I have tried using shapeMode(CENTER); but this unfortunately causes no improvement. Hope someone can help, thanks!
For Reference
Here is WOE.svg: https://www.dropbox.com/s/jp02yyfcrrnep93/WOE.svg?dl=0
I think part of your problem is that you're mixing rotating the shape and translating the entire sketch. I would try to stick with one or the other: either translate and rotate the entire sketch, or only translate and rotate the shape.
That being said, I'm not surprised this gave you trouble.
I would expect this to work:
PShape WOE;
void setup() {
size (500, 500);
WOE = loadShape("WOE.svg");
WOE.translate(-WOE.width/2, -WOE.height/2);
}
void draw() {
background(20);
WOE.rotate(.01);
shape(WOE, width/2, height/2);
}
However, this exhibits the same off-center behavior you're noticing. But if I switch to the P2D renderer, it works fine!
size (500, 500, P2D);
Now the shape is centered in the window and rotates around the shape's center. The difference between renderers seems buggy, but I can't find an open bug on GitHub. Edit: I found this SO question, which lead to this GitHub issue.
In any case, it might be easier to rotate the entire sketch instead of the shape:
PShape WOE;
float rotation = 0;
void setup() {
size (500, 500);
WOE = loadShape("WOE.svg");
shapeMode(CENTER);
}
void draw() {
background(20);
translate(width/2, height/2);
rotate(rotation);
shape(WOE);
rotation += .01;
}
This code works by translating the entire sketch to the center of the window, then rotating the entire sketch, then drawing the shape. Think of this like moving the camera instead of moving the shape. If you have other stuff to draw, then you can use the pushMatrix() and popMatrix() functions to isolate the transformation. This works the same in the default renderer and the P2D renderer.

Processing or Java: get Window Position?

I'm writing a sketch in Processing and I'm curious how I can get the position of the OS's window that the sketch lives in. If I use getPosition() (part of java.awt) I only get the position of the viewport within the window.
You can use the getLocationOnScreen() inherited from java.awt.Component, but you need to make sure the applet isShowing() first:
void draw(){
if(frame.isShowing()) println(frame.getLocationOnScreen());
}
or slightly more graphical:
void draw(){
if(frame.isShowing()) {
java.awt.Point pt = frame.getLocationOnScreen();
background(255);
rectMode(CENTER);
rect(map(pt.x,0,displayWidth,0,width),//use screenWidth instead of displayWidth in Processing 1.5.1 or older
map(pt.y,0,displayHeight,0,height),//use screenHeight instead of displayHeight in Processing 1.5.1 or older
10,10);
}
}
where
Frame frame = ( (PSurfaceAWT.SmoothCanvas) ((PSurfaceAWT)surface).getNative()).getFrame();
(For other renderers such as P2D or FX2D check out this answer)

draw simple rectangle in MFC Dialog-Based

I wrote this code to draw a simple rectangle in a dialog , I also added ON_WM_PAINT() to my message map. but it didnt show anything on dialog to me ! I really appreciate it if anyone could tell my mistakes in code:
void Ctest4Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = 2;
int y = 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
//I want to draw a rectangle
dc.Rectangle(10,10,50,50);
}
else
{
CDialogEx::OnPaint();
}
}
Looks like your paint code only runs when the window is iconic? Why are you doing that?
Put it in the else block, after the call to CDialogEx::OnPaint().
Your first and biggest mistake is trying to draw directly in a dialog. While it is possible to do so, it's almost always a bad idea. A dialog should usually be treated as a container for controls.

Resources