Why doesn't my servo control work? - arduino

Basically I'm trying to control a servo with two push buttons (one for forward and one for backward). However, my code doesn't work and I am not sure why. Essentially I used the Sweep and Button examples to make this code. However, it doesn't seem to be working unless something is wrong with my hookup.
#include <Servo.h>
Servo servoOne;
int servoOnePos = 0;
const int buttonUpPin = 13;
const int buttonDownPin = 12;
int buttonUpState = 0;
int buttonDownState = 0;
void setup() {
servoOne.attach(11);
pinMode(buttonUpPin, INPUT);
pinMode(buttonDownPin, INPUT);
}
void loop() {
buttonUpState = digitalRead(buttonUpPin);
buttonDownState = digitalRead(buttonDownPin);
if (buttonUpState == HIGH) {
for (servoOnePos < 180; servoOnePos += 1;) {
servoOne.write(servoOnePos);
delay(15);
}
} else if (buttonDownState == HIGH) {
for (servoOnePos <= 180; servoOnePos = servoOnePos - 1;) {
servoOne.write(servoOnePos);
delay(15);
}
}
}

#include <Servo.h>
Servo servoOne;
int pos = 90;
const int maxDeg = 160;
const int minDeg = 5;
const int buttonUpPin = 13;
const int buttonDownPin = 12;
const int leftPin = 3;
const int rightPin = 2;
int leftPressed = 0;
int rightPressed = 0;
void setup()
{
servoOne.attach(11);
pinMode(buttonUpPin, INPUT);
pinMode(buttonDownPin, INPUT);
}
void loop()
{
leftPressed = digitalRead(leftPin);
rightPressed = digitalRead(rightPin);
if(leftPressed){
if(pos < maxDeg) {
pos += 3;
}
servoOne.write(pos);
}
if(rightPressed){
if(pos > minDeg) {
pos -= 3;
}
servoOne.write(pos);
}
delay(15);
}

Related

why my audio sound is not playing in my arduino code but if i play it separately it started working good

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?

Arduino Combination Button?

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();
}
}

delay() line not allowing my Arduino code to run

The code is below
// initialize lights and sensors and valueHolders
int redN = 13;
int yellowN = 12;
int greenN = 11;
const int sensorN = A0;
int analogValueN = 0;
int redW = 10;
int yellowW = 9;
int greenW = 8;
const int sensorW = A1;
int analogValueW = 0;
int redS = 7;
int yellowS = 6;
int greenS = 5;
const int sensorS = A2;
int analogValueS = 0;
int redE = 4;
int yellowE = 3;
int greenE = 2;
const int sensorE = A3;
int analogValueE = 0;
//set thresholdValue;
const int threshValue = 200;
//initialize booleanSet for sectors
boolean bSet[] = {false, false, false, false};
void setBSet(){
if(analogValueN > threshValue) bSet[0] = false;
if(analogValueN < threshValue) bSet[0] = true;
if(analogValueW > threshValue) bSet[1] = false;
if(analogValueW < threshValue) bSet[1] = true;
if(analogValueS > threshValue) bSet[2] = false;
if(analogValueS < threshValue) bSet[2] = true;
if(analogValueE > threshValue) bSet[3] = false;
if(analogValueE < threshValue) bSet[3] = true;
// if (analogValueN > threshValue){
// bSet[0] = false;
// } else {
// bSet[0] = true;
// }
}
//setup outputs
void start(){
pinMode(redN, OUTPUT);
pinMode(yellowN, OUTPUT);
pinMode(greenN, OUTPUT);
analogValueN = analogRead(sensorN);
pinMode(redW, OUTPUT );
pinMode(yellowW, OUTPUT);
pinMode(greenW, OUTPUT);
analogValueW = analogRead(sensorW);
pinMode(redS, OUTPUT);
pinMode(yellowS, OUTPUT);
pinMode(greenS, OUTPUT);
analogValueS = analogRead(sensorS);
pinMode(redE, OUTPUT );
pinMode(yellowE, OUTPUT);
pinMode(greenE, OUTPUT);
analogValueE = analogRead(sensorE);
}
// set method for active sector
// ROS ==> red of sector
void active(int ROS){
//set sector as go
digitalWrite(ROS, LOW);
digitalWrite(ROS-1, LOW);
digitalWrite(ROS-2, HIGH);
Serial.println('Pin' + (ROS-2) + "Active High");
}
//set method for light action
void action(int ROS){
//set active for sector...
active(ROS);
//...and its complimentary sector
if (ROS>8){
active(ROS - 6);
} else {
active(ROS + 6);
}
}
void runTraffic(){
setBSet();
for(int i=0;i<4;i++){
if (bSet[i]){
action((3*i)+4);
// delay(8000);
}
}
}
void setup() {
// put your setup code here, to run once:
start();
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
runTraffic();
}
When i try to compile, it fails and error message is just "Error Compiling" and no reference to which line the error is coming from. But upon debugging i found the error to be in the delay line of the runTraffic() method. When i comment the line, the code compiles but it doesn't otherwise. But the delay works fine on other codes of mine.
What might be wrong with it?
There's lot of people have same problems with same IDE (probably that version is unstable).
I used the 1.6.4 to compile and no problem appear.
My advice is to unistall your Arduino IDE version (1.6.3) and install the 1.6.4 or something older and more stable.
Here you can download it and try which IDE works for you.
For now, i don't suggest the 1.6.5 (I've never tried it) but it could be a good candidate.

