I want my stepper motor to run at a specified speed for a specified time and stop for a set time and then repeat the same cycle.
In fact, the number of times the user has to do this will determine the user
I have the last code that failed, but this is not working properly for more than 30 seconds and the steppper motor rotates permanently.
#include <Stepper.h>
const int stepPin = 6; //PUL -Pulse
const int stepsPerRevolution = 1600;
const int dirPin = 7; //DIR -Direction
const int enPin = 8; //ENA -Enable
int one = 30000;//user input
int c = 2;// user input
int rpm = 1200;//user input
unsigned long t = 0;
Stepper myStepper(stepsPerRevolution, 6, 7);
void setup() {
Serial.begin(9600);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enPin, OUTPUT);
digitalWrite(enPin, LOW);
myStepper.setSpeed(rpm);
}
void loop() {
Cycle();
digitalWrite(enPin, HIGH);
}
void Cycle() {
int cycle = 1;
for (cycle ; cycle <= c; cycle++) {
t = millis();
while ((millis() - t) < one) {
myStepper.step(stepsPerRevolution);//counter clockwise rotation
}
delay(3000);
}
}
int one = 30000;//user input
If you're going to use an int as your timing variable, then you need to look up what is the maximum value that an int can hold on your particular board. If you're using something like an UNO or a Mega then that's 32767 and that will probably explain why it doesn't work with anything over about 32 seconds.
Related
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);
}
}
I have an absolute encoder(Hengstler AD36) that gives 12 bit position value with gray code. I am trying to convert that value to decimal, I mean in degrees 0 to 360.
I have the 12 bit value in gray code by using Arduino.I have read the Arduino port and converted it in Python, but I need to do it in Arduino. But, I couldn't convert it to any meaningful numbers in Arduino.
///Pin setup for the Sensor
const int CLOCK_PIN = 18;
const int DATA_PIN = 19;
const int DATA_PIN2 = 17;
///12-bit sensor
const int BIT_COUNT = 12;
void setup() {
//setup our pins
pinMode(DATA_PIN, INPUT);
pinMode(CLOCK_PIN, OUTPUT);
//give some default values
digitalWrite(CLOCK_PIN, HIGH);
Serial.begin(9600);
}
void loop() {
unsigned long reading1 = readPosition1();
Serial.print(reading1,BIN);
Serial.print(",");
delay(50);
}
//read the current angular position
int readPosition1() {
unsigned long sample1 = shiftIn(DATA_PIN, CLOCK_PIN, BIT_COUNT);
delayMicroseconds(25); // delay for clock
return sample1;
}
//read in a byte of data from the digital input of the board.
unsigned long shiftIn(const int data_pin, const int clock_pin, const int bit_count) {
unsigned long data = 0;
for (int i=0; i<bit_count; i++) {
data <<= 1;
digitalWrite(clock_pin, LOW);
delayMicroseconds(1);
digitalWrite(clock_pin, HIGH);
delayMicroseconds(1);
data |= digitalRead(data_pin);
}
return data;
}
How can I convert data to decimal (0 to 360 degrees)?
The main problem that I couldn't solve, I found some functions that convert gray code to binary, then it is easy to convert it to decimal. However, these functions take input as string or int, and I have no idea about converting data value to int.
Any help would be greatly appreciated.
Thanks.
Newbie here.
I want to delay-off 5 sets of LEDs each set has 1 main push button to turn on the LED and 2 push buttons for increase or decrease its delay-off value.
Power On:
All LEDs off
All timers value min. value is 0
All timers are delay-off
function:
When T1 has pressed the delay off value increments by 10
When button (1) was pressed D1 will on and turns off after delay-off
the value was achieved.
Same functionality with other LEDs.
Consideration:
each sets are independent to each other which means if timer 1 has 1 sec delay-off timer 2 can be set to 3secs.
Panel Board
I search on many libraries, but I'm having difficulty on what to use.
I have my code, and I'm having the problem changing the timer value.
Here's my code--------------------------------------------------------------
#include <Arduino.h>
#include <Timer.h>
const int LED1 = 5;
const int LED2 = 11;
int btn = 6;
int addV = 7; //connect another LED to this pin (don't forget the resistor)
const unsigned long PERIOD1 = 1000; //one second
int unsigned long PERIODsum; //ten seconds
Timer t; //instantiate the timer object
void setup(void)
{
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(btn, INPUT_PULLUP);
pinMode(addV, INPUT_PULLUP);
t.oscillate(LED1, PERIOD1, HIGH);
t.oscillate(LED2, PERIODsum, HIGH);
Serial.begin(9600);
}
int addVf(){
PERIODsum += 10;
return(PERIODsum);
}
void loop(void)
{
int addVon = digitalRead(addV);
if(!addVon){
addVf();
}
int btnS = digitalRead(btn);
if(!btnS){
t.update();
Serial.println("Loop Value: ");
Serial.println(PERIODsum);
}
}
Attached a tiny bit of code which works for 3 channels like you need.
Without any additional libraries needed.
The loop function calls funtions to check buttons and timer value and do then a 50ms delay.
What you have still to add is an press/realease function for start, increase and decrese button press. Else every loop (50ms) the buttons will detected as pressed. What mean the value of settime will increased all time.
//timing defines
#define INC_STEP 20
#define DEC_STEP 20
#define LOOP_DELAY 50
//input/output defines
#define BTN_CH_A_Start 6
#define BTN_CH_A_Inc 7
#define BTN_CH_A_Dec 8
#define LED_PIN_CH_A 9
unsigned int SetTime_CH_A = 0;
unsigned int DelayTimer_CH_A = 0;
#define BTN_CH_B_Start 10
#define BTN_CH_B_Inc 11
#define BTN_CH_B_Dec 12
#define LED_PIN_CH_B 13
unsigned int SetTime_CH_B = 0;
unsigned int DelayTimer_CH_B = 0;
#define BTN_CH_C_Start 14
#define BTN_CH_C_Inc 15
#define BTN_CH_C_Dec 16
#define LED_PIN_CH_C 17
unsigned int SetTime_CH_C = 0;
unsigned int DelayTimer_CH_C = 0;
void setup() {
Serial.begin(9600);
pinMode(BTN_CH_A_Start, INPUT_PULLUP);
pinMode(BTN_CH_A_Inc, INPUT_PULLUP);
pinMode(BTN_CH_A_Dec, INPUT_PULLUP);
pinMode(LED_PIN_CH_A, OUTPUT);
pinMode(BTN_CH_B_Start, INPUT_PULLUP);
pinMode(BTN_CH_B_Inc, INPUT_PULLUP);
pinMode(BTN_CH_B_Dec, INPUT_PULLUP);
pinMode(LED_PIN_CH_B, OUTPUT);
pinMode(BTN_CH_C_Start, INPUT_PULLUP);
pinMode(BTN_CH_C_Inc, INPUT_PULLUP);
pinMode(BTN_CH_C_Dec, INPUT_PULLUP);
pinMode(LED_PIN_CH_C, OUTPUT);
}
void CheckButtons()
{
//check if the delay timer value have to set to inital value
if (digitalRead(BTN_CH_A_Start)) DelayTimer_CH_A = SetTime_CH_A;
if (digitalRead(BTN_CH_B_Start)) DelayTimer_CH_B = SetTime_CH_B;
if (digitalRead(BTN_CH_C_Start)) DelayTimer_CH_C = SetTime_CH_C;
//check if SetTime value have to increase
if (digitalRead(BTN_CH_A_Inc)) SetTime_CH_A += INC_STEP;
if (digitalRead(BTN_CH_B_Inc)) SetTime_CH_B += INC_STEP;
if (digitalRead(BTN_CH_C_Inc)) SetTime_CH_C += INC_STEP;
//check if SetTime value have to decrease
//if button pressed check if decrease if greater 0 decrese by DEC_STEP. if not set to 0
if (digitalRead(BTN_CH_A_Dec)) if (SetTime_CH_A-DEC_STEP>=0) SetTime_CH_A -= DEC_STEP; else SetTime_CH_A=0;
if (digitalRead(BTN_CH_B_Dec)) if (SetTime_CH_B-DEC_STEP>=0) SetTime_CH_B -= DEC_STEP; else SetTime_CH_B=0;
if (digitalRead(BTN_CH_C_Dec)) if (SetTime_CH_C-DEC_STEP>=0) SetTime_CH_C -= DEC_STEP; else SetTime_CH_C=0;
}
void CheckDelayTimer()
{
//check the actual value of DelayTimer decrase if greater than 0 drectese an the output high else set output low
if (DelayTimer_CH_A>0) { DelayTimer_CH_A--; digitalWrite(LED_PIN_CH_A, 1); } else digitalWrite(LED_PIN_CH_A, 0);
if (DelayTimer_CH_B>0) { DelayTimer_CH_B--; digitalWrite(LED_PIN_CH_B, 1); } else digitalWrite(LED_PIN_CH_B, 0);
if (DelayTimer_CH_C>0) { DelayTimer_CH_C--; digitalWrite(LED_PIN_CH_C, 1); } else digitalWrite(LED_PIN_CH_C, 0);
}
void loop() {
CheckButtons();
CheckDelayTimer();
delay(LOOP_DELAY);
}
I'm trying to use an IR sensor with my Arduino Uno and only want a HIGH or LOW signal without decoding making any IR signal turn the state to a 1 or 0. There is also a motion sensor but that code has been removed.
int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int relayPin = 4; //PIN FOR RELAY OPERATION
int irPin = 7; //IR Sensor pin
int lightState = 0;
int irVal = 0;
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
pinMode(relayPin, OUTPUT);
pinMode(irPin, INPUT);
Serial.begin(9600);
}
void loop() {
irVal = digitalRead(irPin);
if (irVal == HIGH) {
lightState = 1;
Serial.println("IR received");
while(irVal == HIGH) {
irVal = digitalRead(irPin);
if(irVal == HIGH) {
irVal = LOW;
} else {
irVal = HIGH;
}
}
}
Are you trying to say that the input is not working correctly? Maybe try INPUT_PULLUP instead of INPUT in the setup loop.
For example:
pinMode(inputPin, INPUT_PULLUP);
Information about this principle you can find here:
https://www.arduino.cc/en/Tutorial/InputPullupSerial
This is my code and everything works fine except that i don't know how to get the melody i've created to loop? Another question is how do i get the LED to flash simultaneously as the melody plays?
#include "pitches.h"
int led = 9;
int melody[] = {
NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4
};
int noteDurations[] = { 4, 8, 8, 4,4,4,4,4 };
void setup() {
pinMode(led, OUTPUT);
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
tone(8, melody[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
}
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000);
}
simply put your code inside a standalone function, and call it from within loop:
#include "pitches.h"
int led = 9;
int melody[] = {
NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4
};
int noteDurations[] = { 4, 8, 8, 4,4,4,4,4 };
void play_melody();
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
// keep the LED on while the melody's playing
play_melody();
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
// pause for one second between each melody iteration (you can remove this for continuous playing)
delay(1000);
}
void play_melody() {
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
tone(8, melody[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
return;
}
If you don't know about function callings, I suggest you to open a C language book like the K&R, and read it, there's a lot to learn for you in it about the basics of C language programming.
在loop()內播放音樂,同時;在Timer中斷程式callback()內控制LED閃爍
例: Timer1.attachInterrupt(callback);
Playing the melody in the loop() function and blinking LED in the timer interrup function at the same time.
ex: Timer1.attachInterrupt(callback);