Arduino rotate servo - arduino

I'm trying to turn a continious rotation servo with an Arduino micro-controller.
I want to turn the servo 1 degree to the right when pushing the right arrow-key with a serial-connection.
This is my code:
const int servoPin = 6;
int incomingByte;
Servo servo;
int pos;
void setup() {
Serial.begin(9600);
pos = 0;
servo.attach(servoPin);
servo.write(pos);
}
void loop() {
incomingByte = Serial.read();
if (incommingByte == 67) {
pos++;
servo.write(pos);
}
}
What do I have to do to make him turning? Because now, it doesn't move...
Thanks a lot!!

There are several things wrong with your code. You have several syntax errors going on.
First, you need to do a #include <Servo.h> and declare incomingByte as an int. You also have a typo in the if-condition line.
Also, you can't read from keyboard if the keyboard isn't connected to the Arduino board, unless you have something in the middle to relay keyboard data to the board. Here's the code you can use to start with:
#include <Servo.h>
int incomingByte;
Servo servo;
int pos;
int dir;
void setup() {
Serial.begin(9600);
Serial.print("Test\n");
pos = 90;
dir = 1;
servo.attach(9);
servo.write(pos);
}
void loop() {
if (pos >= 180 || pos <= 0) { dir = -dir; }
pos += dir;
Serial.print(pos);
Serial.println();
servo.write(pos);
delay(50);
}

Related

How to control the display and non-display of sensor values ​in Arduino using Bluetooth?

I have a sensor that connects to the body and displays muscle signals.
In the setup guide of this sensor, it is said to upload the following code on Arduino, and when we open the Serial Monitor, the sensor values start to be displayed.
Now I want to control the display of these signals using Bluetooth.
So that when I click on the start button in my App, Serial.print() will start working. Also, when I click on the Stop button, the display of these signals and numbers will stop.
Sensor setup guide is this :
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(analogRead(A0));
}
And this is how it works properly :
But when I upload a piece of code that I wrote to my Arduino, it only shows me just on value.
this is my code :
#include <SoftwareSerial.h>
SoftwareSerial BTserial(0, 1); // RX | TX
char Incoming_value = 0;
void setup() {
Serial.begin(9600);
BTserial.begin(9600);
}
void loop() {
Incoming_value = Serial.read(); // "1" is for Start
if (Incoming_value == '1') {
Serial.println(Incoming_value);
StartSensor();
}
}
int StartSensor() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(200);
}
also please tell me How to write StopSensor Function for Stop print Sensor Value.
Try this code first (Without Bluetooth module)
#include <SoftwareSerial.h>
SoftwareSerial BTserial(0, 1); // RX | TX
char Incoming_value = 0;
int state = 0;
void setup() {
Serial.begin(9600);
//BTserial.begin(9600);
}
void loop() {
Incoming_value = Serial.read(); // "1" is for Start
if (Incoming_value == '1') {
state = 1;
}
else if (Incoming_value == '0') {
state = 0;
}
if (state == 1) {
StartSensor();
} else {
Serial.println(0);
}
}
int StartSensor() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(200);
}

Incorporating button into Servo Loop

