Creating a wave using MEGA2560 (analogWrite) - arduino

Been trying to create a wave using the PWM ports (because this Arduino doesn't have DAC)of an Arduino Mega using this code. In the simulation I use a wave form generator that goes to A0, then I just want to convert it from 1023 bits to 255 but I get nothing as output.
int in = A0;
int out = 10;
void setup()
{
pinMode(in, INPUT);
pinMode(out, OUTPUT);
}
void loop(){
analogRead(in);
analogWrite(10, in/4);
}
Any suggestion would be great, thanks in advance!

You're discarding the returned value from analogRead. Change:
void loop(){
analogRead(in);
analogWrite(10, in/4);
}
to:
void loop(){
int p = analogRead(in);
analogWrite(out, p / 4);
}

The pin 10 is digital output, isn't it ?
Moreover there is a function for creating a wave : tone(pin, freq, time);

Related

Joystick with switch control for Arduino Uno R3 will not display switch push

I am trying to get my code to show a switch input from a joystick on an Arduino Uno.
When I view it on the serial monitor, I see the x and y axis being displayed when I move the joystick.
When I click it I do not get anything to show up. I have the switch set to pin 2 on the Arduino.
There are no errors when I compile the code.
int pin_x = A0;
int position_x = 0;
int pin_y = A1;
int position_y = 0;
int pin_z = 2; // switch pin
int position_z = LOW;
void setup() {
Serial.begin(9600); // initialize serial communications at 9600 bps
pinMode(pin_x, INPUT); // set pin mod as INPUT
pinMode(pin_y, INPUT);
pinMode(pin_z, INPUT); // switch pin
}
void read(){
position_x = analogRead(pin_x);
position_y = analogRead(pin_y);
position_z = digitalRead(pin_z);
}
void show(){
Serial.print(" X:"); //print information to Serial Monitor
Serial.print(position_x);
Serial.print(" Y:");
Serial.print(position_y);
Serial.print(" Z:");
Serial.print(position_z);
}
void loop() {
read();
show();
delay(500);
}
There are several methods you could use to find the error, here are a couple suggestions:
read the datasheet of your joystick
use a multimeter to check conductivity of the two switch pins
hook something else to your switch input, check that the input is working
That said, the way your code is written it expects your switch pin to be connected to Vcc, check if that is really the case.
Try this:
void setup() {
Serial.begin(9600); // initialize serial communications at 9600 bps
pinMode(pin_x, INPUT); // set pin mod as INPUT
pinMode(pin_y, INPUT);
pinMode(pin_z, INPUT); // switch pin
digitalWrite(pin_z, HIGH);
}
Otherwise code looks correct. The switch on a Joystick is indeed digital, while the others are analog, you got that right.
Here's a video on how to work with joysticks, maybe it helps: https://www.youtube.com/watch?v=MlDi0vO9Evg

Send a Analog Value from Arduino A to Analog Pin of Arduino B

I'm trying to Read values from LM35 sensor on Arduino UNO and send it to another Arduino board through a PWM pin and an analog Pin
When I run this project, The Serial Emulator of Arduino A is showing right values but Second one is always 0.00.
Here is my first Arduino Code:
int pin = 2;
int TempPin = A0;
int pinAnalog = 3;
void setup() {
pinMode(3, OUTPUT);
Serial.begin(9600);
}
void loop() {
float tmp = analogRead(TempPin);
float Result = (tmp/1024.0) * 500;
Serial.println(Result);
analogWrite(pinAnalog, Result);
delay(3000);
}
And Here is My Second Arduino Code:
void setup() {
Serial.begin(9600);
}
void loop() {
float res = analogRead(A0);
Serial.println(res);
delay(3000);
}
What's wrong with my project or code?
I understand this is an exercise only, as PWM itself is not suitable to feed analogRead. (better measure pulse durations, if you really want to use it for data transmission.)
For a 400 Hz PWM you need a RC Value of e.g. 20 ms to filter the PWM pulses reasonably.
(e.g 1µF * 20k)
As you work in a 3sec Cycle, much bigger values are fine as well.
BTW: Sender could be simplified to:
const byte inPin = A0;
const byte outPin = 3;
void setup() {
Serial.begin(9600);
}
void loop() {
byte tmp = analogRead(inPin)/4; // 0 .. 255
analogWrite(outPin, tmp);
Serial.println((int)tmp);
delay(3000);
}

Having hard time achieving 1 Msps with external 12bit- ADC(LTC2365) using Arduino Due

I have an LTC2365/1 Msps 12bit-ADC with SPI pins. Somehow I only could achieve a maximum of 200 Ksps with this ADC rather than something close to 1 Msps using an Arduino Due. I wonder if anyone could help me with this issue. I tried many ways but couldn't figure it out.
Datasheet for the ADC:
http://cds.linear.com/docs/en/datasheet/23656fb.pdf
Here is the code I use for Arduino:
#include <SPI.h>
const int spi_ss = 10; // set SPI SS Pin
uint8_t byte_0, byte_1; // First and second bytes read
uint16_t spi_bytes; // final 12 bit shited value
long int starttime,endtime;
long int count=0;
void setup() {
SerialUSB.begin(2000000); // begin serial and set speed
pinMode(spi_ss, OUTPUT); // Set SPI slave select pin as output
digitalWriteDirect(spi_ss, HIGH); // Make sure spi_ss is held high
SPI.begin();
SPI.beginTransaction(SPISettings(16000000, MSBFIRST, SPI_MODE0));
loop2();
SPI.endTransaction();
}
void loop2() {
starttime=millis();
endtime=starttime;
while((endtime-starttime)<=1000) {
// write the LTC CS pin low to initiate ADC sample and data transmit
digitalWriteDirect(spi_ss, LOW);
byte_0 = spi_read(0x00); // read firt 8 bits
byte_1 = spi_read(0x00); // read second 8 bits
digitalWriteDirect(spi_ss, HIGH);
// wite LTC CS pin high to stop LTC from transmitting zeros.
spi_bytes = ( ( (byte_0 <<6) ) + (byte_1 >>2) );
SerialUSB.println(spi_bytes);
count=count+1;
endtime=millis();
}
//samples per second
SerialUSB.println(count);
}
static inline uint8_t spi_read(uint8_t b) {
return SPI.transfer(b);
}
inline void digitalWriteDirect(int pin, boolean val) {
if(val) g_APinDescription[pin].pPort -> PIO_SODR = g_APinDescription[pin].ulPin;
else g_APinDescription[pin].pPort -> PIO_CODR = g_APinDescription[pin].ulPin;
}

Arduino frequency counter Issue

I am successfully using this great Arduino frequency library: Arduino frequency counter.
But there is one issue with my LEDs. They are working if I give them the value "HIGH" or "255". But however they are not working with a lower value. I have tested the LEDs in another sketch. So they are correctly wired and are working fine, also the Arduino. It seems to be a problem with the "FreqCounter::start(100);" line. If I remove it, the lower values are working, but of course the frequency counter is not...
How can I fix this problem?
Here's the code:
#include <FreqCounter.h>
unsigned long frq;
/*** OUTPUT LED ***/
int ledGreen = 9;
int ledYellow = 10;
int ledRed = 11;
void setup() {
pinMode(ledGreen, OUTPUT);
pinMode(ledYellow, OUTPUT);
pinMode(ledRed, OUTPUT);
Serial.begin(115200);
Serial.println("Frequency Counter");
}
void loop() {
/*** WRITE ***/
analogWrite(ledGreen, 255);
analogWrite(ledYellow, 100);
analogWrite(ledRed, 10);
/*** FREQUENCY COUNTER ***/
FreqCounter::f_comp = 10; // Calibration value / calibrate with a professional frequency counter
FreqCounter::start(100); // 100 ms gate time
while (FreqCounter::f_ready == 0){
frq = FreqCounter::f_freq;
}
}
According to the documentation this library repurposes TIMER1 for counting the frequency. However PWM for pins 9 and 10 requires TIMER1 with the default setup.

Arduino code not working

My plan is to make an adjustable speed strobe. I'm just learning to code and this is what I have so far.
int potentiometer_pin = A0;
int led_pin = 7;
int on_time = 100;
int analog_value_multiplier = 15;
int strobe_delay = 0;
int minimum_delay = 500;
void setup() {
pinMode(led_pin, OUTPUT);
}
void loop() {
strobe_delay = minimum_delay + analogRead(potentiometer_pin) * analog_value_multiplier;
digitalWrite(led_pin, HIGH);
delayMicroseconds(on_time);
digitalWrite(led_pin, LOW);
delayMicroseconds(strobe_delay - on_time);
}
I have the LED + on digital 7 with a 220ohm resistor and the pot on analog 0, it is a 10K pot with one side hooked up to 5v+ and the other to ground. My problem is that the LED stays on and turning the pot just changes the brightness. Any help on what to do - not just a new code but what to do? I want to actually learn how to fix this.
like david said, but I will add I think you want delay not delayMicroseconds.
http://arduino.cc/en/Reference/delay
Your speeds are all WAY too fast. Multiply all your delays by about 100. You've made a pulse width modulator.

Resources