I am making a hand gesture controlled tank.I made a code to test the dc motors on Arduino UNO, but the problem is that motors are just vibrating and not moving. The code is:
#include <AFMotor.h>
AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);
void setup() {
motor1.setSpeed(255);
motor2.setSpeed(255);
motor3.setSpeed(255);
motor4.setSpeed(255);
}
void loop() {
// Serial.print("tien");
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
delay(10);
//Serial.print("lui");
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
delay(10);
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
delay(10);
//Serial.print("tack");
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
delay(10);
}
I don't know why it isn't moving. I checked it replacing the wires, but same is happening. I checked them individually connecting to the batteries and they are working pretty well. Please HELP!!
If you set the delay to 10 ms, this is what happens:
-motor moves clockwise for 10 ms
-motor stops for 10 ms
-motor moves counter clockwise for 10 ms
-motor stops for 10 ms
-loop
And since 10ms (milliseconds) is a very small amount of time it appears that the motor is vibrating.
To make it move, increase the delay to something like 1000 ms which is 1 second.
It is supposed to work like that. Give it another shot. You can also try removing the MOTOR12_1KHZ. Also try removing the parts containing RELEASE and BACKWARD, this way it will be simplified and there can be less chances of error. If that works, then go for adding more parts slowly.
Related
I started using Visual Studio 2022 with Visual Micro, Arduino 1.6/1.8 as my new Arduino IDE, because I needed some extra features, that the original Arduino IDE does not provide.
To plot a graph, I am using the breakpoint command {#Plot.windowName.graphName variable} to print out the graph for my variable x with x = sin(2i), i being incremented by 0.01 every cycle by a for loop.
Here is my code:
double x;
void setup() {
Serial.begin(115200);
Serial.println("Hello, World!");
}
void loop() {
float i;
for (i = 0; i < 1000; i=i+0.01) {
x = sin(2*i);
Serial.println(x);
delay(50);
}
}
Here is a picture of the breakpoint command:
The graph this outputs looks jagged and has cyclically occurring lags every 1.5 second.
Where might this originate from?
Is it the Visual Micro software, the speed/noisiness of the serial connection (bad cable, etc.) or the Arduino Uno being overloaded by that?
I already tried changing the data rates as recommended in the answer of visualmicro.
Unfortunately it is not about the data rate itself.
The Serial Debugger in Visual Micro throttles the data coming from the sketch to prevent the PC becoming overloaded with Serial data.
If you enable the vMicro > Debugger > Full Speed (no throttle), this should speed up the data coming in.
Also the interval the chart refreshes at can be changed from the controls above it, in this scenario "At rate of incoming data" would be best suited.
for a prototype we need to have a hardware switch (e.g., a momentary pushbutton) trigger the taking of a screenshot on a PC and save it to file. Writing some windows software to take a screenshot and save it is trivial, the slightly trickier part is how to get an electrical signal (we can choose the voltage, and provide power as necessary) to the software. We absolutely want to keep this simple (i.e., no labview or anything) and reliable as possible. I see small module boxes such as this
https://labjack.com/products/u3?gclid=EAIaIQobChMI-MXkjcbB2gIVxVYNCh3C6AODEAQYAiABEgK_OvD_BwE
available, but are there even simpler solutions? I'm thinking of (but haven't taken the time to test) possibly a parallel-port-to-USB converter (which would be similar to the more common RS232-to-USB converter but may allow detection of individual high/lows(just a guess, never worked with a parallel driver from windows)), or something like that. Just querying for ideas before I spend time buying things and testing. Thanks!
This can be easily done with an Arduino Leonardo, Micro, and Due module. This page has an example very similar to your project:
// use this option for OSX:
char ctrlKey = KEY_LEFT_GUI;
// use this option for Windows and Linux:
// char ctrlKey = KEY_LEFT_CTRL;
void setup() {
// make pin 2 an input and turn on the
// pullup resistor so it goes high unless
// connected to ground:
pinMode(2, INPUT_PULLUP);
// initialize control over the keyboard:
Keyboard.begin();
}
void loop() {
while (digitalRead(2) == HIGH) {
// do nothing until pin 2 goes low
delay(500);
}
delay(1000);
// new document:
Keyboard.press(ctrlKey);
Keyboard.press('n');
delay(100);
Keyboard.releaseAll();
// wait for new window to open:
delay(1000);
}
How can I use EnvGen in a loop in such a way that it won't restart at every iteration of the loop?
What I need it for: piecewise synthesis. I want e.g. 50ms of a xfade between first and second Klang, then a 50ms xfade between second and third Klang, then a 50ms xfade between third and fourth Klang and so on, and I want this concatenation as a whole to be modulated by an envelope.
Unfortunately the EnvGen seems to restart from the beginning on every iteration of the loop that plays the consecutive Klang pairs. I want a poiiiiinnnnnnnnnng, but no matter what I try all I get is popopopopopopopopo.
2019 EDIT:
OK, since nobody would answer the "how to achieve the goal" question, I am now downgrading this question to a mere "why doesn't this particular approach work", changing the title too.
Before I paste some code, a bit of an explanation: this is a very simplified example. While my original desire was to modulate a complicated, piecewise-generated sound with an envelope, this simplified example only "scissors" 100ms segments out of the output of a SinOsc, just to artificially create the "piecewise generation" situation.
What happens in this program is that the EnvGen seems to restart at every loop iteration: the envelope restarts from t=0. I expect to get one 1s long exponentially fading sound, like plucking a string. What I get is a series of 100ms "pings" due to the envelope restarting at the beginning of each loop iteration.
How do I prevent this from happening?
Here's the code:
//Exponential decay over 1 second
var envelope = {EnvGen.kr(Env.new([1,0.001],[1],curve: 'exp'), timeScale: 1, doneAction: 2)};
var myTask = Task({
//A simple tone
var oscillator = {SinOsc.ar(880,0,1);};
var scissor;
//Prepare a scissor that will cut 100ms of the oscillator signal
scissor = {EnvGen.kr(Env.new([1,0],[1],'hold'),timeScale: 0.1)};
10.do({
var scissored,modulated;
//Cut the signal with the scisor
scissored = oscillator*scissor;
//Try modulating with the envelope. The goal is to get a single 1s exponentially decaying ping.
modulated = {scissored*envelope};
//NASTY SURPRISE: envelope seems to restart here every iteration!!!
//How do I prevent this and allow the envelope to live its whole
//one-second life while the loop and the Task dance around it in 100ms steps?
modulated.play;
0.1.wait;
});
});
myTask.play;
(This issue, with which I initially struggled for MONTHS without success, actually caused me to shelve my efforts at learning SuperCollider for TWO YEARS, and now I'm picking up where I left off.)
You way of working here is kind of unusual.
With SuperCollider, the paradigm shift you're looking for is to create SynthDefs as discrete entities:
s.waitForBoot ({
b = Bus.new('control');
SynthDef(\scissors, {arg bus;
var env;
env = EnvGen.kr(Env.linen);
//EnvGen.kr(Env.new([1,0.001],[1],curve: 'exp'), timeScale: 1, doneAction: 2);
Out.kr(bus, env);
}).add;
SynthDef(\oscillator, {arg bus, out=0, freq=440, amp = 0.1;
var oscillator, scissored;
oscillator = SinOsc.ar(freq,0,1);
scissored = oscillator * In.kr(bus) * amp;
Out.ar(out, scissored);
}).add;
s.sync;
Task({
Synth(\scissors, [\bus, b]);
s.sync;
10.do({|i|
Synth(\oscillator, [\bus, b, \freq, 100 * (i+1)]);
0.1.wait;
});
}).play
});
I've changed for a longer envelope and a change in pitch, so you can hear all the oscillators start.
What I've done is I've defined two SynthDefs and a bus.
The first SynthDef has an envelope, which I've lengthened for purposes of audibility. It writes the value of that envelope out to a bus. This way, every other SynthDef that wants to use that shared envelope can get it by reading the bus.
The second SynthDef has an a SinOsc. We multiply the output of that by the bus input. This uses the shared envelope to change the amplitude.
This "works", but if you run it a second time, you'll get another nasty surprise! The oscillator SynthDefs haven't ended and you'll hear them again. To solve this, you'll need to give them their own envelopes or something else with a doneAction. Otherwise, they'll live forever.Putting envelopes on each individual oscillator synth is also a good way to shape the onset of each one.
The other new thing you might notice in this example is the s.sync; lines. A major feature of SuperCollider is that the audio server and the language are separate processes. That line makes sure the server has caught up, so we don't try to use server-side resources before they're ready. This client/server split is also why it's best to define synthdefs before using them.
I hope that the long wait for an answer has not turned you off permanently. You may find it helpful to look at some tutorials and get started that way.
I have recently been trying to pneumatically actuate a cylinder using a 12 V double solenoid and an Arduino Uno. The solenoid works when tested without code and wiring, however when I try to actuate the cylinder using code, nothing happens. I have a feeling that the way in which I wired everything to the breadboard may be incorrect, so I was wondering if anyone had any tips or good schematics by which I could wire it all together.
The materials I am using are two PNP transistors, two resistors, two diodes, and then the actual solenoid and similar hardware. My code is just a simple LED blink code which can be used to send signals to the solenoids, so I do not believe that is the issue. However, I have attached it underneath just in case.
int solenoid1 = 4;
int solenoid2 = 5;
void setup() {
// put your setup code here, to run once:
pinMode(solenoid1, OUTPUT);
pinMode(solenoid2, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(solenoid1, HIGH);
delay(1000);
digitalWrite(solenoid1, LOW);
digitalWrite(solenoid2, HIGH);
delay(1000);
digitalWrite(solenoid2, LOW);
}
Arduino digital pin puts 5V when HIGH. So your 12V solenoid do not get enough voltage to run. You have to use relay and additional 12V power supply to run your solenoid with Arduino.
The code which I have written is not running when the Arduino gets plugged in
How can you see this?
A solenoid certainly cannot be controlled by 5V * 20mA from an Arduino pin. Further requirements depend on the solenoid you want to use. (Current consumption, free-wheeling diode already integrated?)
hey I have this problem that I have been dealing with for the last few day I have a way around it but I know for a fact that it is really bad way to program and I am really hoping someone can suggest a better way to go about this.
I am trying to get a time from an RTC ( real clock timer) I think that is the acronym anyway ,,,
Its using a DS 1307 IC ,,,,, every time I start and run the program under constant power it boots fine, but the moment I remove the USB cable for around 10 seconds and then reattach it will give me these funny times.
Something like year 2036 and 46 hours and 165 mins really just garbage.
So I read somewhere that these time are just the programs way of saying that there is no connection to the device. That I don't really get because its permanently plugged in but hey thats what it wants.
So here is a basic code that I got from an example that came with the library.
I thought because there is no connection just do a while loop until the device gets connection this works well but it takes like sometimes 10 seconds to boot up.
The RTC has a battery backup connected and with lines SCL to A5 and SDA A4
As I say it work but takes really long to boot up and give me the correct time.
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;
void setup () {
Serial.begin(57600);
Wire.begin();
RTC.begin();
Serial.println("RTC capturing time!");
while (! RTC.isrunning())
{
Serial.println("RTC is NOT running!");
Wire.begin();
RTC.begin();
}
Serial.println("RTC IS running!");
// following line sets the RTC to the date & time this sketch was compiled
// RTC.adjust(DateTime(__DATE__, __TIME__));
}
void loop () {
DateTime now = RTC.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.println();
delay(1000);
}
The output looks like this just with A LOT more RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC IS running!
2013/6/11 22:22:0
2013/6/11 22:22:2
2013/6/11 22:22:3
2013/6/11 22:22:4
2013/6/11 22:22:5
and if I was to not include my while loop idea I get really messed up times and dates as I said before until it for some reason comes right by itself.
Please let me know if anyone knows of a better way to fix my problem I am really confused to why this would be occurring.
Try this code...
void setup () {
Serial.begin(57600);
Wire.begin();
RTC.begin();
Serial.println("RTC capturing time!");
while (!RTC.isrunning())
{
// do not really need this, remove after testing
Serial.println("RTC is NOT running!");
delay(10);
}
Serial.println("RTC IS running!");
// following line sets the RTC to the date & time this sketch was compiled
// RTC.adjust(DateTime(__DATE__, __TIME__));
}
You should clarify how you power the RTC when USB is disconnected. First you should check if the battery is actually good. Then you need to ensure that the Arduino notices that the RTC was battery powered. This is because the RTC will shut down I2C completely while battery powered --> I2C must be reinitialized when power is recovered. The point is that your DS1307 library might not account for that.
In doubt you need to analyze the source code of your library AND read the datasheet of your DS1307 chip.
Another thing is that the datasheet says
the device switches from battery to VCC when VCC is greater than VBAT+0.2V and
recognizes inputs when VCC is greater than 1.25 x VBAT
Did you ever measure VBAT and VCC at startup?