I have a project on Dragon12-Plus and I have to use de temperature sensor. I found this function
void temp(unsigned int x) //input voltage to convert
{
//ATD0CTL0,ATD0CTL1- reserved
ATD0CTL2 = 0xC0; //1100 0000
ATD0CTL3 = 0x08;
ATD0CTL4 = 0xEB; //1110 1011
ATD0CTL5 = 0xA4; //1010 0101
displayValue(x*500/255);
}
and the function has this call temp(ATD0DR0L); in main.
Why the value has the form x*500/255 ? Can someone help me?
Thanks!
Related
so I developed an app which will send a series of integers over Bluetooth (for RED, GREEN & BLUE) to a ESP32 which will then change the colour of 3 LED's (WS2811) based on the numbers it receives. It works the first time I send them but when I try to change the colour of the LED's a second time nothing happens.
By using the serial monitor of the Arduino IDE I have verified that the numbers are being received by the ESP32 every time I send the numbers, but I cannot understand why the LEDs are not changing colour after the first send.
The code is as follows :
#include <Arduino.h>
#include <fastled_config.h>
#define NUM_LEDS 3 // was 100
#define LED_TYPE WS2811
#define COLOR_ORDER RGB
#define DATA_PIN 4
//#define CLK_PIN 4
#define VOLTS 12
#define MAX_MA 4000
CRGBArray<NUM_LEDS> leds;
#define LED 2
int myRGB[30];
int counter =0;
int display =-1;
#include "BluetoothSerial.h"
// init Class:
BluetoothSerial ESP_BT;
// Parameters for Bluetooth interface
int incoming;
void setup() {
Serial.begin(115200);
ESP_BT.begin("ESP32_Control"); //Name of your Bluetooth interface -> will show up on your phone
delay( 3000 ); //safety startup delay
FastLED.setMaxPowerInVoltsAndMilliamps( VOLTS, MAX_MA);
FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS)
.setCorrection(TypicalLEDStrip);
}
void loop() {
delay(1000);
Serial.println(myRGB[1]);
Serial.println(myRGB[2]);
Serial.println(myRGB[3]);
leds[0].r = myRGB[1];
leds[0].g = myRGB[2];
leds[0].b = myRGB[3];
leds[1].r = myRGB[4];
leds[1].g = myRGB[5];
leds[1].b = myRGB[6];
leds[2].r = myRGB[7];
leds[2].g = myRGB[8];
leds[2].b = myRGB[9];
FastLED.show();
// -------------------- Receive Bluetooth signal ----------------------
if (ESP_BT.available())
{
incoming = ESP_BT.read(); //Read what we receive
digitalWrite(LED, HIGH);
counter ++;
myRGB[counter] = incoming;
if (counter > 29) counter = 0;
Serial.print("counter :" );
Serial.println(counter);
Serial.print( myRGB[counter]);
}
} // end loop
It looks like the LED colors are being shown before before Bluetooth inputs are received.
I would move FastLED.show() to the end of loop().
FastLED.show();
} // end loop
sorry for the late reply.
Ok the following code works, it not really much different to the original code
apart from this :
enter code hereif (counter < 9)
myRGB[counter] = incoming;
enter code here }
Which I'm guessing did make a difference (although it shouldn't have)
enter code here
#include <Arduino.h>
#include <fastled_config.h>
#define NUM_LEDS 3 // was 100
#define LED_TYPE WS2811
#define COLOR_ORDER RGB
#define DATA_PIN 4
//#define CLK_PIN 4
#define VOLTS 12
#define MAX_MA 4000
CRGBArray<NUM_LEDS> leds;
#define LED 2
int myRGB[30];
int counter =0;
int displayCounter =-1;
#include "BluetoothSerial.h"
// init Class:
BluetoothSerial ESP_BT;
// Parameters for Bluetooth interface
int incoming;
void setup() {
Serial.println("ESP started !!");
Serial.begin(115200);
ESP_BT.begin("ESP32_Control"); //Name of your Bluetooth interface -> will show up on your phone
delay( 3000 ); //safety startup delay
FastLED.setMaxPowerInVoltsAndMilliamps( VOLTS, MAX_MA);
FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS)
.setCorrection(TypicalLEDStrip);
}
void loop() {
delay(20); // WITHOUT THIS LINE MORE LIKELY TO GET INCORRECT DATA FROM BLUETOOTH
displayCounter ++;
if (displayCounter < 4){
Serial.println("***** the following values going into led[0] ( R G B ) *** ");
Serial.println(myRGB[1]);
Serial.println(myRGB[2]);
Serial.println(myRGB[3]);
Serial.println("***** the following values going into led[1] ( R G B ) *** ");
Serial.println(myRGB[4]);
Serial.println(myRGB[5]);
Serial.println(myRGB[6]);
Serial.println("***** the following values going into led[2] ( R G B ) *** ");
Serial.println(myRGB[7]);
Serial.println(myRGB[8]);
Serial.println(myRGB[9]);
}
leds[0].r = myRGB[1];
leds[0].g = myRGB[2];
leds[0].b = myRGB[3];
leds[1].r = myRGB[4];
leds[1].g = myRGB[5];
leds[1].b = myRGB[6];
leds[2].r = myRGB[7];
leds[2].g = myRGB[8];
leds[2].b = myRGB[9];
FastLED.show();
// -------------------- Receive Bluetooth signal ----------------------
if (ESP_BT.available())
{
displayCounter = -1;
incoming = ESP_BT.read(); //Read what we receive
//digitalWrite(LED, HIGH);
counter ++;
if (counter < 9)
myRGB[counter] = incoming;
}
else {counter = 0;
// Serial.println("End of bluetooth data");
}
} // end loop
I´m trying to read a dht22 sensor in ulp deep sleep, and it´s not working, can perhaps someone tell me what I´m doing wrong?
I´m reimplementing the arduino-DHT library because it´s working in non ulp mode with my sensor, it looks like this:
digitalWrite(pin, LOW); // Send start signal
pinMode(pin, OUTPUT);
delayMicroseconds(800);
pinMode(pin, INPUT);
digitalWrite(pin, HIGH); // Switch bus to receive data
// We're going to read 83 edges:
// - First a FALLING, RISING, and FALLING edge for the start bit
// - Then 40 bits: RISING and then a FALLING edge per bit
// To keep our code simple, we accept any HIGH or LOW reading if it's max 85 usecs long
uint16_t rawHumidity = 0;
uint16_t rawTemperature = 0;
uint16_t data = 0;
for ( int8_t i = -3 ; i < 2 * 40; i++ ) {
byte age;
startTime = micros();
do {
age = (unsigned long)(micros() - startTime);
if ( age > 90 ) {
error = ERROR_TIMEOUT;
return;
}
}
while ( digitalRead(pin) == (i & 1) ? HIGH : LOW );
if ( i >= 0 && (i & 1) ) {
// Now we are being fed our 40 bits
data <<= 1;
// A zero max 30 usecs, a one at least 68 usecs.
if ( age > 30 ) {
data |= 1; // we got a one
}
}
switch ( i ) {
case 31:
rawHumidity = data;
break;
case 63:
rawTemperature = data;
data = 0;
break;
}
}
Looks simple enough :D, I tried the first part with this code, but it doesn´t work:
rtc_gpio_init(15);
rtc_gpio_set_direction(15, RTC_GPIO_MODE_INPUT_OUTPUT);
And in my ulp script file:
.set temp_humidity_sensor_pin, 13 // gpio 15 (adc 13)
send_start_signal:
/* disable hold on gpio 15 (data pin) */
WRITE_RTC_REG(RTC_IO_TOUCH_PAD3_REG, RTC_IO_TOUCH_PAD3_HOLD_S, 1, 0)
/* switch to output mode */
WRITE_RTC_REG(RTC_GPIO_OUT_W1TS_REG, RTC_GPIO_OUT_DATA_W1TS_S + temp_humidity_sensor_pin, 1, 1)
/* send start signal (LOW) */
WRITE_RTC_REG(RTC_GPIO_OUT_REG, RTC_GPIO_OUT_DATA_S + temp_humidity_sensor_pin, 1, 0)
/* pull low for 800 microseconds (8Mhz) */
wait 6400
/* switch to input mode */
WRITE_RTC_REG(RTC_GPIO_OUT_W1TC_REG, RTC_GPIO_OUT_DATA_W1TC_S + temp_humidity_sensor_pin, 1, 1)
/* switch bus to receive data (HIGH) */
WRITE_RTC_REG(RTC_GPIO_OUT_REG, RTC_GPIO_OUT_DATA_S + temp_humidity_sensor_pin, 1, 1)
wait_for_sensor_preparation_low:
READ_RTC_REG(RTC_GPIO_IN_REG, RTC_GPIO_IN_NEXT_S + temp_humidity_sensor_pin, 1)
and r0, r0, 1
jump wait_for_sensor_preparation_low, eq
wait_for_sensor_preparation_high:
READ_RTC_REG(RTC_GPIO_IN_REG, RTC_GPIO_IN_NEXT_S + temp_humidity_sensor_pin, 1)
and r0, r0, 0
jump wait_for_sensor_preparation_high, eq
jump wake_up // <-- never called :(
Any ideas?
Your "and r0, r0, 0" instruction (near the end) always sets r0 to zero (x & 0 == 0 by definition), which means the following jump instruction will loop forever. Remember that the "eq" flag doesn't really mean "equal". It means "zero". I think you want:
wait_for_sensor_preparation_low:
READ_RTC_REG(RTC_GPIO_IN_REG, RTC_GPIO_IN_NEXT_S + temp_humidity_sensor_pin, 1)
and r0, r0, 1
jump wait_for_sensor_preparation_high, eq
jump wait_for_sensor_preparation_low
wait_for_sensor_preparation_high:
READ_RTC_REG(RTC_GPIO_IN_REG, RTC_GPIO_IN_NEXT_S + temp_humidity_sensor_pin, 1)
and r0, r0, 1
jump wait_for_sensor_preparation_high, eq
By the way, I wrote a C compiler for the ULP, which should make your life easier. It's on https://github.com/jasonful/lcc
unable to measure temperature from temperature sensor in arduino ,
its giving wrong temp = Temprature =499.51 * c. im connected tempreture sensor to arduino uno kit . i need the temp value like 35
int val;
int tempPin = 1;
void setup()
{
Serial.begin(9600);
}
void loop()
{
val = analogRead(tempPin);
float mv = ( val / 1024.0) * 5000;
float cel = mv / 10;
float farh = (cel * 9) / 5 + 32;
Serial.print("TEMPRATURE = ");
Serial.print(cel);
Serial.print("*C");
Serial.println();
delay(1000);
/* uncomment this to get temperature in farenhite
Serial.print("TEMPRATURE = ");
Serial.print(farh);
Serial.print("*F");
Serial.println();
*/
}
It is not clear what temperature sensor you are using. From your code, it would seem that you use an analog sensor. If so, you should
a) check the data sheet of the sensor and adjust your code accordingly
OR
b) calibrate your system by taking readings of known temperatures and adjusting your code accordingly.
With the info you give, it is difficult to be more specific.
I want to take first 10-bits value of UInt16 value. I tried;
x = (x & 0x00001111111111 )
But it gives me an error : Cannot convert type 'int' to 'ushort'.
How about, as 0x3FF in binary is 0000 0011 1111 1111
x = (UInt16)(x & 0x3FF)
You may try like this:-
UInt16 val1 = 8;
UInt16 value = (UInt16)(val1 << 6);
or simply cast it like this:-
x = (UInt16)(x & 0x3FF)
I am trying to generate a tone to the sound card (Frequency: 1950 hz, duration: 40 ms, level: -30 db, right-channel (stereo), on steam 1). Eventually, I would like to play two of these tones (one goes to channel 1 and one goes to channel 2).
Any help or direction is greatly appreciated.
Thanks,
DW
Hi Bjorn, I tried this but I am not getting the what I am expecting as a frequency (plus seems sound is not clean). Any ideas what's wrong?
I greatly appreciate any help.
#define SAMPLE_RATE (44100)
#define TABLE_SIZE (200)
float FREQUENCY = 422;
...
for(int i=0; i<TABLE_SIZE; i++ )
{
data.sine[i] = (float) sin( (double)i * ((2.0 * M_PI)/(double)SAMPLE_RATE) * FREQUENCY );
}
data.left_phase = 0;
data.right_phase = 0;
...
... in callback function ...
for(unsigned long i = 0; i < framesPerBuffer; i++ )
{
// fill output buffer with sin wave
*out++ = data->amp * data->sine[data->left_phase]; // left
*out++ = data->amp * data->sine[data->right_phase]; // right
data->left_phase += 1;
if( data->left_phase >= TABLE_SIZE )
data->left_phase -= TABLE_SIZE;
data->right_phase += 1;
if( data->right_phase >= TABLE_SIZE )
data->right_phase -= TABLE_SIZE;
}
PortAudio has sample code for generating tones, you just need to figure out the frequency. See for example this answer:
[portaudio]Transmit and Detect frequency - Windows
Update:
Rather than trying to store a table of sine data, simply calculate the sine value in the callback using this formula:
amplitude[n] = sin( n * desiredFreq * 2 * pi / samplerate )
so (untested) your code will look something like this:
typedef struct
{
long n;
} MyData;
float FREQUENCY = 422;
static int MyCallback(
const void *inputBuffer,
void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData
)
{
MyData *data = (MyData*)userData;
float *out = (float*)outputBuffer;
(void) timeInfo; /* Prevent unused variable warnings. */
(void) statusFlags;
(void) inputBuffer;
for(unsigned long i = 0; i < framesPerBuffer; i++ )
{
// fill output buffer with sin wave
float v = sin( data->n * FREQUENCY * 2 * PI / (float) SAMPLERATE )
*out++ = v; // left
*out++ = v; // right
}
return paContinue;
}
This code is not without problems: eg. eventually n will "wrap around" and I'm not sure if sin remains accurate and efficient as the input gets larger. Nevertheless it's a good starting point, and if you just need to generate a few seconds of a tone on modern hardware, this is really all you need. If you need something fancier, get this working first, then you can worry about making it more efficient and robust with a LUT.