Digital Clock C code / PIC, Protues

I'm taking a Microcontroller course this semester, and I have an assignment to make a digital clock using PIC18 and display it on LCD. My code is in C and I'm protues to simulate.
I wrote a code but something is wrong if anyone can help me figure out my mistake..
Thank you
#include <P18F4580.h>
#define ldata PORTD
#define rs PORTBbits.RB0
#define rw PORTBbits.RB1
#define en PORTBbits.RB2
void msdelay(unsigned int itime)
{ unsigned int i,j;
for (i=0; i<itime; i++)
for (j=0; j<135; j++);
}
void lcdcmd(unsigned char value)
{ ldata = value;
rs = 0;
rw = 0;
en = 1;
msdelay(1);
en = 0;
}
void lcddata(unsigned char value)
{ ldata = value;
rs = 1;
rw = 0;
en = 1;
msdelay(1);
en = 0;
}
void main()
{
TRISD = 0;
TRISB = 0;
en = 0;
int msCounter =0;
int secCounter =0;
int minCounter =0;
int hrCounter =0;
msdelay(15);
lcdcmd(0x01); //Clear display
msdelay(15);
lcdcmd(0x02); //Home cursor
msdelay(15);
lcdcmd(0x06); //Left to right, still
msdelay(250);
lcdcmd(0x0E); //display cursor
msdelay(250);
lcdcmd(0x3C); //5x10 2 lines
msdelay(15);
lcdcmd(0x86);
while(1)
{
msdelay(15);
lcdcmd(0x08);
lcddata(secCounter);
msdelay(15);
msCounter++;
if (msCounter==1000)
{secCounter++; msCounter=0; }
if (secCounter==60)
{minCounter++; secCounter=0; }
if (minCounter==60)
{hrCounter++; minCounter=0; }
if (hrCounter==24)
{hrCounter=0; }
msdelay(15);
lcddata(hrCounter);
msdelay(15);
lcddata(':');
msdelay(15);
lcddata(minCounter);
msdelay(15);
lcddata(':');
msdelay(15);
lcddata(secCounter);
}
}
Its better to display digital clock using 7-segment display than in LCD.
use this code in your program and check...
for lcddata(mscounter);
use *thous(mscounter);*
declare it in the code and check
lcdcmd(0x08); instead use
lcdcmd(0x80);
void thous(unsigned int count)
{
lcddata((count/1000)+0x30);
lcddata(((count/100)%10)+0x30);
lcddata(((count%100)/10)+0x30);
lcddata((count%10)+0x30);
}

Arduino memory game

This game has 4 leds and 4 buttons. the game is turning RANDOMLY those leds ON and OFF.
The player should be able to push the right button whenever he sees one led to be turned ON.
The leds should be turning ON and OFF with incerasing speed, so reaction time of a player is shorter and shorter
I have this code but i just know how to add more leds and buttons.
const int BUTTON1 = A0;
const int BUTTON2 = A1;
const int BUTTON3 = A2;
const int BUTTON4 = A3;
int LED1 = 2;
int LED2 = 3;
int LED3 = 4;
int LED4 = 5;
int ran;
int right = 0;
int ledOrder[9];
int guessOrder[9];
void setup()
{
Serial.begin(9600);
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);
pinMode(LED3,OUTPUT);
pinMode(LED4,OUTPUT);
pinMode(BUTTON1,INPUT);
pinMode(BUTTON2,INPUT);
pinMode(BUTTON3,INPUT);
pinMode(BUTTON4,INPUT);
}
void randomLed() {
for (int i = 0; i < 9; i++) {
ran = random(1,20);
if (ran < 11) {
digitalWrite(LED1,HIGH);
delay(500);
digitalWrite(LED1,LOW);
ledOrder[i] = 1;
}
else {
digitalWrite(LED2,HIGH);
delay(500);
digitalWrite(LED2,LOW);
ledOrder[i] = 2;
}
delay(500);
}
}
void btnClick() {
int ans = 0;
while (ans < 9) {
if (digitalRead(BUTTON1) == HIGH) {
guessOrder[ans] = 1;
ans++;
while (digitalRead(BUTTON1) == HIGH) {
}
}
else if (digitalRead(BUTTON2) == HIGH) {
guessOrder[ans] = 2;
ans++;
while (digitalRead(BUTTON2) == HIGH) {
}
}
}
}
void loop() {
Serial.print("Press button1 to start \n");
while (digitalRead(BUTTON1) == LOW) {
}
randomLed();
btnClick();
for (int i = 0; i < 9; i = i + 1) {
Serial.print("Guess: ");
Serial.print(guessOrder[i]);
Serial.print(" Answer: ");
Serial.print(ledOrder[i]);
if (guessOrder[i] == ledOrder[i]) {
Serial.print(" Right");
right++;
} else {
Serial.print(" Wrong");
}
Serial.print("\n ");
}
Serial.print(right);
Serial.print("/9\n");
delay(2000);
}
If I understand your question correctly, you would like to increase the led blink rate(when the LED's come on) each time the user gets the previous game right. If this is true you should try to put loop in for/if the user got the the previous one(game) right it will increase blink rate by a simple increment( I++;). Comment if you feel this is clarified enough or if your asking for something else

Resources