Stepper motor with mp3 shield Arduino - arduino

For a college project I want to make a stepper motor combined with a mp3shield.
It is al working fine, but when I put them in the same code, is it acting really weird and my stepper motor starts shaking.
My code is below:
int sensor = A0;
int led1 = A1;
int led2 = A2;
int led3 = A3;
int led4 = A4;
int led5 = A5;
int button = 12;
int dirpin = 10;
int steppin = 11;
int buttonState;
void setup()
{
Serial.begin(9600);
pinMode(sensor, INPUT);
pinMode(button, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(dirpin, OUTPUT);
pinMode(steppin, OUTPUT);
}
void loop()
{
buttonState = digitalRead(button);
Serial.println(buttonState);
delay(10);
if(buttonState == HIGH)
{
rotateDeg(360, .1);
playSong(6);
}
}
The code for letting the stepper motor turn is the following:
void rotateDeg(float deg, float speed){
int dir = (deg > 0)? HIGH:LOW;
digitalWrite(dirpin,dir);
int steps = abs(deg)*(1/0.225);
float usDelay = (1/speed) * 70;
for(int i=0; i < steps; i++){
digitalWrite(steppin, HIGH);
delayMicroseconds(usDelay);
digitalWrite(steppin, LOW);
delayMicroseconds(usDelay);
}
}
The code for playing a song:
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>
SdFat sd;
SFEMP3Shield MP3player;
void playSong(int song){
Serial.begin(9600);
//start the shield
sd.begin(SD_SEL, SPI_HALF_SPEED);
MP3player.begin();
//start playing track 1
MP3player.playTrack(song);
}
I use a driver for the stepper motor with 12V 1000mA input. If I want to play a song without the motor, is works fine.
If I want to rotate the motor without sound, it works fine as well.
Hopefully you are able to help.
Thank you in advance,
NiSh
EDIT:
I have also some LEDs, which work fine. When I press a button there will be a random pattern.
When I put it in the code like:
if(buttonState == HIGH)
{
setLed();
rotateDeg(360, .1);
}
It works like I want that it works. But if I try:
if(buttonState == HIGH)
{
playSound(4);
rotateDeg(360, .1);
}
the motor starts shaking when playing the song, and never makes that 360 degree turn.

Related

Some LED fade and others will flash when all LED should flash Adrunio Nano

I am having trouble with my Arduino code and circuit. The goal is to have each LED fade one after another. This is not happening correctly. Some LED will fade up and down properly and then some will blink instead. I have been trying to troubleshoot and here is what I have done and has not fixed it.
used a different board
Swap LEDs
Used different resistors
Swapped pins that blink to pins that faded and the blink will be fading
Moved circuit to a different breadboard
Check that the code is sending the correct light level through the serial monitor
Here is a picture of my board
Here is the code:
const int BUTTON = 2; // Naming switch button pin
const int LED1 = 3; // Namin LED pin
const int LED2 = 4;
const int LED3 = 5;
const int LED4 = 6;
const int LED5 = 7;
const int LED6 = 8;
const int LED7 =9;
const int LED8 = 10;
const int LED9 = 11;
int BUTTONstate = 0; // A variable to store Button Status / Input
int brightness = 0;
int fadeAmount =5;
void setup(){
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(LED7, OUTPUT);
pinMode(LED8, OUTPUT);
pinMode(LED9, OUTPUT);
pinMode (BUTTON, INPUT);
Serial.begin(9600);
}
void loop() {
BUTTONstate = digitalRead(BUTTON); // Reading button status / input
if (BUTTONstate == HIGH) // Condition to check button input
{
FlashingLight();
}
else
{
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);
digitalWrite(LED8, LOW);
digitalWrite(LED9, LOW);
}
}
void FlashingLight()
{
for ( int i = 0; i<=4;i++){
//digitalWrite(LED, LOW);
fading(LED1); //Fades
fading(LED2); // blinks
fading(LED3); //fades
fading(LED4); //fades
fading(LED5); //blinks
fading(LED6); //blinks
fading(LED7); //fades
fading(LED8); //fades
fading(LED9); //fades
delay(1000);
}
}
void fading(int val) {
//brightness =0;
//digitalWrite(LED, LOW);
analogWrite(val,brightness);
for (brightness = 0; brightness <= 150; brightness += 5) {
analogWrite(val,brightness);
delay(30);
Serial.println(brightness);
}
for (brightness = 150; brightness >= 0; brightness -= 5) {
analogWrite(val,brightness);
delay(30);
Serial.println(brightness);
}
delay(100);
//brightness =0;
}
Thank you for your help and let me know if you have any questions,
According to this Arduino.cc reference not all pins of the Arduino Nano are suitable for PWM control (using analogWrite()). On the Nano, only pins 3, 5, 6, 9, 10, 11 can be used to output a PWM signal.

Using a Arduino Mega instead of a Uno for a capacitive sensor