I am extremely new so please bare with me. I am attempting to control a servo by both the internal timer and by button. Essentially the servo will open a door for 6 seconds every 24 hours or so OR if you press the button enter image description here
The loop for the timer works but the button doesn't. However if i just upload the button code it works fine. Help please! Even better if the servo could go to 180 when the button is held down and return to 0 when released would be ideal.
Here is my code. Tell me where I messed up please!
#include <Servo.h>
Servo myservo;
const int BUTTON_PIN = 8;
const int SERVO_PIN = 9;
int angle = 0; // the current angle of servo motor
int lastButtonState; // the previous state of button
int currentButtonState; // the current state of button
void setup(){
myservo.attach(SERVO_PIN);
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
myservo.write(angle);
currentButtonState = digitalRead(BUTTON_PIN);
}
void loop(){
myservo.write(0);// move servos to center position -> 90°
delay(18000);
myservo.write(180);// move servos to center position -> 120°
delay (6000);
lastButtonState = currentButtonState; // save the last state
currentButtonState = digitalRead(BUTTON_PIN); // read new state
if(lastButtonState == HIGH && currentButtonState == LOW) {
Serial.println("The button is pressed");
// change angle of servo motor
if(angle == 180)
angle = 0;
else
if(angle == 0)
angle = 180;
// control servo motor arccoding to the angle
myservo.write(angle);
}
}
Based on your picture your pins should be 7 and 9. Basically, the only issue with your code is that you can not use delay if you also want to be monitoring something (your button). So instead you use what is called a watchdog timer, where you basically make something happen ever so often based on the system clock, but then also remain available to do other things when that is not happening.
Comparing the blink and blink without delay examples in the arduino example sketch folder may help explain this concept further.
#include <Servo.h>
Servo myservo;
const int BUTTON_PIN = 7;
const int SERVO_PIN = 9;
unsigned long dayTimer_ms = 0;
unsigned long autoOpenDelay_ms = 86400000;
int angle = 0;
void setup(){
myservo.attach(SERVO_PIN);
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
myservo.write(0);
}
void loop() {
if(millis() - dayTimer_ms > autoOpenDelay_ms )
{
dayTimer_ms = millis();
myservo.write(180); //(open?)
delay(6000);
myservo.write(0);
}
if(millis()<dayTimer_ms)//overflow handling (in case this runs for more than 50 days straight)
{
dayTimer_ms = millis();
}
if (!digitalRead(BUTTON_PIN) && angle != 180)
{
angle = 180;
myservo.write(angle);
}
if (digitalRead(BUTTON_PIN) && angle != 0)
{
angle = 0;
myservo.write(angle);
}
}

Wemos D1 reset when setting pins mode

I'm building a little car, remote controlled by a Wemos D1 board, in order to set the WiFi connection and the control logic I'm running this script:
#include <ESP8266WiFi.h>
const char* pass = "**********";
const char* ssid = "**********";
IPAddress ip(192,168,1,91);
IPAddress gat(192,168,1,1);
IPAddress dns(192,168,1,1);
IPAddress sub(255,255,255,0);
WiFiServer s(2000);
int inA1 = 1;
int inA2 = 2;
int enA = 3;
int inB1 = 4;
int inB2 = 5;
int enB = 6;
int trigger = 7;
int echo = 8;
double vSuono = 343; //Unità di misura: m/s
int speed = 255;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
WiFi.config(ip,gat,sub,dns);
WiFi.begin(ssid,pass);
delay(500);
while(WiFi.status() != WL_CONNECTED){
delay(500);
Serial.println(".");
}
Serial.println("Connected!");
delay(30);
s.begin();
Serial.println("Server running!");
delay(30);
//Here starts the problems
pinMode(inA1,OUTPUT);
pinMode(inA2,OUTPUT);
pinMode(enA,OUTPUT);
pinMode(inB1,OUTPUT);
pinMode(inB2,OUTPUT);
pinMode(enB,OUTPUT);
pinMode(trigger,OUTPUT);
pinMode(echo,INPUT);
delay(500);
}
void loop() {
// put your main code here, to run repeatedly:
WiFiClient c = s.available();
delay(30);
if(c){
Serial.println("New client connected!");
delay(3);
while(c.connected()){
if(c.available()){
String command = c.readStringUntil('\n');
if(command == "forward"){
Serial.println("forward");
forward(speed);
}else if(command == "right"){
Serial.println("right");
right(speed);
}else if(command == "left"){
Serial.println("left");
left(speed);
}else{
Serial.println("back");
back(speed);
}
}
delay(30);
}
c.stop();
}
}
void forward(int velocita){
digitalWrite(inA1,HIGH);
digitalWrite(inA2,LOW);
digitalWrite(inB1,HIGH);
digitalWrite(inB2,LOW);
analogWrite(enA,velocita);
analogWrite(enB,velocita);
}
void left(int velocita){
digitalWrite(inA1,HIGH);
digitalWrite(inA2,LOW);
digitalWrite(inB1,LOW);
digitalWrite(inB2,HIGH);
analogWrite(enA,velocita);
analogWrite(enB,velocita);
}
void right(int velocita){
digitalWrite(inA1,LOW);
digitalWrite(inA2,HIGH);
digitalWrite(inB1,HIGH);
digitalWrite(inB2,LOW);
analogWrite(enA,velocita);
analogWrite(enB,velocita);
}
void back(int velocita){
digitalWrite(inA1,LOW);
digitalWrite(inA2,HIGH);
digitalWrite(inB1,LOW);
digitalWrite(inB2,HIGH);
analogWrite(enA,velocita);
analogWrite(enB,velocita);
}
void stop(){
digitalWrite(inA1,LOW);
digitalWrite(inA2,LOW);
digitalWrite(inB1,LOW);
digitalWrite(inB2,LOW);
}
The problem is that when the board execute the pinMode function in the setup() block, the board stop the execution, crash and restart, and I'm not able to ping the board.
If I comment all the portion of the setup() block, with the pinMode calls, the program starts to work but obviously I can't use the pins.
On the serial monitor when the board crash appears this messages:
ets Jan 8 2013,rst cause:4, boot mode:(3,6)
wdt reset
load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v09f0c112
~ld
What could be the problem?
I don't know the pin mapping by heart, but you should stick to the GPIO pins named D1, D2...D8. You've named them 1, 2...8 which are different pins. You likely used a pin which is used by something else (like serial or reset).
int inA1 = D1;
int inA2 = D2;
int enA = D3;
int inB1 = D4;
int inB2 = D5;
int enB = D6;
int trigger = D7;
int echo = D8;
I was getting the same wdt reset error while trying to use pinMode with digitalWrite() function.
I solved it by figuring out how the Wemos Pin mapping works.
Actually, you need to refer the pin in 'Dx' notation.
e.g
digitalWrite(D15,LOW);
or
pinMode(D15, OUTPUT);
Also, make sure to select "Wemos D1 R1" in Tools > Boards so that Dx constants will match
the labels.
Have a look at the conversation here on Arduino FOrum to understand more about wemos pin mapping:
https://forum.arduino.cc/index.php?topic=545113.0

