So I am not the greatest at Arduino but I am learning. I am trying to control 2 Nema 17 stepper motors using 2 identical tb6600 drivers. They are supposed to be controlled by 6 separate buttons. For simplicity, 4 of these buttons are supposed to call a forward loop. The other 2 have other another function of spinning the second motor. When I flash this and plug everything in the 1st stepper just spins constantly. What am I doing wrong here?
#define STEPPIN1 13
#define STEPPIN2 10
#define DIRPIN1 12
#define DIRPIN2 9
#define ENAPIN1 11
#define ENAPIN2 8
int Button = 4;
int Button2 = 5;
int Button3 = 6;
int Button4 = 7;
int Button5 = 3;
int Button6 = 2;
int button_cond1;
int button_cond2;
int button_cond3;
int button_cond4;
int button_cond5;
int button_cond6;
int stepCount = 0;
int steps;
int j;
int i;
void forward(int steps){
int i;
digitalWrite(ENAPIN1,LOW);
digitalWrite(DIRPIN1,HIGH);
for(i=0;i<steps;i++){
digitalWrite(STEPPIN1, !digitalRead(STEPPIN1));
delay(1);
}
digitalWrite(ENAPIN2,HIGH);
}
void forward2(int steps){
int i;
digitalWrite(ENAPIN2,LOW);
digitalWrite(DIRPIN2,HIGH);
for(i=0;i<steps;i++){
digitalWrite(STEPPIN2, !digitalRead(STEPPIN2));
delay(1);
}
digitalWrite(ENAPIN2,HIGH);//disable steppers
}
// void forwardLoop(int loops){
// for(int i=0 ; i < loops ; i++){
// forward(1);
// delay(1);
// }}
void setup() {
pinMode(STEPPIN1,OUTPUT);
pinMode(STEPPIN2,OUTPUT);
pinMode(DIRPIN1,OUTPUT);
pinMode(DIRPIN2,OUTPUT);
pinMode(ENAPIN1,OUTPUT);
pinMode(ENAPIN2,OUTPUT);
pinMode(Button, INPUT_PULLUP);
pinMode(Button2, INPUT_PULLUP);
pinMode(Button3, INPUT_PULLUP);
pinMode(Button4, INPUT_PULLUP);
pinMode(Button5, INPUT_PULLUP);
pinMode(Button6, INPUT_PULLUP);
}
void loop() {
button_cond1 = digitalRead(Button);
button_cond2 = digitalRead(Button2);
button_cond3 = digitalRead(Button3);
button_cond4 = digitalRead(Button4);
button_cond5 = digitalRead(Button5);
button_cond6 = digitalRead(Button6);
if (button_cond1 == HIGH) forwardLoop(1);
else if (button_cond2 == HIGH) forwardLoop(1);
else if (button_cond3 == HIGH) forwardLoop(1);
else if (button_cond4 == HIGH) forwardLoop(1);
else if (button_cond5 == HIGH) forward2(1);
else if (button_cond6 == HIGH) forward2(1);
}
void forwardLoop(int loops){
for(int j=0 ; j < loops ; j++){
forward(1);
delay(10);
}}
//void Load(int loadSteps){
// int i;
// digitalWrite(ENAPIN1,LOW);
// digitalWrite(DIRPIN1,HIGH);
// for(i=0;i<loadSteps;i++){
// digitalWrite(STEPPIN1, !digitalRead(STEPPIN1));
// delay(1);}
//}
//void reverse (int steps){
// int i;
// digitalWrite(ENAPIN1,LOW);
// digitalWrite(DIRPIN1,LOW);
// for(i=0;i<steps;i++){
// digitalWrite(STEPPIN1, !digitalRead(STEPPIN1));
// delay(1);
// }
// digitalWrite(ENAPIN1,HIGH);//disable steppers
// }
//void reverse2 (int steps){
// int i;
// digitalWrite(ENAPIN2,LOW);
// digitalWrite(DIRPIN2,LOW);
// for(i=0;i<steps;i++){
// digitalWrite(STEPPIN2, !digitalRead(STEPPIN2));
// delay(1);
// }
// digitalWrite(ENAPIN2,HIGH);//disable steppers
// }
Given your code, I would expect the motor to spin unless you pressed buttons 1-4 simultaneously. You have each button pin configured with the internal pullup, that implies that the normal state of the pin is HIGH. When you press the button, it connects that pin to ground which makes the state LOW until the button is released.
Try changing your code as follows:
if (button_cond1 == LOW) forwardLoop(1);
else if (button_cond2 == LOW) forwardLoop(1);
else if (button_cond3 == LOW) forwardLoop(1);
else if (button_cond4 == LOW) forwardLoop(1);
Related
i keep getting these Buzzer-CLiCK:33:19: error: a function-definition is not allowed here before '{' token
void circling() {
^
Buzzer-CLiCK:40:17: error: a function-definition is not allowed here before '{' token
void stopc () {
^
exit status 1
a function-definition is not allowed here before '{' token
below is my code
const int BUTTON_PIN = 0; // Arduino pin connected to button's pin
const int BUZZER_PIN = 12; // Arduino pin connected to Buzzer's pin
const int LED_1 = 13;
int point;
int pin[] = {9, 8, 7, 6, 2, 3, 4, 5};
void setup() {
Serial.begin(9600);
for (int i = 0 ; i <= 7; i++) { // initialize serial
pinMode(pin[i], OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
pinMode(BUZZER_PIN, OUTPUT); // set arduino pin to output mode
}
}
void loop() {
int buttonState = digitalRead(BUTTON_PIN); // read new state
if (buttonState == LOW) {
Serial.println("The button is being pressed");
digitalWrite(BUZZER_PIN, HIGH); // turn on
digitalWrite(LED_1, HIGH);
circling();
}
else if (buttonState == HIGH) {
Serial.println("The button is unpressed");
digitalWrite(BUZZER_PIN, LOW); // turn off
digitalWrite(LED_1, LOW);
stopc();
}
void circling() {
for (int i = 0; i <= 7 ; i++) {
digitalWrite(pin[i], HIGH);
digitalWrite(pin[i], LOW);
}
}
void stopc () {
for (int i = 0 ; i <= 7; i++) {
digitalWrite(pin[i], LOW);
}
}
}
I want to implement the function interrupt () but I don't know exactly how..In this case there is 2 for loops which can be seen in the code:I want whenever one of the 2 buttons is pressed the process inside the loop to be interrupted immediately:
void loop() {
int brightButton = digitalRead(K1);
int ldrStatus = analogRead(ldrPin);
if (brightButton == LOW && ldrStatus >= 200)
{
for (int i = 0; i < 10; i++)
{
digitalWrite(greenLed, HIGH);
tone(buzzer,400);
delay(500);
noTone(buzzer);
delay(500);
}
}
else {
digitalWrite(greenLed, LOW);
}
int tempButton = digitalRead(K2);
int valNTC = analogRead(NTC);
if (tempButton == LOW && valNTC > 512)
{
for (int i = 0; i <10; i++)
{
digitalWrite(redLed, HIGH);
tone(buzzer,450);
delay(300);
noTone(buzzer);
delay(1000);
}
}
else {
digitalWrite(redLed, LOW);
}
}
Example code from the Arduino manual:
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}
void loop() {
digitalWrite(ledPin, state);
}
void blink() {
state = !state;
}
Note that this will interrupt the for loop and return to it once the interrupt service routine is finished.
If you want to abort the for loop check the pin state in every loop cycle and break if you want to leave the for loop or return if you want to leave loop().
Of course this is not "immediately".
I am a beginner to using the arduino and I'm stuck at a certain problem. What I have is a programm that prints numbers to the console with the press of a button. What I'm trying to get is to the point that I enter 4 numbers after which it checks if it is the same as an array of numbers that I set before.
Now the problem is that I dont know how to make the programm check if the array I entered with the buttons is the same as the one I wrote before.
int b1 = 4;
int bs1 = 0;
int b2 = 2;
int bs2 = 0;
int b3 = 3;
int bs3 = 0;
int count = 0;
int correctcode[] = {2,3,3,1};
int code[4];
void setup() {
// put your setup code here, to run once:
pinMode(greenPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode (b1, INPUT);
pinMode (b2, INPUT);
pinMode (b3, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
bs1 = digitalRead(b1);
bs2 = digitalRead(b2);
bs3 = digitalRead(b3);
if (bs1 == HIGH) {
count++;
Serial.print ("1");
delay(500);
if (count == 4){
Serial.println ("pincode ingevoerd, checking....");
}
}
if (bs2 == HIGH) {
count++;
Serial.print ("2");
delay(500);
if (count == 4){
Serial.println ("pincode ingevoerd, checking....");
}
}
if (bs3 == HIGH) {
count++;
Serial.print ("3");
delay(500);
if (count == 4){
Serial.println ("pincode ingevoerd, checking....");
}
}
}
// Button pins ( with external pulldown resistors )
const byte b1 = 4;
const byte b2 = 2;
const byte b3 = 3;
// Signal pins
const byte greenPin = 12;
const byte redPin = 13;
byte count = 0; // 0 ... 4 counted button presses
byte correctcode[4] = {2,3,3,1};
byte code[4];
void setup() {
pinMode(greenPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode (b1, INPUT);
pinMode (b2, INPUT);
pinMode (b3, INPUT);
Serial.begin(9600);
}
void loop() {
bool bs1 = digitalRead(b1);
bool bs2 = digitalRead(b2);
bool bs3 = digitalRead(b3);
if (bs1) add(1);
else if (bs2) add(2);
else if (bs3) add(3);
}
void add( byte value ) {
Serial.print (value);
code[count++] = value;
if (count == 4) {
count=0;
if (check()) correct();
else wrong();
}
else
delay(500);
}
bool check() {
for (byte i=0; i < 4; i++) {
if (code[i] != correctcode[i]) {
return false;
}
}
return true;
}
void wrong() {
Serial.println(" -> wrong !");
digitalWrite(redPin, HIGH);
delay(1000);
digitalWrite(redPin, LOW);
}
void correct() {
Serial.println(" -> correct !");
digitalWrite(greenPin, HIGH);
delay(300);
digitalWrite(greenPin, LOW);
}
Compiled, but Untested
Don't duplicate code, but create functions. This can make code easier to read.
If you do not want an autorepeat ( after 500 ms ) for your buttons, better check for state changes (Pressed -> Released)
If multiple buttons are pressed, I added a priority. You might want to discard that state instead.
For more than 3 buttons, read about arrays :)
I am trying to set the timer after which the audio will automatically played once but it is not playing anything just a noise but when i run the audio program separately it work perfectly good
This is my code please help me out where i am doing wrong.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SD.h>
#include <TMRpcm.h>
#include <SPI.h>
int timer1_counter;
#define SD_ChipSelectPin 4
TMRpcm tmrpcm;
unsigned long time_now = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const byte ledPin = 13;
const byte interruptPin1 = 2;
const byte interruptPin2 = 3;
int counter2 = 0;
volatile byte state = LOW;
int count = 0;
int limit = 0;
bool TimerFlag = false;
int deviceTime = 0;
int set = 5;
bool soundplayflag = false;
void setup()
{
lcd.begin();
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(interruptPin1, INPUT_PULLUP);
pinMode(interruptPin2, INPUT_PULLUP);
pinMode(set, INPUT_PULLUP);
lcd.backlight();
lcd.setCursor(1,0);
lcd.print("Please Select:");
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 31250; // compare match register 16MHz/256/2Hz
TCCR1B |= (1 << WGM12); // CTC mode
TCCR1B |= (1 << CS12); // 256 prescaler
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
interrupts(); // enable all interrupts
// initialize timer1
}
ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine
{
if (soundplayflag == true)
{
counter2 = counter2+1;
Serial.println(counter2/2);
}
}
void loop()
{
//unsigned long currentMillis = millis();
int up = digitalRead(interruptPin1);
int down = digitalRead(interruptPin2);
int setbutton = digitalRead(set);
delay(230);
if (up == 0)
{
Serial.println("Entering up");
count++;
if (count >= 0) {
lcdprint(count);
}
}
else if (down == 0)
{
Serial.println("Entering down");
count--;
if (count >= 0) {
lcdprint(count);
}
}
else if (setbutton == 0)
{
Serial.println("Entering set");
soundplayflag=true;
deviceTime = count;
}
if (deviceTime > 0)
{
if (deviceTime == counter2)
{
soundplayflag=false;
Serial.println("Hello world");
PlaySound();
counter2 = 0;
}
}
}
int lcdprint(int a)
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Please Select:");
lcd.setCursor (7,1);
lcd.print(a);
Serial.println(a);
lcd.setCursor (10,1);
lcd.print("Min");
}// end of lcdprint(int a)
void PlaySound()
{
for (int i = 0; i < 10; i++)
{
tmrpcm.setVolume(5);
tmrpcm.play("3.wav");
delay(1000);
}// end of for loop
}// end of void PlaySound()
my expected output is it should play the sound when i set the time
It seems TMRpcm uses Timer1, which conflicts with your TIMER1_COMPA_vect. That would explain why it's working if you run nothing else.
Maybe try to use #define USE_TIMER2?
I am trying to set up an Arduino Uno plus a RelayShield along with 6 push buttons(standard), and 3 LED's(red, yellow, green). Now what I have code to do is to take in a button push combination that is 6 pushes long(order only matters for what is set in the array). I have everything wired up to a breadboard and all that other jazz. My problem is, none of the LED's are working(I followed detailed instructions from instructables - minus the code), and it doesn't matter how many button pushes there are, only button 3 triggers the relay.... Now, for simplicities sake, I didn't wire up all 6 buttons, and I just shortened the array down to 3 and adjusted accordingly(I didn't want to hook up 6 buttons right now). Can someone verify this code for me? Or tell me what is wrong with it? Thanks in advance!
//button pins
const int button1 = 2;
const int button2 = 3;
const int button3 = 4;
const int button4 = 5;
const int button5 = 6;
const int button6 = 7;
const int grnLed = 9;
const int redLed = 10;
const int yellowLed = 11;
const int openRelay = 8;
// how long is our code, kinda personal
const int codelen = 3;
//pin code values must match button pin values
char PIN[codelen] = {
'1', '2', '3'
};
// attempted combo
char attempt[codelen] = {
'0', '0', '0'
};
// attempt count
int z = 0;
void setup() {
// you've been setup
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(button5, INPUT);
pinMode(button6, INPUT);
pinMode(openRelay, OUTPUT);
// set pullup resistor for buttons
digitalWrite(button1, HIGH);
digitalWrite(button2, HIGH);
digitalWrite(button3, HIGH);
digitalWrite(button4, HIGH);
digitalWrite(button5, HIGH);
digitalWrite(button6, HIGH);
// set openRelay state to open or closed
digitalWrite(openRelay, LOW);
}
void correctPIN()
{
pulseLED(grnLed, 3000);
digitalWrite(openRelay, HIGH);
delay(2000);
digitalWrite(openRelay, LOW);
delay(2000);
z = 0;
}
void incorrectPIN()
{
pulseLED(redLed, 3000);
z = 0;
}
void checkPIN()
{
int correct = 0;
int i;
for (i = 0; i < codelen; i++)
{
if (attempt[i] == PIN[i])
{
correct++;
}
}
if (correct == codelen)
{
correctPIN();
}
else
{
incorrectPIN();
}
for (int zz = 0; zz < codelen; zz++)
{
attempt[zz] = '0';
}
}
void checkButton(int button){
if (digitalRead(button) == LOW)
{
while (digitalRead(button) == LOW) { } // do nothing
//convert int to string for tracking/compare
char buttStr = button + '0';
attempt[z] = buttStr;
z++;
//light up led so we know btn press worked
pulseLED(yellowLed, 500);
}
}
void pulseLED(int ledpin, int msec) {
digitalWrite(ledpin, HIGH);
delay(msec);
digitalWrite(ledpin, LOW);
}
void loop() {
// check buttons
checkButton(button1);
checkButton(button2);
checkButton(button3);
checkButton(button4);
checkButton(button5);
checkButton(button6);
//if number of buttons pressed, z, matches code/pin length then check
if (z >= codelen)
{
checkPIN();
}
}