I was trying to use a Arduino Mega to substitute a Arduino Uno. The folliwing code works fine in Uno but doesn't work on Mega (I already changed the ports to it). It is used for a Capacitive touchsensor:
#define resolution 8
#define mains 60 // 60: north america, japan; 50: most other places
#define refresh 2 * 1000000 / mains
void setup() {
Serial.begin(9600);
// unused pins are fairly insignificant,
// but pulled low to reduce unknown variables
for(int i = 2; i < 14; i++) {
pinMode(i, OUTPUT);
digitalWrite(i, LOW);
}
for(int i =22; i < 53; i++){
pinMode(i, OUTPUT);
digitalWrite(i, LOW);
}
pinMode(8, INPUT);
startTimer();
}
void loop() {
Serial.println(time(8, B00100000), DEC);
}
long time(int pin, byte mask) {
unsigned long count = 0, total = 0;
while(checkTimer() < refresh) {
// pinMode is about 6 times slower than assigning
// DDRB directly, but that pause is important
pinMode(pin, OUTPUT);
PORTH = 0;
pinMode(pin, INPUT);
while((PINH & mask) == 0)
count++;
total++;
}
startTimer();
return (count << resolution) / total;
}
extern volatile unsigned long timer0_overflow_count;
void startTimer() {
timer0_overflow_count = 0;
TCNT0 = 0;
}
unsigned long checkTimer() {
return ((timer0_overflow_count << 8) + TCNT0) << 2;
}
I would like to know what I need to change on the Timer interrupt stuff to make it work properly.

How do I receive a HIGH or LOW signal from an IR sensor on an Arduino?

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

no breadboard LED output

I'm making a simple temperature sensor to light one of two LEDs depending on the temperature.
For some reason the LED output only blinks the onboard LED (pin 13 on the Edison) once.
My temperature output is working fine, but I'm not sure why my code is working incorrectly.
Photo of the wiring here.
int temppin = 0;
int ledhigh = 7;
int ledlow = 8;
void setup()
{
Serial.begin(9600);
pinMode(temppin, INPUT);
pinMode(ledhigh, OUTPUT);
pinMode(ledlow, OUTPUT);
}
void loop()
{
int tempout = analogRead(temppin);
float volts = tempout * 5.0;
volts /= 1024.0;
float temp = (volts - 0.5) * 100 ;
Serial.print(temp); Serial.println(" celsius");
if (temp > 0){
Serial.print("high temp =");
digitalWrite(ledhigh, HIGH);
} else {digitalWrite(ledlow, HIGH);
Serial.print("low temp");
}
delay(3000);
}
The problem is probably that you're trying to use the analog input pins as output. You need to use the digital pins.
As explained in this video:
https://youtu.be/BtLwoNJ6klE?t=50s

athletic response light trainer by arduino

currently I am trying to develop a light response trainer by arduino , as a beginning I used 3 led and 3 push button ,the led must work randomly and when the ledx is flash the user press push bottonx and so on
of course I must use approximate sensor or something similar to be more reliable
when uploading following code all leds continuous week glow (flash) what the problem? thanks for help.
int ledselect = 0;
int led1 = 11;
int led2 = 12;
int led3 = 13;
int pb1 = 4;
int pb2 = 5;
int pb3 = 6;
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(pb1, INPUT);
pinMode(pb2, INPUT);
pinMode(pb3, INPUT);
}
void loop() {
int ledselect = random(3);
switch (ledselect) {
case 0: //if ledcolor equals 0 then the led1 will turn on
digitalWrite(led1, HIGH);
if (digitalRead(pb1),HIGH)
digitalWrite(led1,LOW);
break;
case 1: //if ledcolor equals 1 then the led2 will turn on
digitalWrite(led2, HIGH);
if (digitalRead(pb2),HIGH)
digitalWrite(led2,LOW);
break;
case 2: //if ledcolor equals 2 then the led3 will turn on
digitalWrite(led3, HIGH);
if (digitalRead(pb3),HIGH)
digitalWrite(led3,LOW);
break;
}
}
There are two timing problems in this program: 1) loop executes too quickly to see the response, 2) the pushbutton is read before the person has time to respond to the led.
I recommend restructuring your loop() to have the following general structure:
Turn on an led, based on random()
delay to give the person time to respond. For example, delay(500); will wait 1/2 second (1000 / 2).
read the pushbutton, based on a second switch(ledselect) code block.
delay to give the person time to get ready for the next loop. For example, delay(1000);
thankx alot I catch it ,i change the circuit so I Connect all output's switchs and connect to pin 4 in arduino (every switch become active only if his led on)
int ledselect = 0;
int led1 = 11;
int led2 = 12;
int led3 = 13;
int pb = 4;
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(pb, INPUT);
}
void loop() {
int ledselect = random(3);
switch (ledselect) {
case 0:
digitalWrite(led1, HIGH);
delay(20);
while(digitalRead(pb) != HIGH){delay(20);}
digitalWrite(led1,LOW);
break;
case 1:
digitalWrite(led2, HIGH);
delay(20);
while(digitalRead(pb) != HIGH){delay(20);}
digitalWrite(led2,LOW);
break;
case 2:
digitalWrite(led3, HIGH);
delay(20);
while(digitalRead(pb) != HIGH){delay(20);}
digitalWrite(led3,LOW);
break;
default:
delay(20);
break;
}
}

Resources