Multiple If statements for Arduino

I'm new to this site and also the wonderful world of Arduino, I have been playing around with a Leonardo board and some Neopixel LEDS ( WS2812B ). I'm currently trying to set predefined colors on the LEDs with a single Pot, but also have an interrupt for a PIR sensor. I'm using the Fastled library since its great but at this point I seem to be stuck on the interrupt part. Any guidance will be appreciated !
#include "FastLED.h"
#define NUM_LEDS 3
#define DATA_PIN 6
#define PIR_PIN 2
int PIR_PIN = 0
// These constants won't change:
const int analogPin = A0; // pin that the sensor is attached to
CRGB leds[NUM_LEDS];
void setup() {
// initialize the LED pin as an output:
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
// initialize serial communications:
pinMode(PIR_PIN, INPUT);
attachInterrupt(0, PIR_PIN, CHANGE);
}
void loop() {
// read the value of the potentiometer:
int analogValue = analogRead(analogPin);
if (analogPin < 1000)
{
leds[0] = CRGB::Red;
FastLED.show();// do Thing A
}
else if (analogPin < 500)
{
leds[0] = CRGB::Orange;
FastLED.show();// do Thing B
}
else if (analogPin < 250)
{
leds[0] = CRGB::Green;
FastLED.show();//do fuckity
}
else
{
leds[0] = CRGB::Purple;
FastLED.show();// do Thing C
}
}
void PIN_PIR () {
buttonState = digitalRead(PIR_PIN);
digitalWrite(DATA_PIN, buttonState);
}

arduino interrupts with servo motor

currently am working on project to open a door with access code using arduino UNO and a servo motor. Normal operation requires entering access code using keypad which is working fine. Another option requires pressing a button that causes an interrupt to rotate the servo motor. My problem is my interrupt only works once and never works again. Plus how do i put the for-loop to rotate the servo motor inside the interrupt function with a delay. I know that is not possible but am calling another function that has the delayMicroseconds but all this is not working. Below is my implementation please help
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Servo.h>
Servo servo;
const int openButtonPin = 2;
void setup() {
// put your setup code here, to run once:
servo.attach(5);
pinMode(openButtonPin, INPUT); //Pin 2 is input
attachInterrupt(0, enforceOpenAccess, HIGH); // PIN 2
}
void(* resetFunc)(void) = 0;
void loop()
{
//My other keypad implementations go here
}
void myDelay(int x) // function to cause delay in the interrupt
{
for(int i = 0; i<x; i++)
{
delayMicroseconds(1000);
}
}
void enforceOpenAccess() // ISR
{
for(int k =0; k<=180; k+=2)
{
servo.write(k); //rotate the servo
myDelay(30); //delay the rotation of the servo
}
}
The code above is run on arduino UNO being simulated in proteus and the interrupt button is a push button. Please if there is other ways of implementing that but with the same behaviour as I have described above help out. Thanks a lot
There are a couple of problems in the slice of code you posted. Just for completeness, you should post the loop function, since we can't guess what you wrote inside.
Just one comment: did you put a pullup? Otherwise use INPUT_PULLUP instead of INPUT for the button pinmode.
The main one is that you attached the interrupt for the HIGH mode, which will trigger the interrupt any time the pin is up, not on the rising edge. And please use the macro digitalPinToInterrupt to map to the correct pin:
attachInterrupt(digitalPinToInterrupt(openButtonPin), enforceOpenAccess, RISING);
Then.. Let's improve the code. You really should use the interrupts only when strictly necessary when you have to respond IMMEDIATELY (= less than a couple of milliseconds) to an input. Here you don't have to, so it's MUCH better to check for the button in the loop (more on turning the motor following)
uint8_t lastState;
void setup()
{
...
lastState = LOW;
}
void loop()
{
uint8_t currentState = digitalRead(openButtonPin);
if ((currentState != lastState) && (currentState == HIGH))
{
// Start turning the motor
}
lastState = currentState;
...
}
This will enable you to properly debounce the button too:
#include <Bounce2.h>
Bounce debouncer = Bounce();
void setup()
{
...
pinMode(openButtonPin, INPUT); //Pin 2 is input
debouncer.attach(openButtonPin);
debouncer.interval(5); // interval in ms
}
void loop()
{
debouncer.update();
if (debouncer.rose())
{
// Start turning the motor
}
...
}
If, on the other way, you REALLY want to use the interrupts (because waiting for a couple of milliseconds is too much for you), you should do something like this:
#include <Bounce2.h>
Bounce debouncer = Bounce();
void setup()
{
...
pinMode(openButtonPin, INPUT);
attachInterrupt(digitalPinToInterrupt(openButtonPin), enforceOpenAccess, RISING);
}
void loop()
{
...
}
void enforceOpenAccess() // ISR
{
// Start turning the motor
}
It looks like your code? No, because now we'll speak about turning the motor
You should NOT use delays to make steps, because otherwise you will wait for 30ms * 180 steps = 5.4s before being able to do anything else.
You can, however, make a sort of reduced state machine. You want your servo to move from 0 to 180 in steps of 1. So let's code the "don't move" state with any value greater than 180, and consequently we can do something like this in the loop:
unsigned long lastServoTime;
uint8_t servoPosition = 255;
const int timeBetweenSteps_in_ms = 30;
void loop()
{
...
if (servoPosition <= 180)
{ // servo should move
if ((millis() - lastServoTime) >= timeBetweenSteps_in_ms)
{
lastServoTime += timeBetweenSteps_in_ms;
servoPosition++;
if (servoPosition <= 180)
servo.write(servoPosition);
}
}
}
Then, using any of the previous examples, instead of // Start turning the motor write
lastServoTime = millis();
servoPosition = 0;
servo.write(servoPosition);
This way you won't block the main loop even when the button is pressed
This is what is in my loop()
char key = keypad.getKey();
if(key)
{
if(j < 10)
{
studentNumber[j] = key;
//holdMaskedNumber[j] = '*';
lcd.setCursor(0,2);
lcd.print(String(studentNumber));
if(j == 9)
{
studentNumber[9] = '\0';
//holdMaskedNumber[9] = 0;
lcd.clear();
//String number = String(studentNumber);
//lcd.print(number);
//delay(1000);
//lcd.clear();
lcd.print("Access Code");
}
j++;
}
else
{
if(i < 5)
{
accessCode[i] = key;
holdMaskedCode[i] = '*';
lcd.setCursor(1,2);
lcd.print(String(holdMaskedCode));
if(i == 4)
{
holdMaskedCode[5] = '\0';
accessCode[5] = '\0';
//lcd.clear();
//lcd.setCursor(0,0);
//accessCodeString = String(accessCode);
//lcd.print(accessCodeString);
//delay(1000);
lcd.clear();
for(int i =0; i<6; i++)
{
lcd.print("Please wait.");
delay(500);
lcd.clear();
lcd.print("Please wait..");
delay(500);
lcd.clear();
lcd.print("Please wait...");
delay(500);
lcd.clear();
}
digitalWrite(4, HIGH);
lcd.print("Access Granted");
for(int k =0; k<=180; k+=2)
{
servo.write(k);
delay(30);
}
resetFunc();
}
i++;
}
}
}

Resources